-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.lua
83 lines (66 loc) · 1.54 KB
/
example.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
local ESC = "\27"
---@class el
---@field rows integer terminal rows
---@field cols integer terminal cols
---@field line_height integer status line height
---@vararg string
local function puts(...)
io.stdout:write(...)
end
local STATE = {
frame = 0,
timer = 0,
}
---
--- print status line
---
local function make_status()
STATE.frame = STATE.frame + 1
local str = ""
---@vararg string
local function append(...)
for _, v in ipairs { ... } do
str = str .. v
end
end
-- save state
append(ESC, "[s")
-- hide cursor
append(ESC, "[?25l")
local row = el.rows - el.line_height + 1 -- 1 origin
append(ESC, string.format("[%d;%dH", row, 1))
append(ESC, "[0m", string.format("hello status line ! %d: %d", STATE.frame, STATE.timer))
-- show cursor
append(ESC, "[?25h")
-- restore
append(ESC, "[u")
return str
end
---
--- called when rows & cols are updated by SIGWINCH
---
function el.update()
puts(ESC, "[s")
-- DECSTBM
puts(ESC, string.format("[%d;%dr", 1, el.rows - el.line_height))
puts(ESC, "[u")
end
---@return integer next_timer_ms
function el.timer()
STATE.timer = STATE.timer + 1
puts(make_status())
return 500
end
--- process input
---@param key_input string keyboard input from stdin
---@return string input_to_child_pty
function el.input(key_input)
return key_input
end
--- process output
---@param output string escape sequence from child pty output
---@return string output_to_stdout
function el.output(output)
-- TODO: track escape sequence state
return output .. make_status()
end