local CURRENT_COMMAND = nil local data_path = vim.fn.stdpath("data") ---@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(command, commandsFilePath, directory) local current_json = readJsonFromFile(commandsFilePath) current_json["hihi"] = 2 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 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 COMMANDS = {} PERSONAL_COMMAND_JSON_PATH = data_path .. "/personal_commands.json" appendCommandsFromFile(COMMANDS, PERSONAL_COMMAND_JSON_PATH, vim.fn.getcwd()) local personalCommands = require("aotrix.personal_commands") COMMANDS.AddCommand = { command = function() local created_command = personalCommands.createCommand(COMMANDS) addCommandToJsonFile(created_command, PERSONAL_COMMAND_JSON_PATH) CURRENT_COMMAND = created_command.command return CURRENT_COMMAND() end, description = function() return "Add command" 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() end end) end local executeCommand = function(listOfCommands) if (CURRENT_COMMAND == nil) then selectCommand(listOfCommands, function() executeCommandInTerminal(CURRENT_COMMAND) end) else executeCommandInTerminal(CURRENT_COMMAND) end end vim.keymap.set("n", "mc", function() executeCommand(COMMANDS) end) vim.keymap.set("n", "mC", function() selectCommand(COMMANDS, function() executeCommandInTerminal(CURRENT_COMMAND) end) end)