[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Output pagination
- From: Luiz Henrique de Figueiredo <lhf@...>
- Date: Wed, 19 Apr 2017 13:03:41 -0300
> When I'm reading a long text file with LUA, Can I use pagination for
> splitting output?
Try the code below. Adjust d to your needs.
Use q to quit, f to page forward, b to page backward, enter to see the next line
-- page.lua
-- usage: lua page.lua file
function page(file)
local f,err = io.open(file, "r")
if f == nil then
print(err)
return
end
local lines = {}
local n = 0
for line in f:lines() do
n = n + 1
lines[n] = line
end
os.execute("stty cbreak </dev/tty >/dev/tty 2>&1")
local b=1
local d=10
while true do
local e=math.min(n,b+d-1)
for i=b,e do
print(i,lines[i])
end
io.write(":"); io.flush()
local key = io.read(1)
io.write("\n"); io.flush()
if key == "q" then break end
if key == "b" then b=math.max(1,b-d) end
if key == "f" then b=math.min(n,b+d) end
if key == "\n" then b=math.min(n,b+1) end
end
os.execute("stty -cbreak </dev/tty >/dev/tty 2>&1");
end
page(arg[1])