-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.lua
150 lines (136 loc) · 3.59 KB
/
util.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
local util = safeinit(...)
local i18n = require "i18n"
local tests = require "tests"
-- for mentions
function util.nick_pattern(nick)
-- TODO only works with alphanumeric nicks
return "%f[%w]"..nick.."%f[^%w]"
end
function util.file_exists(path)
local f = io.open(path)
if f then
f:close()
return true
else
return false
end
end
-- Takes a character-wise substring, preserving any original formatting.
function util.visub(str, from, to)
if from ~= 0 then error("unimplemented") end
local i = 1
local len = string.len(str)
local res = {} -- table.concat is supposedly the best way to build strings
local vlen = 0
local utf8p = "("..utf8.charpattern..")"
while i <= len do
local a, b, match = string.find(str, "^(\x1b%[[^\x40-\x7E]*[\x40-\x7E])", i)
if a then
i = b+1
table.insert(res, match)
else
a, b, match = string.find(str, utf8p, i)
if not a then break end
-- if a ~= i, whatever, pretend everything is fine anyways
i = b+1
if vlen < to then
table.insert(res, match)
end
vlen = vlen + 1
end
end
return table.concat(res), vlen
end
tests.run(function(t)
local vs = util.visub
t(vs("\x1b[3m12345678\x1b[m", 0, 4), "\x1b[3m1234\x1b[m")
t(vs("\x1b[3mąęźć\x1b[m", 0, 2), "\x1b[3mąę\x1b[m")
t(vs("\x1b[3ma\x1b[mb\x1b[3mc\x1b[m", 0, 2), "\x1b[3ma\x1b[mb\x1b[3m\x1b[m")
end)
-- max_args doesn't include the command itself
-- see below for examples
function util.parsecmd(line, max_args, pipe)
-- line = string.gsub(line, "^ *", "") guaranteed to start with /
local args = {}
local pos = 1
if pipe then
local split = string.find(line, "|")
if split then
args.pipe = string.sub(line, split+1)
line = string.sub(line, 1, split-1)
end
end
line = string.gsub(line, " *$", "")
while true do
local ws, we = string.find(line, " +", pos)
if ws and (not max_args or #args < max_args) then
table.insert(args, string.sub(line, pos, ws-1))
pos = we + 1
else
table.insert(args, string.sub(line, pos))
break
end
end
args[0] = string.gsub(args[1], "^/", "")
table.remove(args, 1)
return args
end
tests.run(function(t)
t(util.parsecmd("/q dzwdz blah blah"), {[0]="q", "dzwdz", "blah", "blah"})
t(util.parsecmd("/q dzwdz blah blah", 2), {[0]="q", "dzwdz", "blah blah"})
t(util.parsecmd("/q dzwdz blah blah", 2), {[0]="q", "dzwdz", "blah blah"})
t(util.parsecmd("/list | less"),
{[0]="list", "|", "less"})
t(util.parsecmd("/list | less", nil, true),
{[0]="list", ["pipe"]=" less"})
end)
function util.config_load()
package.loaded.config = nil
package.loaded.config_default = nil
config = {}
config.ident = {}
config.color = {}
config.commands = {}
require "config_default"
local succ, res = xpcall(require, debug.traceback, "config")
if not succ then
print(res)
print(i18n.err_config)
end
end
-- increment a number at the end of a nick. used when first joining the server
-- to find a free nick before timing out
function util.nicksucc(s)
local match
s, match = string.gsub(s, "%d+$", function(s)
return tostring(tonumber(s, 10) + 1)
end)
if match == 1 then
return s
else
return s.."1"
end
end
tests.run(function(t)
local ns = util.nicksucc
t(ns("dzwdz"), "dzwdz1")
t(ns("dzwdz1"), "dzwdz2")
t(ns("dzwdz2"), "dzwdz3")
t(ns("dzwdz9"), "dzwdz10")
t(ns("dzwdz10"), "dzwdz11")
end)
function util.patescape(s)
return string.gsub(s, "[%^%$%(%)%%%.%[%]%*%+%-%?]", function(s)
return "%"..s
end)
end
tests.run(function(t)
t(util.patescape("^$()%.[]*+-?"), "%^%$%(%)%%%.%[%]%*%+%-%?")
t(util.patescape("oh?"), "oh%?")
end)
function util.proxycmp(proxy)
return function(a, b)
return proxy(a) < proxy(b)
end
end
return util