Skip to content

Instantly share code, notes, and snippets.

@dpk
Created November 16, 2011 14:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dpk/1370253 to your computer and use it in GitHub Desktop.
Save dpk/1370253 to your computer and use it in GitHub Desktop.
Display a sparkline live, based on the STDIN (newline-seperated) or a comma-seperated list on the cmd-line. Options are obvious enough.
#!/usr/bin/env ruby
# encoding: UTF-8
# live sparkline display
# gems required: ruby-terminfo, getoptions
require 'terminfo'
require 'getoptions'
def bar_height n, min, max
h = (((n - min) / (max - min)) * $bs).to_i
h = 0 if h < 0; h = $bs if h > $bs;
return h
end
def show_bar h
b = $bars[h]
$bar.prepend b
ww = TermInfo.screen_size[1]
print "\r#{$bar[0..ww-1]}"
$stdout.flush
end
$bars = %w{▂ ▃ ▄ ▅ ▆ ▇}; $bs = $bars.length-1
$o = GetOptions.new %w{min|l:f max|u:f sep|f:s}
$bar = ""
sep = $o[:sep] || $/
min = $o[:min] || 0
max = $o[:max] || $bs
if ARGV[0]
sep = ',' unless $o[:sep]
ns = ARGV[0].split(sep).map(&:to_f)
min, max = ns.min, ns.max
hs = ns.map {|n| bar_height n, min, max}
hs.each {|h| show_bar h}; puts
else
trap("SIGINT") { exit 0 }
while gets sep
n = $_.to_f
h = bar_height n, min, max
show_bar h
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment