Created
July 9, 2017 20:54
-
-
Save emad-elsaid/f10a5412db2795f359fe56fc46abc461 to your computer and use it in GitHub Desktop.
graph a file as bars in terminal, or from standard input
This file contains hidden or 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 | |
| # 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
output example