-
Notifications
You must be signed in to change notification settings - Fork 0
/
exists.lua
206 lines (159 loc) · 4.3 KB
/
exists.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
require('user_api.types.user.check')
local Value = require('user_api.check.value')
local is_nil = Value.is_nil
local is_bool = Value.is_bool
local is_str = Value.is_str
local is_tbl = Value.is_tbl
local is_fun = Value.is_fun
local empty = Value.empty
local ERROR = vim.log.levels.ERROR
---@type User.Check.Existance
---@diagnostic disable-next-line:missing-fields
local M = {}
---@param mod string
---@param return_mod? boolean
---@return boolean|unknown|nil
function M.module(mod, return_mod)
if not is_str(mod) then
error('`(user_api.check.exists.module)`: Input is not a string')
end
if mod == '' then
error("`(user_api.check.exists.module)`: Input can't be an empty string")
end
return_mod = is_bool(return_mod) and return_mod or false
local res
if return_mod then
local m
res, m = pcall(require, mod)
return (res and not is_nil(m)) and m or nil
end
res = pcall(require, mod)
return res
end
---@param mod string|string[]
---@param need_all? boolean
---@return boolean|table<string, boolean>
function M.modules(mod, need_all)
local exists = M.module
if not (is_str(mod) or is_tbl(mod)) or empty(mod) then
error('`(user_api.check.exists.modules)`: Input is neither a string nor a table')
end
need_all = is_bool(need_all) and need_all or false
if is_str(mod) then
return not need_all and exists(mod) or { [mod] = exists(mod) }
end
---@type boolean|table<string, boolean>
local res = {}
for _, v in next, mod do
local r = exists(v)
if need_all then
res[v] = r
else
res = r
-- Break when a module is not found
if not res then
break
end
end
end
return res
end
---@param expr string|string[]
---@return boolean
function M.vim_has(expr)
if is_str(expr) then
return vim.fn.has(expr) == 1
end
if is_tbl(expr) and not empty(expr) then
for _, v in next, expr do
if not M.vim_has(v) then
return false
end
end
return true
end
return false
end
---@param expr string|string[]
---@return boolean
function M.vim_exists(expr)
local exists = vim.fn.exists
if is_str(expr) then
return exists(expr) == 1
end
if is_tbl(expr) and not empty(expr) then
local res = false
for _, v in next, expr do
res = M.vim_exists(v)
if not res then
break
end
end
return res
end
return false
end
---@param vars string|string[]
---@param fallback? fun()
---@return boolean
function M.env_vars(vars, fallback)
local environment = vim.fn.environ()
if not (is_str(vars) or is_tbl(vars)) then
vim.notify(
'(user_api.check.exists.env_vars): Argument type is neither string nor table',
ERROR
)
return false
end
fallback = is_fun(fallback) and fallback or nil
local res = false
if is_str(vars) then
res = vim.fn.has_key(environment, vars) == 1
elseif is_tbl(vars) then
for _, v in next, vars do
res = M.env_vars(v)
if not res then
break
end
end
end
if not res and is_fun(fallback) then
fallback()
end
return res
end
---@param exe string|string[]
---@param fallback? fun()
---@return boolean
function M.executable(exe, fallback)
if not (is_str(exe) or is_tbl(exe)) then
vim.notify(
'(user_api.check.exists.executable): Argument type is neither string nor table',
ERROR
)
return false
end
fallback = is_fun(fallback) and fallback or nil
local res = false
if is_str(exe) then
res = vim.fn.executable(exe) == 1
elseif is_tbl(exe) then
for _, v in next, exe do
res = M.executable(v)
if not res then
break
end
end
end
if not res and is_fun(fallback) then
fallback()
end
return res
end
---@param path string
---@return boolean
function M.vim_isdir(path)
return (is_str(path) and not empty(path)) and (vim.fn.isdirectory(path) == 1) or false
end
return M
--- vim:ts=4:sts=4:sw=4:et:ai:si:sta:noci:nopi: