Last active
August 29, 2015 14:11
-
-
Save dgulino/298516f7977c57199a4a to your computer and use it in GitHub Desktop.
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
require Logger | |
defmodule Tgraph do | |
def main(args) do | |
Logger.info inspect args | |
options = parse_args(args) | |
f = Dict.get(options,:file,:stdio) | |
case is_atom(f) do | |
true -> f | |
false -> {:ok, f} = File.open f | |
end | |
{cols, _} = System.cmd("tput",["cols"],[]) | |
{cols, _} = Integer.parse(String.strip(cols)) | |
cols = cols - 8 | |
options = Dict.put(options, :columns, cols) | |
maximum = Dict.get(options,:maximum,1) | |
symbol = Dict.get(options,:symbol,"*") | |
columns = Dict.get(options,:columns) | |
display_number = Dict.get(options,:display_number,false) | |
threshold = Dict.get(options,:threshold,0) | |
proc_file(f, maximum, symbol, columns, display_number, threshold ) | |
end | |
def parse_args(args) do | |
{options , _, _} = OptionParser.parse(args, | |
switches: [ | |
help: :boolean, | |
maximum: :integer, | |
symbol: :string, | |
columns: :integer, | |
display_number: :boolean, | |
threshold: :integer, | |
file: :string | |
], | |
aliases: [ | |
h: :help, | |
m: :maximum, | |
s: :symbol, | |
c: :columns, | |
d: :display_number, | |
t: :threshold, | |
f: :file | |
] | |
) | |
options | |
end | |
def version do | |
IO.puts "version: 0.1" | |
end | |
def proc_file(f, maximum, symbol, columns, display_number, threshold ) do | |
l = IO.gets(f, '') | |
case l do | |
{:error, reason} -> | |
Logger.info inspect {:error, reason} | |
:eof -> | |
:ok | |
"\n" -> | |
false | |
line -> | |
stripped = String.strip(line,?\n) | |
{num, _} = Integer.parse(stripped) | |
IO.write IO.ANSI.reset() | |
case num > 0 do | |
true -> | |
case num >= maximum do | |
true -> | |
newmax = num | |
IO.write IO.ANSI.yellow() | |
false -> | |
newmax = maximum | |
IO.write IO.ANSI.normal() | |
end | |
scale = columns / newmax | |
num_chars = round(num * scale) + 1 | |
graph = List.to_string(Enum.map(1..num_chars, fn n -> symbol end)) | |
case threshold do | |
0 -> | |
false | |
_ -> | |
case num >= threshold do | |
true -> | |
IO.write IO.ANSI.red() | |
false -> | |
IO.write IO.ANSI.reset() | |
false | |
end | |
end | |
case display_number do | |
true -> | |
IO.puts "#{graph}#{num}" | |
false -> | |
IO.puts "#{graph}" | |
end | |
proc_file(f, newmax, symbol, columns, display_number, threshold) | |
false -> | |
IO.write IO.ANSI.normal() | |
IO.write IO.ANSI.reset() | |
IO.puts "#{num}" | |
proc_file(f, maximum, symbol, columns, display_number, threshold) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
mix new tgraph
copy gist to tgraph/lib/tgraph.ex
cd tgraph
edit mix.exs:
mix escript.build
(values over 10K are colored red (color not supported in github markdown))