-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathui.lua
More file actions
133 lines (126 loc) · 3.75 KB
/
Copy pathui.lua
File metadata and controls
133 lines (126 loc) · 3.75 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
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
require('nis.message_window')
function silent_print(text)
local current_window = vis.win
vis:message(tostring(text))
if current_window ~= nil then vis.win = current_window end
end
function register_colors(window)
local holes = {}
local existant = {}
local lexer = vis.lexers.load("nim", nil, true)
for i = 0, 64, 1 do holes[i] = true end
for name, id in pairs(lexer._TOKENSTYLES) do
local style = vis.lexers['STYLE_'..name:upper()] or ""
window:style_define(id, style)
existant[style] = id
holes[id] = nil
end
return lexer, setmetatable(existant, {__index = function(t, k)
local free
for freeid in pairs(holes) do
free = freeid
break
end
if window:style_define(free, k) then
holes[free] = nil
t[k] = free
return free
else return 65 -- UI_STYLE_DEFAULT
end
end})
end
local function stylize(window, data)
local start, finish, style, lexer, existent
local result = data
if not window.existent then
lexer, existent = register_colors(window)
else
existent = window.existent
lexer = vis.lexers.load("nim", nil, true)
end
local styles = lexer._TOKENSTYLES
local paints = {}
local tokenstart = 0
local currenttoken = "reset"
repeat
start, finish, style = result:find("\\e%[([^%]]+)%]", start)
if start == nil then
start = #result
style = "reset"
else
result = result:sub(1, start-1)..result:sub(finish+1)
end
if currenttoken ~= "reset" then
if currenttoken == "syntax" then
--do syntax highlighting
local tokens = lexer:lex(result:sub(tokenstart,start), 1)
local tstart = tokenstart
for i = 1, #tokens, 2 do
local tend = tokenstart + tokens[i+1] - 1
if tend >= tstart then
local name = tokens[i]
local tstyle = styles[name]
if tstyle ~= nil then
table.insert(paints, {start = tstart-1, finish = tend-1,
style = tstyle})
end
end
tstart = tend
end
elseif tokenstart == start and style ~= "syntax" then
currenttoken = currenttoken..","..style
else
--do colorizing according the style
table.insert(paints, {start = tokenstart-1, finish = start-2,
style = existent[currenttoken]})
end
end
tokenstart = start
currenttoken = style
until start == #result
window.existent = existent
return result, paints
end
function stylized_print(notifier, text, append)
local lastwin = vis.win
notifier:show()
-- the order matters! stylize needs to have window that it is stylizing in
-- focus
local curwin = notifier.win
local cleantext, paints = stylize(curwin, text)
if append then
if not curwin.paints then curwin.paints = {} end
local shift = #notifier.text
for i, paint in pairs(paints) do
table.insert(curwin.paints, {
style = paint.style,
start = paint.start + shift,
finish = paint.finish + shift
})
end
else
curwin.paints = paints
end
notifier:setText(cleantext, append)
curwin.triggers.error_highlighter = function(win)
for _, task in pairs(win.paints) do
win:style(task.style, task.start, task.finish)
end
end
if lastwin and vis.win ~= lastwin then vis.win = lastwin end
end
local colors = require('nis.graphic').colors
function convertTermColors(line)
local replacer = function(a)
if a == "0" then return "\\e[reset]" end
for i, v in pairs(colors) do
if v.id == a then return "\\e["..v.description.."]" end
end
return ""
end
return line:gsub("%c", function (a)
local code = a:byte()
if code == 27 then return "{"..code.."}"
else return a end end)
:gsub("{27}%[([0-9]+)m", replacer)
end