Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dgulino
Last active December 12, 2015 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dgulino/4750118 to your computer and use it in GitHub Desktop.
Save dgulino/4750118 to your computer and use it in GitHub Desktop.
Console plotting in erlang script (escript)
#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp disable
%% Author: Drew Gulino
-module(tgraph).
-export([main/1]).
main(CmdLine) ->
OptSpecList = option_spec_list(),
case getopt:parse(OptSpecList, CmdLine) of
{ok, {Options, NonOptArgs}} ->
true;
{error, {Reason, Data}} ->
Options = [],
NonOptArgs = [],
io:format("Error: ~s ~p~n~n", [Reason, Data]),
version(),
getopt:usage(OptSpecList, "tgraph")
end,
Symbol = get_opt_value(symbol,Options),
Columns = get_opt_value(columns,Options),
Display_number = get_opt_value(display_number,Options),
Threshold = get_opt_value(threshold,Options),
Maximum = get_opt_value(maximum,Options),
Bold = strip_newlines(os:cmd("tput bold")),
Init = strip_newlines(os:cmd("tput init")),
Dim = strip_newlines(os:cmd("tput sgr0")),
Red = strip_newlines(os:cmd("tput setaf 1")),
%Green = strip_newlines(os:cmd("tput setaf 2")),
%Yellow = strip_newlines(os:cmd("tput setaf 3")),
%Blue = strip_newlines(os:cmd("tput setaf 4")),
%Magenta = strip_newlines(os:cmd("tput setaf 5")),
case NonOptArgs of
[] ->
F = standard_io;
_ ->
{ok, F} = file:open(NonOptArgs, read)
end,
proc_file(F, {Symbol, Columns, Display_number, Threshold, Maximum} , {Bold, Init, Dim, Red}).
version() ->
io:format("Version: 1.1\n").
get_opt_value(Key, Options) ->
case lists:keyfind(Key,1,Options) of
{Key, Value} ->
Value
end,
Value.
option_spec_list() ->
%CurrentUser = os:getenv("USER"),
[
%% {Name, ShortOpt, LongOpt, ArgSpec, HelpMsg}
{help, $h, "help", undefined, "Show the program options"},
{version, $v, "version", undefined, "Version"},
{display_number, $n, "display_number", {boolean, true}, "Display number w/graph"},
{columns, $c, "columns", {integer, 72}, "Display columns (default = 72)"},
{symbol, $s, "symbol", {string, "*"}, "Symbol to display (default = '*')"},
{threshold, $t, "threshold", {integer, 0}, "Will color lines over this value"},
{maximum, $m, "maximum", {integer, 0}, "Presets the scale for this maximum value (default = 0)"}
].
proc_file(F, Options, Tput) ->
{Symbol, Columns, Display_number, Threshold, Maximum} = Options,
{Bold, Init, Dim, Red} = Tput,
%Columns = erlang:list_to_integer(os:cmd("tput cols")) - 8},
L = io:get_line(F, ''),
case L of
eof ->
ok;
"\n" ->
false;
Line ->
Stripped = strip_newlines(Line),
Num = cast_to_integer(Stripped),
io:put_chars(Init),
case Num > 0 of
true ->
case Num >= Maximum of
true ->
NewMax = Num,
io:put_chars(Bold);
false ->
NewMax = Maximum,
io:put_chars(Dim)
end,
Scale = Columns / NewMax,
Graph = lists:map(fun(_) -> io_lib:format(Symbol,[]) end , lists:seq(1,erlang:round(Num * Scale))),
case Threshold of
0 ->
false;
_ ->
case Num >= Threshold of
true ->
io:put_chars(Red);
false ->
%io:put_chars(Init)
false
end
end,
case Display_number of
true ->
io:format("~s~p~n",[Graph,Num]);
false ->
io:format("~p~n",[Graph])
end,
NewOptions = {Symbol, Columns, Display_number, Threshold, NewMax},
proc_file(F,NewOptions, Tput);
false ->
io:put_chars(Dim),
io:put_chars(Init),
io:format("~p~n",[Num]),
proc_file(F,Options, Tput)
end
end.
strip_newlines(String) ->
string:strip(re:replace(String,"(.*)[\n\r]","\\1", [global,{return,list}])).
cast_to_integer([]) ->
[];
cast_to_integer(Input) when is_integer(Input) ->
Input;
cast_to_integer(Input) when is_float(Input) ->
erlang:round(Input);
cast_to_integer(Input) when is_list(Input)->
case lists:member($., Input) of
true ->
erlang:round(erlang:list_to_float(Input));
false ->
erlang:list_to_integer(Input)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment