Lua で CUI 棒グラフ書いてみルア #
ルアルアうるさくてすまんね
なんか使うもん書かんと覚えんので、
15:40:01 0.01
15:50:01 0.04
16:00:01 0.01
16:10:01 0.03
16:20:01 0.01
16:30:01 0.01
16:40:01 0.01
16:50:01 0.01
17:00:01 0.02
17:10:02 0.17
17:20:01 0.15
17:30:01 0.02
こんなのを STDIN から渡したら
15:40:01 0.01 **
15:50:01 0.04 ********
16:00:01 0.01 **
16:10:01 0.03 ******
16:20:01 0.01 **
16:30:01 0.01 **
16:40:01 0.01 **
16:50:01 0.01 **
17:00:01 0.02 ****
17:10:02 0.17 *************************************
17:20:01 0.15 *********************************
17:30:01 0.02 ****
こんな風に出力するやつ書いてみた
こんな使い方を想定してる。。。が、使わんかもなぁ
sar -u |awk '{print $1,$6}'|./txt_graph.lua
以下、ソースコード
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
#!/usr/bin/lua | |
maxnum = tonumber(arg[1]) or 40 | |
max, min = 0, 0 | |
lines = io.stdin:lines() | |
arrays = {} | |
for line in lines do | |
-- split | |
local tmps = {} | |
for c in string.gmatch(line,'[^%s]+') do | |
table.insert(tmps, c) | |
end | |
local num = tonumber(tmps[2]) | |
if #tmps == 2 and num then | |
table.insert(arrays, {tmps[1], num}) | |
if max < num then max = num end | |
if min > num then min = num end | |
end | |
end | |
for k,v in pairs(arrays) do | |
local num = math.floor(maxnum * v[2] / max) | |
print( string.format('%s\t%s\t%s', v[1], v[2], string.rep('*', num) ) ) | |
end |