-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcmd.lua
More file actions
161 lines (133 loc) · 3.96 KB
/
Copy pathcmd.lua
File metadata and controls
161 lines (133 loc) · 3.96 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
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
--- @class distant.core.builder.CmdBuilder
--- @field private __cmd string
--- @field private __internal table<string, boolean|string>
--- @field private __tail? string
--- @field private __allowed? table<string, boolean>
local M = {}
M.__index = M
--- Creates a new instance of cmd.
---
--- If `opts` provided, `opts.allowed` will restrict the `set` function to only operate for those in the allowed list.
---
--- @param cmd string|string[]
--- @param opts? {allowed?:string[]}
--- @return distant.core.builder.CmdBuilder
function M:new(cmd, opts)
opts = opts or {}
local instance = {}
setmetatable(instance, M)
instance.__internal = {}
instance.__tail = nil
if type(cmd) == 'table' then
instance.__cmd = table.concat(cmd, ' ')
else
instance.__cmd = cmd
end
if vim.islist(opts.allowed) then
instance.__allowed = {}
for _, key in ipairs(opts.allowed) do
instance.__allowed[key] = true
end
end
return instance
end
--- Sets multiple arguments using the given table.
--- @param tbl table<string, boolean|string>
--- @return distant.core.builder.CmdBuilder #reference to self
function M:set_from_tbl(tbl)
for key, value in pairs(tbl) do
if value then
self:set(key, value)
end
end
return self
end
--- Sets an argument.
--- @param key string #the key to add
--- @param value? boolean|string #optional value for argument
--- @param verbatim? boolean #if true, does not transform key casing and uses as is
--- @return distant.core.builder.CmdBuilder #reference to self
function M:set(key, value, verbatim)
if not key then
return self
end
local key_label = key
if not verbatim then
key_label = key:gsub('_', '-')
end
-- Ignore if we have an allowlist and this key is not in it
if self.__allowed and not self.__allowed[key_label] then
return self
end
self.__internal[key_label] = true
-- If value is truthy and not "true" itself, we assign a value
if value and type(value) ~= 'boolean' then
self.__internal[key_label] = tostring(value)
end
return self
end
--- Sets the tail of the command, which equates to `{cmd} -- {tail}`.
--- @param value string|nil #the tail, if nil then clears the tail
--- @return distant.core.builder.CmdBuilder #reference to self
function M:set_tail(value)
self.__tail = value
return self
end
--- Checks if the command has a `key` specified.
--- @param key string
--- @return boolean
function M:has(key)
return self.__internal[key] ~= nil
end
--- Removes an argument.
--- @param key string #the key to add
--- @param verbatim? boolean #if true, does not transform key casing and uses as is
--- @return distant.core.builder.CmdBuilder #reference to self
function M:clear(key, verbatim)
if not key then
return self
end
local key_label = key
if not verbatim then
key_label = key:gsub('_', '-')
end
self.__internal[key_label] = nil
return self
end
--- Clears the tail of the command.
--- @return distant.core.builder.CmdBuilder #reference to self
function M:clear_tail()
self.__tail = nil
return self
end
--- Converts cmd into a list of string.
--- @return string[]
function M:as_list()
local lst = {}
-- Break up cmd by whitespace and add each piece individually
for _, arg in ipairs(vim.split(self.__cmd, ' ', { plain = true, trimempty = true })) do
table.insert(lst, arg)
end
for k, v in pairs(self.__internal) do
table.insert(lst, '--' .. k)
if type(v) == 'string' then
table.insert(lst, v)
end
end
if self.__tail then
table.insert(lst, '--')
table.insert(lst, self.__tail)
end
return lst
end
--- Returns cmd as a string.
--- @return string
function M:as_string()
return vim.trim(table.concat(self:as_list(), ' '))
end
--- Returns cmd as a string.
--- @return string
function M:__tostring()
return self:as_string()
end
return M