Repository navigation
Expand file tree
/
Copy pathutils.lua
More file actions
82 lines (69 loc) · 2.22 KB
/
Copy pathutils.lua
File metadata and controls
82 lines (69 loc) · 2.22 KB
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
local C = require('easy_dialog.constants')
local U = {}
local VALID_LAUNCH_MODE = {
[C.LaunchMode.STANDARD] = true,
[C.LaunchMode.ROOT] = true,
[C.LaunchMode.SINGLE_TOP] = true,
}
function U.warn(msg, ...)
print(('[easy-dialog] Warning: ' .. tostring(msg):format(...)))
end
function U.errorln(msg, ...)
print(('[easy-dialog] Error: ' .. tostring(msg)):format(...))
end
function U.safe_call(fn, ...)
if type(fn) ~= 'function' then return true, nil end
local ok, res = pcall(fn, ...)
if not ok then
U.errorln('in user callback: %s', tostring(res))
end
return ok, res
end
function U.truncate(text, limit, name)
text = tostring(text or '')
if #text > limit then
U.warn("'%s' exceeds limit of %d chars (got %d). Truncating.",
name or 'text', limit, #text)
return text:sub(1, limit)
end
return text
end
local function clamp_tab_cols(s)
local cols, n = {}, 0
for col in tostring(s):gmatch('[^\t]+') do
n = n + 1
cols[#cols + 1] = U.truncate(col, C.LIMITS.TAB_COL, 'Tablist column')
if n >= C.LIMITS.TAB_COLS then break end
end
return table.concat(cols, '\t')
end
function U.clamp_tablist_row(line)
return U.truncate(clamp_tab_cols(line), C.LIMITS.TAB_ROW, 'Tablist row')
end
function U.format_content(content_table, LIMIT_TOTAL)
if type(content_table) ~= 'table' then
return U.truncate(tostring(content_table or ''), LIMIT_TOTAL or C.LIMITS.TOTAL_TEXT, 'Dialog content')
end
local rows = {}
for _, item in ipairs(content_table) do
if type(item) == 'table' then
if item.text ~= nil then
rows[#rows + 1] = tostring(item.text)
else
local parts = {}
for _, cell in ipairs(item) do
parts[#parts + 1] = tostring(cell)
end
rows[#rows + 1] = table.concat(parts, '\t')
end
else
rows[#rows + 1] = tostring(item)
end
end
local text = table.concat(rows, '\n')
return U.truncate(text, LIMIT_TOTAL or C.LIMITS.TOTAL_TEXT, 'Dialog content')
end
function U.is_valid_launch_mode(mode)
return VALID_LAUNCH_MODE[mode] == true
end
return U