diff options
| author | Aotrix <[email protected]> | 2026-06-21 17:12:31 +0200 |
|---|---|---|
| committer | Aotrix <[email protected]> | 2026-06-21 17:12:31 +0200 |
| commit | fd601e49e0abaa1d56247f9ce3a7d97ea63d54ee (patch) | |
| tree | e5111aadd9084e3799cec9486a87e5826e72f4b6 /lua | |
| parent | b45495e1683223046df881e93de4a86ef23dc16d (diff) | |
Retrieve commands from given json file
Diffstat (limited to 'lua')
| -rw-r--r-- | lua/aotrix/build.lua | 53 |
1 files changed, 31 insertions, 22 deletions
diff --git a/lua/aotrix/build.lua b/lua/aotrix/build.lua index 9fcbed9..d1b10a6 100644 --- a/lua/aotrix/build.lua +++ b/lua/aotrix/build.lua @@ -3,7 +3,7 @@ local data_path = vim.fn.stdpath("data") ---@param command string|nil ---@return nil -function executeCommandInTerminal(command) +local executeCommandInTerminal = function(command) if (command ~= nil) then vim.cmd.term() vim.cmd.startinsert() @@ -17,48 +17,57 @@ function executeCommandInTerminal(command) end end -function readJsonFromFile(filePath) +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 -function writeJsonToFile(json, filePath) - -- local file, error = io.open(filePath, 'w') - -- if file then - -- file:write(tostring(json)) - -- file:close() - -- else - -- print("error:", error) - -- end +local writeTableToJsonFile = function(table, filePath) + local file, error = io.open(filePath, 'w') + if file then + file:write(vim.json.encode(table)) + file:close() + else + print("Error in writeTableToJsonFile: " .. error) + end end -function appendCommandsFromDirectory(commands, directory) - local encoded_json = vim.json - .encode({first_element = 1, second_element = 2}) - local personal_commands_path = string.format("%s/personal_commands.json", - data_path) - readJsonFromFile(personal_commands_path) - -- print(readJsonFromFile(personal_commands_path)["first_element"]) - writeJsonToFile(encoded_json, personal_commands_path) +local appendCommandsFromFile = function(commands, commandsFilePath, directory) + local personal_commands_path = commandsFilePath + local current_list_of_commands_json = readJsonFromFile( + personal_commands_path) + 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 COMMANDS = {} -appendCommandsFromDirectory(COMMANDS, vim.fn.getcwd()) +appendCommandsFromFile(COMMANDS, data_path .. "/personal_commands.json", + vim.fn.getcwd()) local personalCommands = require("aotrix.personal_commands") COMMANDS.AddCommand = { command = function() CURRENT_COMMAND = personalCommands.createCommand(COMMANDS).command return CURRENT_COMMAND() end, - description = function() return "Add temporary command" end + description = function() return "Add command" end } -function selectCommand(listOfCommands, callback) +local selectCommand = function(listOfCommands, callback) vim.ui.select(vim.tbl_keys(listOfCommands), { prompt = "Select your command.", format_item = function(item) @@ -72,7 +81,7 @@ function selectCommand(listOfCommands, callback) end) end -function executeCommand(listOfCommands) +local executeCommand = function(listOfCommands) if (CURRENT_COMMAND == nil) then selectCommand(listOfCommands, function() |
