-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib.lua
127 lines (111 loc) · 2.32 KB
/
lib.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
local love = require 'love'
local pathSep = '/'
if love.system.getOS() == "Windows" then
pathSep = '\\'
end
local _mod = {
pathSep = pathSep
}
---@param color table
---@return function
function _mod.SetColor(color)
local r, g, b = love.graphics.getColor()
love.graphics.setColor(color[1], color[2], color[3])
return function()
love.graphics.setColor(r, g, b)
end
end
---@param mode "fill" | "line"
---@param color table
---@param x number
---@param y number
---@param width number
---@param height number
function _mod.DrawRect(mode, color, x, y, width, height)
local revert = _mod.SetColor(color)
love.graphics.rectangle(mode, x, y, width, height)
revert()
end
---@param path string
---@return string
function _mod.ResolvePath(path)
if path[1] ~= pathSep then
path = os.getenv("PWD") .. pathSep .. path
end
return path
end
function _mod.FormatPath(path, truncate, hideUser)
if truncate then
local truncPath = ""
for i = #path, 1, -1 do
if path:sub(i, i) == pathSep then
break
end
truncPath = truncPath .. path:sub(i, i)
end
path = truncPath
else
if hideUser and path:sub(1, 1) == pathSep then
local sepCount = 0
while sepCount < 3 do
if path:sub(1, 1) == pathSep then
sepCount = sepCount + 1
end
path = path:sub(2)
end
path = "~" .. pathSep .. path
end
end
return path
end
---@param seq string | table
---@param item any
---@return boolean
function _mod.Contains(seq, item)
for i = 1, #seq do
if type(seq) == "string" then
if seq:sub(i, i) == item then
return true
end
else
if seq[i] then
return true
end
end
end
return false
end
---@param n number
---@param min number
---@param max number?
---@return number
function _mod.Clamp(n, min, max)
if max == nil then
max = n
end
if n < min then
return min
elseif n > max then
return max
end
return n
end
---@param s string
---@return number, number, string
function _mod.GetCurrentLine(s, pos)
local start, sEnd = 1, #s
for i = pos + 1, #s, 1 do
if s:sub(i, i) == '\n' then
sEnd = i
break
end
end
for i = pos - 1, 1, -1 do
if s:sub(i, i) == '\n' then
start = i + 1
break
end
end
return start, sEnd, s:sub(start, sEnd)
end
return _mod