-
Notifications
You must be signed in to change notification settings - Fork 0
/
sview.lua
59 lines (51 loc) · 1.44 KB
/
sview.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
local sdoc = require "libs.sdoc"
local args = { ... }
if #args < 1 then
print("sview <fn>")
return
end
local f = assert(fs.open(args[1], "r"))
local str = f.readAll()
f.close()
local document = sdoc.decode(sdoc.decode(str):remove(5, 6))
local blit = sdoc.render(document)
local page = 1
local win = window.create(term.current(), 1, 1, term.getSize())
local function centerWrite(y, s)
local w, h = win.getSize()
win.setCursorPos(math.floor((w - #s) / 2), y)
win.write(s)
end
local function draw()
local w, h = win.getSize()
win.setVisible(false)
win.clear()
if document.editable.title then
centerWrite(1, document.editable.title)
end
centerWrite(h, ("Page %d of %d"):format(page, #document.pages))
win.setCursorPos(1, h)
win.write("(q)Quit")
sdoc.blitOn(blit, page, nil, nil, win)
win.setVisible(true)
end
local running = true
while running do
draw()
local e, key = os.pullEvent()
if e == "key" then
if key == keys.up then
page = math.max(1, page - 1)
elseif key == keys.down then
page = math.min(#document.pages, page + 1)
end
elseif e == "char" and key == "q" then
term.clear()
term.setCursorPos(1, 1)
running = false
elseif e == "mouse_scroll" then
page = math.max(1, math.min(#document.pages, page + key))
elseif e == "term_resize" then
win.reposition(1, 1, term.getSize())
end
end