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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
return {
"MunifTanjim/nui.nvim",
config = function()
local COMMANDS = {}
local CURRENT_COMMAND = nil
local data_path = vim.fn.stdpath("data")
local PERSONAL_COMMAND_JSON_PATH = data_path ..
"/personal_commands.json"
---@param command string|nil
---@return nil
local executeCommandInTerminal = function(command)
if (command ~= nil) then
vim.cmd.term()
vim.cmd.startinsert()
local channel = vim.bo.channel
-- 'clear' is used to hide the command
local exitMessage = "\nPress enter to exit..."
local commandString = command()
vim.api.nvim_chan_send(channel,
"clear && " .. commandString ..
"; echo '" .. exitMessage ..
"' && read -s && exit" .. "\n")
end
end
local readJsonFromFile = function(filePath)
local file, error = io.open(filePath, 'r')
if file ~= nil then
local content = file:read("*a")
local decoded_json = vim.json.decode(content)
return decoded_json
else
print("Error in readJsonFromFile: " .. error)
end
return nil
end
local addCommandToJsonFile = function(commandBlock, commandsFilePath,
directory)
local current_json = readJsonFromFile(commandsFilePath)
if current_json ~= nil then
if current_json[directory] == nil then
current_json[directory] = {}
end
-- FIXME: duplicates
table.insert(current_json[directory], {
command = tostring(commandBlock.command()),
description = tostring(commandBlock.description())
})
end
local file, error = io.open(commandsFilePath, 'w')
if file then
local indent =
vim.bo.expandtab and (' '):rep(vim.o.shiftwidth) or '\t'
file:write(vim.json.encode(current_json, {indent = indent}))
file:close()
else
print("Error in writeTableToJsonFile: " .. error)
end
end
local updateJsonFile = function(listOfCommands, commandsFilePath,
directory)
local current_json = readJsonFromFile(commandsFilePath)
current_json[directory] = {}
for _, value in pairs(listOfCommands) do
table.insert(current_json[directory], {
command = tostring(value.command()),
description = tostring(value.description())
})
end
local file, error = io.open(commandsFilePath, 'w')
if file then
local indent =
vim.bo.expandtab and (' '):rep(vim.o.shiftwidth) or '\t'
file:write(vim.json.encode(current_json, {indent = indent}))
file:close()
else
print("Error in updateJsonFile: " .. error)
end
end
local appendCommandsFromFile = function(commands, commandsFilePath,
directory)
local current_list_of_commands_json = readJsonFromFile(
commandsFilePath)
if current_list_of_commands_json ~= nil and
current_list_of_commands_json[directory] ~= nil then
for _, block in pairs(current_list_of_commands_json[directory]) do
local new_command_name = block["description"]
commands[new_command_name] = {
command = function()
return block["command"]
end,
description = function()
return new_command_name
end
}
end
end
end
local selectCommand = function(listOfCommands, callback)
vim.ui.select(vim.tbl_keys(listOfCommands), {
prompt = "Select your command.",
format_item = function(item)
return listOfCommands[item].description()
end
}, function(choice)
if (choice ~= nil) then
CURRENT_COMMAND = listOfCommands[choice].command
callback(choice)
end
end)
end
local addCommand = function(current_command, commands,
personal_command_json_path)
local selectedCommand = nil
local Input = require("nui.input")
local inputDescription = Input({
position = "50%",
size = {width = 120},
border = {
style = "single",
text = {top = "[Enter description]", top_align = "center"}
},
win_options = {
winhighlight = "Normal:Normal,FloatBorder:Normal"
}
}, {
prompt = "> ",
default_value = "",
on_submit = function(value)
local selectedDescription = value
commands[selectedDescription] = {
command = function()
return selectedCommand
end,
description = function()
return selectedDescription
end
}
local created_command = commands[selectedDescription]
addCommandToJsonFile(created_command,
personal_command_json_path,
vim.fn.getcwd())
current_command = created_command.command
executeCommandInTerminal(current_command)
end
})
local input = Input({
position = "50%",
size = {width = 120},
border = {
style = "single",
text = {top = "[Add command]", top_align = "center"}
},
win_options = {
winhighlight = "Normal:Normal,FloatBorder:Normal"
}
}, {
prompt = "> ",
default_value = "",
on_submit = function(value)
selectedCommand = value
inputDescription:mount()
local insert = vim.api.nvim_replace_termcodes("i", true,
true, true)
vim.api.nvim_feedkeys(insert, "t", false)
inputDescription:map("n", "<Esc>", function()
inputDescription:unmount()
end, {noremap = true})
end
})
-- mount/open the component
input:mount()
input:map("n", "<Esc>", function() input:unmount() end,
{noremap = true})
end
local deleteCommand = function()
local previous_command = CURRENT_COMMAND
selectCommand(COMMANDS, function(choice)
local index = 0
for key, _ in pairs(COMMANDS) do
index = index + 1
if key == choice then
COMMANDS[key] = nil
updateJsonFile(COMMANDS, PERSONAL_COMMAND_JSON_PATH,
vim.fn.getcwd())
break
end
end
CURRENT_COMMAND = previous_command
end)
end
vim.api.nvim_create_user_command("AddCommand", function()
addCommand(CURRENT_COMMAND, COMMANDS, PERSONAL_COMMAND_JSON_PATH)
end, {})
vim.api.nvim_create_user_command("DeleteCommand", deleteCommand, {})
vim.api.nvim_create_autocmd({'BufEnter', 'BufWinEnter', 'DirChanged'}, {
callback = function()
COMMANDS = {}
local current_json =
readJsonFromFile(PERSONAL_COMMAND_JSON_PATH)
local current_path = vim.fn.getcwd()
if current_json ~= nil then
for existing_directory, _ in pairs(current_json) do
if (current_path == existing_directory or
string.find(current_path, existing_directory) == 1) then
appendCommandsFromFile(COMMANDS,
PERSONAL_COMMAND_JSON_PATH,
existing_directory)
end
end
end
end
})
vim.keymap.set("n", "mc", function()
if (CURRENT_COMMAND == nil) then
selectCommand(COMMANDS, function()
executeCommandInTerminal(CURRENT_COMMAND)
end)
else
executeCommandInTerminal(CURRENT_COMMAND)
end
end)
vim.keymap.set("n", "mC", function()
selectCommand(COMMANDS, function()
executeCommandInTerminal(CURRENT_COMMAND)
end)
end)
end
}
|