-
Notifications
You must be signed in to change notification settings - Fork 0
/
terminal.lua
163 lines (144 loc) · 4.33 KB
/
terminal.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
---@alias uint8 integer
---@class Terminal
---@field write fun(self: Terminal, ...): self
---@field close fun(self: Terminal, ...): nil close the terminal before program exit to ensure it returns to the original state
---@field columns integer
---@field lines integer
local Terminal = setmetatable({
__name = "Terminal",
Black = 0,
Red = 1,
Green = 2,
Yellow = 3,
Blue = 4,
Magenta = 5,
Cyan = 6,
White = 7,
BrightBlack = 8,
BrightRed = 9,
BrightGreen = 10,
BrightYellow = 11,
BrightBlue = 12,
BrightMagenta = 13,
BrightCyan = 14,
BrightWhite = 15
}, {
---@param self Terminal
---@param file file*|{ write: function, close: function? }? defaults to stdout
---@return Terminal
__call = function(self, file)
file = file or io.stdout
file:write("\27[?1049h")
return setmetatable({
write = function(self, ...)
file:write(...)
return self
end,
close = function(self)
if not file then return end
self:showcursor()
local f = file --[[@as file*]]
file = nil
f:write("\27[?1049l\27[0m\n")
if f ~= io.stdout then f:close() end
end
}, self)
end
})
function Terminal:__close()
self:close()
end
function Terminal:__index(key)
if key == "columns" then
return math.tointeger(os.getenv("COLUMNS")) or 80
elseif key == "lines" then
return math.tointeger(os.getenv("LINES")) or 24
else
return Terminal[key]
end
end
---@param x integer
---@param y integer
function Terminal:position(x, y) return self:write("\27[", x, ";", y, "H") end
---@param r uint8 red 0-255
---@param g uint8 green 0-255
---@param b uint8 blue 0-255
---@return self
---@overload fun(self: Terminal, color: uint8): Terminal
function Terminal:bgcolor(r, g, b)
if g and b then
return self:write("\27[48;2;", r, ";", g, ";", b, "m")
else
return self:write("\27[48;5;", r, "m")
end
end
---@param r uint8 red 0-255
---@param g uint8 green 0-255
---@param b uint8 blue 0-255
---@return self
---@overload fun(self: Terminal, color: uint8): Terminal
function Terminal:color(r, g, b)
if g and b then
return self:write("\27[38;2;", r, ";", g, ";", b, "m")
else
return self:write("\27[38;5;", r, "m")
end
end
function Terminal:clearscreen() return self:write("\27[2J") end
---@param text string
---@param width integer
function Terminal:writewrapped(text, width)
local words = text:gmatch("%S+")
local word = words()
self:write(word)
local x = #word
for word in words do
if x + #word + 1 >= width then
self:back(x):down(1):write(word)
x = #word
else
self:write(" ", word)
x = x + #word + 1
end
end
return self
end
---@param width integer
---@param height integer
---@return Terminal
function Terminal:drawbox(width, height)
if width < 2 or height < 2 then return self end
self:write("╔", ("═"):rep(width - 2), "╗")
for _ = 2, height - 1 do
self:back(width):down(1):write("║"):forward(width - 2):write("║")
end
return self:back(width):down(1):write("╚", ("═"):rep(width - 2), "╝")
end
---@param char string
---@param width integer defaults to columns
---@param height integer defaults to lines
function Terminal:fillarea(char, width, height)
width = width or self.columns
height = height or self.lines
local line = char:rep(width)
if height >= 1 then
self:write(line)
end
for _ = 2, height do
self:back(width):down(1):write(line)
end
return self
end
function Terminal:hidecursor() return self:write("\27[?25l") end
function Terminal:showcursor() return self:write("\27[?25h") end
function Terminal:savecursor() return self:write("\27[s") end
function Terminal:restorecursor() return self:write("\27[u") end
---@param n integer?
function Terminal:up(n) return self:write("\27[", n or "", "A") end
---@param n integer?
function Terminal:down(n) return self:write("\27[", n or "", "B") end
---@param n integer?
function Terminal:forward(n) return self:write("\27[", n or "", "C") end
---@param n integer?
function Terminal:back(n) return self:write("\27[", n or "", "D") end
return Terminal