Skip to content

Instantly share code, notes, and snippets.

@emad-elsaid
Created July 9, 2017 20:54
Show Gist options
  • Select an option

  • Save emad-elsaid/f10a5412db2795f359fe56fc46abc461 to your computer and use it in GitHub Desktop.

Select an option

Save emad-elsaid/f10a5412db2795f359fe56fc46abc461 to your computer and use it in GitHub Desktop.
graph a file as bars in terminal, or from standard input
#!/usr/bin/env ruby
# usage : graph file
# usage : cat file | graph
# file lines are casted to float so it has to start with a number
MAX_LENGTH = 80
BAR_CHAR = '|'.freeze
data = ARGV.empty? ? STDIN.read.lines : File.read(ARGV.first).lines
max = data.map(&:to_f).max
data.each do |line|
number = line.to_f
bar = BAR_CHAR * (number / max * MAX_LENGTH)
bar = bar.rjust(MAX_LENGTH)
print bar, ' ', line
end
@emad-elsaid
Copy link
Author

output example

                                                                                 0,zero
                                                        |||||||||||||||||||||||| 30,fourth
                                        |||||||||||||||||||||||||||||||||||||||| 50,second
        |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 90,third
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| 100,first choice

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment