-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.lua
More file actions
131 lines (111 loc) · 2.53 KB
/
utils.lua
File metadata and controls
131 lines (111 loc) · 2.53 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
local micro = import("micro")
local utf8 = import("unicode/utf8")
local time = import("time")
local TICK_DURATION = time.ParseDuration("0ms")
local function toboolean(str)
if str == "true" then
return true
elseif str == "false" then
return false
else
return nil
end
end
local function xor(a, b)
return (a or b) and not (a and b)
end
local function swap(a, b)
return b, a
end
local function after(duration, fn)
-- micro.After requires micro v2.0.14-rc1
if type(micro.After) == "function" then
micro.After(duration, fn)
elseif
-- time.AfterFunc requires micro before v2.0.14-rc1
type(time.AfterFunc) == "function"
then
time.AfterFunc(duration, fn)
else
micro.TermMessage("** vi environment error **\ncannot find After* method")
end
end
local function next_tick(fn, n)
if not n or n < 2 then
after(TICK_DURATION, fn)
else
next_tick(fn, n - 1)
end
end
local function last_line_index(buf)
if not buf then
buf = micro.CurPane().Buf
end
local index = buf:LinesNum() - 1
local last_line = buf:Line(index)
local last_line_length = utf8.RuneCount(last_line)
if last_line_length < 1 then
index = math.max(index - 1, 0)
end
return index
end
local function utf8_index(line, x)
local str = line
local index = 0
for _ = 1, x do
local _, size = utf8.DecodeRuneInString(str)
str = str:sub(1 + size)
index = index + size
end
return index + 1
end
local function utf8_sub(line, from, to)
if not to then
to = utf8.RuneCount(line)
end
local str = line
local start_offset = 0
for _ = 1, from - 1 do
local _, size = utf8.DecodeRuneInString(str)
str = str:sub(1 + size)
start_offset = start_offset + size
end
local end_offset = start_offset
for _ = 1, to - from + 1 do
local _, size = utf8.DecodeRuneInString(str)
str = str:sub(1 + size)
end_offset = end_offset + size
end
return line:sub(1 + start_offset, end_offset)
end
local function is_locs_ordered(start_loc, end_loc)
if start_loc.Y < end_loc.Y then
return true
elseif start_loc.Y > end_loc.Y then
return false
else -- start_loc.Y == end_loc.Y
return start_loc.X <= end_loc.X
end
end
local function bytes_to_string(array)
local buf = {}
for i = 1, #array do
table.insert(buf, string.char(array[i]))
end
return table.concat(buf)
end
-------------
-- Exports --
-------------
local M = {}
M.toboolean = toboolean
M.xor = xor
M.swap = swap
M.after = after
M.next_tick = next_tick
M.last_line_index = last_line_index
M.utf8_index = utf8_index
M.utf8_sub = utf8_sub
M.is_locs_ordered = is_locs_ordered
M.bytes_to_string = bytes_to_string
return M