graph.lua
local t = require "terminal"
local TimeSeriesGraph = require "terminal.ui.timeseriesgraph"
local HISTORY_SIZE = 100 local INTERVAL = 0.10 local GRAPH_COLS = 60 local GRAPH_ROWS = 15
local frame = 0
local graph = TimeSeriesGraph({
history = HISTORY_SIZE,
})
local function get_value()
frame = frame + 1
return 100 * math.sin(frame * INTERVAL * 0.8)
end
local function main()
t.cursor.visible.set(false)
t.output.print("Sine wave demo (press any key to quit)")
t.output.write(("\n"):rep(GRAPH_ROWS + 2))
t.cursor.position.up(GRAPH_ROWS + 2)
while true do
graph:push(get_value())
t.output.write(graph:render({
cols = GRAPH_COLS,
rows = GRAPH_ROWS,
fmt = "%g%%",
graph_attr = { fg = "black", bg = "yellow" },
label_attr = { fg = "red", bg = "white" },
}))
if t.input.readansi(INTERVAL) then
break
end
end
t.cursor.position.down(GRAPH_ROWS)
t.output.print("\nDone.")
t.cursor.visible.set(true)
end
t.initwrap(main)()