summaryrefslogtreecommitdiff
path: root/lua/aotrix/build.lua
blob: ffd0bcd5ccadbef55178f1378733a01f9edb1902 (plain)
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
local CURRENT_COMMAND = nil
local data_path = vim.fn.stdpath("data")

---@param command string|nil
---@return nil
function executeCommandInTerminal(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

function writeJsonToFile(json, filePath)
    local file, error = io.open(filePath, 'r')
    local content = file:read("*a")
    if file ~= nil then
        local decoded_json = vim.json.decode(content)
        print(decoded_json["first_element"])
    end
    -- local file, error = io.open(filePath, 'w')
    -- if file then
    --     file:write(tostring(json))
    --     file:close()
    -- else
    --     print("error:", error)
    -- end
end

function appendCommandsFromDirectory(commands, directory)
    encoded_json = vim.json.encode({first_element = 1, second_element = 2})
    personal_commands_path = string.format("%s/personal_commands.json",
                                           data_path)
    writeJsonToFile(encoded_json, personal_commands_path)
end

local COMMANDS = {}
appendCommandsFromDirectory(COMMANDS, 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
}

function selectCommand(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

function executeCommand(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)