Created
November 16, 2011 14:58
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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