Sparkr 0.3.0 - live and in full color
Sparkr is a port of spark for Ruby.
It lets you create ASCII sparklines for your Ruby CLIs: ▁▂▃▅▇
Now live and in full color
As of version 0.3.0, Sparkr lets you create colored sparklines.
Let’s say you have your list of open and closed issues on GitHub. You can then use Sparkr to create a little graph showing you the proportions:
# examples/issues.rb
require 'sparkr'
open_issue_count = 3
closed_issue_count = 13
list = [open_issue_count, closed_issue_count]
puts "Issues: " + Sparkr.sparkline(list)
But you want to format the sparkline so that the open issues are red and the closed ones are green (to quickly see how you are doing).
One tick at a time
We can do that, because Sparkr 0.3.0 lets you call Sparkr.sparkline
with an optional block to format each bar/tick. We can use this to color the chart.
To actually color the bars/ticks, we need to use a gem that adds ANSI-coloring to Strings, like Term::ANSIColor. It adds a #color
method to String
that takes a color name as parameter.
We can then rewrite the above example like this:
# examples/issues_colored.rb
require 'sparkr'
require 'term/ansicolor'
class String
include Term::ANSIColor
end
open_issue_count = 3
closed_issue_count = 13
list = [open_issue_count, closed_issue_count]
sparkline = Sparkr.sparkline(list) do |tick, count, index|
if index == 0
tick.color(:red)
else
tick.color(:green)
end
end
puts "Issues: " + sparkline
Et voilà
And that’s how you use Sparkr to color your sparklines.
For a “real world application” of this feature, take a look at Inch, a documentation measurement tool for Ruby:
(showing output from inch stats
)
Sparkr is available via RubyGems. The source code is published on GitHub.