-
Notifications
You must be signed in to change notification settings - Fork 2
/
ui.lua
341 lines (302 loc) · 9.5 KB
/
ui.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
local ui = safeinit(...)
local Gs = require "state"
local buffers = require "buffers"
local i18n = require "i18n"
local irc = require "irc"
local util = require "util"
-- Prints an IRC command.
function ui.printcmd(ent, current)
local args = irc.parsecmd(ent.line)
local from = args.user
local cmd = string.upper(args[1])
local to = args[2] -- not always valid!
local fmt = ui.ircformat
local hi = ui.highlight
local clock = os.date(config.timefmt, ent.ts)
if config.color.clock then
-- wrap the clock in ANSI color codes iff the user specified a color
clock = string.format("\x1b[%sm%s\x1b[0m", config.color.clock, clock)
end
-- TODO possibly nochan should be the default
local prefix_nochan = clock
if ent.urgency and ent.urgency >= 1 and config.bell and current then
prefix_nochan = "\x07" .. prefix_nochan
end
local prefix = prefix_nochan
if ent.buf and not buffers:is_visible(ent.buf) then
prefix = string.format("%s%s: ", prefix_nochan, ent.buf)
end
if cmd == "PRIVMSG" or cmd == "NOTICE" then
local action = false
local private = false
local notice = cmd == "NOTICE"
local margin = config.margin
local userpart
local msg = args[3]
if not irc.is_channel(to) then
private = true
end
if args.ctcp and args.ctcp.cmd == "ACTION" then
action = true
msg = args.ctcp.params
end
msg = fmt(msg)
-- highlight own nick
msg = string.gsub(msg, util.nick_pattern(Gs.user), hi(Gs.user, "mention1"))
if private then
prefix = prefix_nochan
if notice then
userpart = string.format("-%s:%s-", hi(from), hi(to))
elseif action then
userpart = string.format("[%s -> %s] * %s", hi(from), hi(to), hi(from))
else
userpart = string.format("[%s -> %s]", hi(from), hi(to))
end
else
local style = "nick" -- how to render the sender's nick
if (ent.urgency or 0) >= 1 then
style = "mention2"
end
if notice then
userpart = string.format("-%s:%s-", hi(from, style), to)
elseif action then
if not margin then
userpart = string.format("* %s", hi(from, style))
else
userpart = string.rep(" ", margin-1) .. "* |"
msg = hi(from) .. " " .. msg
end
else
if not margin then
userpart = string.format("<%s>", hi(from, style))
else
local u, l = util.visub(hi(from, style), 0, margin)
if l > margin then -- nick clipped
userpart = string.format("%s+|", u)
else
local pad = string.rep(" ", margin-l)
userpart = string.format("%s%s |", pad, u)
end
end
end
end
print(prefix .. userpart .. " " .. msg)
if private
and not notice
and from ~= Gs.user
and not string.match(to, Gs.prefix_pat)
then
ui.hint(i18n.hint.query, from)
end
elseif cmd == "JOIN" then
printf("%s--> %s has joined %s", prefix_nochan, hi(from), to)
elseif cmd == "PART" then
printf("%s<-- %s has left %s", prefix_nochan, hi(from), to)
elseif cmd == "KICK" then
printf("%s-- %s kicked %s from %s (%s)", prefix_nochan, hi(from), hi(args[3]), args[2], args[4] or "")
elseif cmd == "INVITE" then
printf("%s%s has invited you to %s", prefix_nochan, hi(from), args[3])
elseif cmd == "QUIT" then
printf("%s<-- %s has quit (%s)", prefix, hi(from), fmt(args[2]))
elseif cmd == "NICK" then
printf("%s%s is now known as %s", prefix, hi(from), hi(to))
elseif cmd == irc.RPL_TOPIC then
-- Sent when joining a channel
printf([[%s-- %s's topic is "%s"]], prefix_nochan, args[3], fmt(args[4]))
elseif cmd == "TOPIC" then
printf(
[[%s%s changed %s's topic from "%s" to "%s"]],
prefix_nochan,
hi(from),
to,
ent.oldtopic or "",
fmt(args[3])
)
elseif cmd == irc.RPL_LIST then
if ext.reason == "list" then
prefix = "" -- don't include the hour
else
prefix = prefix_nochan
end
if args[5] ~= "" then
printf([[%s%s, %s users, %s]], prefix, args[3], args[4], fmt(args[5]))
else
printf([[%s%s, %s users]], prefix, args[3], args[4])
end
elseif cmd == irc.RPL_LISTEND then
printf(i18n.list_after)
ext.eof()
else
printf([[error in hewwo: printcmd can't handle "%s"]], cmd)
end
end
function ui.updateprompt()
local chan = Gs.chan or "nowhere"
local unread, mentions = buffers:count_unread()
capi.setprompt(string.format("[%d!%d %s]: ", unread, mentions, chan))
end
--- A nick hashing function compatible with Weechat's default.
local function weehash(str, mod)
-- the djb2 variant used by Weechat for hashing nicks
local h = 5381|0 -- force int
for _,c in utf8.codes(str) do
-- yes, the hash iterates over utf8 codepoints and not bytes.
h = h ~ ((h << 5) + (h >> 2) + c)
end
if mod then
-- Weechat calculates the modulo on an uint64, but Lua's integers are
-- signed, so I have to emulate unsigned modulo.
if h < 0 then
h = h & 0x7FFFFFFFFFFFFFFF
-- to make this correct, we need to add (1<<63 % m)
-- 1<<63 % m = (2 * (1<<62 % m)) % m
-- (2 * (1<<62 % m)) < 2 * m
h = h - 2 * mod -- prevent overflow when adding the remainder
h = h + (2 * ((1<<62) % mod))
end
h = h % mod
end
return h
end
function ui.highlight(s, what)
if not s then return "" end
local mods = {}
local colors = config.color.nicks
what = what or "nick"
if what == "nick" or what == "mention1" or what == "mention2" then
-- it's a person!
if colors and #colors > 0 then
local cid = weehash(s, #colors)
table.insert(mods, colors[cid+1])
end
if what == "mention1" and (config.invert_mentions&1 == 1) then
-- mention in the middle of a message
table.insert(mods, "7")
end
if what == "mention2" and (config.invert_mentions&2 == 2) then
-- the nick of the sender
table.insert(mods, "7")
end
else
-- it's a bug!
error(string.format("unrecognized highlight type %q", what))
end
if #mods == 0 then
return s
else
return "\x1b["..table.concat(mods, ";").."m"..s.."\x1b[m"
end
end
-- this mess isn't even correct. or at least it doesn't match up weechat's
-- settings quite right. 16+ should be correct, though.
local colormap = {
[ 0] = 7, [ 1] = 0, [ 2] = 4, [ 3] = 2, [ 4] = 9, [ 5] = 3,
[ 6] = 5, [ 7] = 202, [ 8] = 11, [ 9] = 10, [10] = 6, [11] = 14,
[12] = 12, [13] = 13, [14] = 8, [15] = 7, [16] = 52, [17] = 94,
[18] = 100, [19] = 58, [20] = 22, [21] = 29, [22] = 23, [23] = 24,
[24] = 17, [25] = 54, [26] = 53, [27] = 89, [28] = 88, [29] = 130,
[30] = 142, [31] = 64, [32] = 28, [33] = 35, [34] = 30, [35] = 25,
[36] = 18, [37] = 91, [38] = 90, [39] = 125, [40] = 124, [41] = 166,
[42] = 184, [43] = 106, [44] = 34, [45] = 49, [46] = 37, [47] = 33,
[48] = 19, [49] = 129, [50] = 127, [51] = 161, [52] = 196, [53] = 208,
[54] = 226, [55] = 154, [56] = 46, [57] = 86, [58] = 51, [59] = 75,
[60] = 21, [61] = 171, [62] = 201, [63] = 198, [64] = 203, [65] = 215,
[66] = 227, [67] = 191, [68] = 83, [69] = 122, [70] = 87, [71] = 111,
[72] = 63, [73] = 177, [74] = 207, [75] = 205, [76] = 217, [77] = 223,
[78] = 229, [79] = 193, [80] = 157, [81] = 158, [82] = 159, [83] = 153,
[84] = 147, [85] = 183, [86] = 219, [87] = 212, [88] = 16, [89] = 233,
[90] = 235, [91] = 237, [92] = 239, [93] = 241, [94] = 244, [95] = 247,
[96] = 250, [97] = 254, [98] = 231,
}
-- format irc messages for display, escaping unknown characters
-- https://modern.ircdocs.horse/formatting.html
function ui.ircformat(s)
-- DON'T USE \x1b[0m unless you're absolutely sure. check the correct code
-- see tsetattr in http://git.suckless.org/st/file/st.c.html
local function t(cur, enable, disable) -- toggle
if cur then
return false, disable
else
return true, enable
end
end
local function color(fg, bg)
if not config.color.in_messages then
return ""
end
local function get_fg(fg)
if not fg then return "" end
fg = tonumber(fg)
if fg == 99 then
return "\x1b[39m" -- reset
else
return string.format("\x1b[38;5;%sm", colormap[fg])
end
end
local function get_bg(bg)
if not bg then return "" end
bg = tonumber(bg)
if bg == 99 then
return "\x1b[49m" -- reset
else
return string.format("\x1b[48;5;%sm", colormap[bg])
end
end
return get_fg(fg) .. get_bg(bg)
end
local bold, italic, underline, reverse
s = string.gsub(s or "", "[\x00-\x1F\x7F]", function (c)
local r -- replacement
if c == "\x02" then
bold, r = t(bold, "\x1b[1m", "\x1b[22m")
elseif c == "\x03" then
-- color handling is a special beast. leave it for later
return c
elseif c == "\x1d" then
italic, r = t(italic, "\x1b[3m", "\x1b[23m")
elseif c == "\x1f" then
underline, r = t(underline, "\x1b[4m", "\x1b[24m")
elseif c == "\x16" then
reverse, r = t(reverse, "\x1b[7m", "\x1b[27m")
elseif c == "\x0F" then
r = "\x1b[m"
bold = false
italic = false
underline = false
reverse = false
end
return r or ui.escape_char(c)
end)
s = string.gsub(s, "\x03([0-9][0-9]?),([0-9][0-9]?)", color)
s = string.gsub(s, "\x03([0-9][0-9]?)", color)
s = string.gsub(s, "\x03", "\x1b[39m\x1b[49m")
if string.find(s, "\x1b") then s = s.."\x1b[m" end -- reset if needed
return s
end
-- https://en.wikipedia.org/wiki/Caret_notation
-- meant to be used as an argument to string.gsub
function ui.escape_char(c)
local b = string.byte(c)
if b < 0x20 or b == 0x7F then
return "\x1b[7m^"..string.char(b ~ 64).."\x1b[27m"
end
return c
end
-- escape non-utf8 chars
function ui.escape(s)
s = string.gsub(s, "[\x00-\x1F\x7F]", ui.escape_char)
return s
end
function ui.strip_ansi(s)
return string.gsub(s, "\x1b%[[^\x40-\x7E]*[\x40-\x7E]", "")
end
function ui.hint(s, ...)
if not Gs.used_hints[s] then
printf(s, ...)
Gs.used_hints[s] = true
end
end
function ui.hint_silence(s)
Gs.used_hints[s] = true
end
return ui