Skip to content

Instantly share code, notes, and snippets.

@dgulino
Last active August 29, 2015 14:11
Show Gist options
  • Save dgulino/298516f7977c57199a4a to your computer and use it in GitHub Desktop.
Save dgulino/298516f7977c57199a4a to your computer and use it in GitHub Desktop.
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
@dgulino
Copy link
Author

dgulino commented Dec 21, 2014

mix new tgraph
copy gist to tgraph/lib/tgraph.ex
cd tgraph
edit mix.exs:

  def project do
    [app: :tgraph,
     version: "0.0.1",
     elixir: "~> 1.0.0",
     escript: [main_module: Tgraph],
     deps: deps]
  end

mix escript.build

while true; do echo $RANDOM  ;done | ./tgraph -t 10000 -d

************4142
************************************************************************************30380
*************************************************17829
***************************************************************************************31698
****************************************************************************27497
************************************************************************************30575
*******************************************15394
****************5380
*****************************************************************************27879
**********************************************************20853
************************8430
****************************9812

(values over 10K are colored red (color not supported in github markdown))

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