Skip to content

Instantly share code, notes, and snippets.

@higemaru
Created August 1, 2013 13:52
Show Gist options
  • Save higemaru/6131554 to your computer and use it in GitHub Desktop.
Save higemaru/6131554 to your computer and use it in GitHub Desktop.
CUI bar graph sample
#!/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
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
num = math.floor(maxnum * v[2] / max)
print( string.format('%s\t%s\t%s', v[1], v[2], string.rep('*', num) ) )
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment