-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpaint.lua
More file actions
53 lines (47 loc) · 956 Bytes
/
paint.lua
File metadata and controls
53 lines (47 loc) · 956 Bytes
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
local function split_on_char (c, text)
local l = {}
if #text == 0 then
return l
end
-- pos: beginning of a segment
local pos = 1
while pos <= #text do
s = text:find(c, pos)
if s == nil then
s = #text + 1
end
l[#l+1] = text:sub(pos, s-1)
pos = s + 1
end
if s == #text then
l[#l+1] = ''
end
return l
end
local function paint_rectangle (scr, l, c, rect)
for i, line in ipairs(split_on_char('\n', rect)) do
scr:mvaddstr(l+i-1, c, line)
end
end
local function rectangle_dim (rect)
local width = 0
local lines = split_on_char('\n', rect)
for _, line in ipairs(lines) do
if width < #line then
width = #line
end
end
return width, #lines
end
local function paint_rectangles (scr, l, c, rects, space)
local width
for _, rect in ipairs(rects) do
paint_rectangle(scr, l, c, rect)
width = rectangle_dim(rect)
c = c + width + space
end
end
return {
rectangles = paint_rectangles,
dim = rectangle_dim,
}