diff --git a/lua/pika/core/custom/terminalcmd.lua b/lua/pika/core/custom/terminalcmd.lua new file mode 100644 index 0000000..7d07e55 --- /dev/null +++ b/lua/pika/core/custom/terminalcmd.lua @@ -0,0 +1,52 @@ +-- ─< Set path to your shell functions file (update this to your actual path) >───────── +local shell_script_path = vim.fs.joinpath(vim.fn.stdpath("config") .. "/functions.sh") + +-- ─< Add this before the keymap definition to check for file existence >─────────────── +if vim.uv.fs_stat(shell_script_path) then -- Check if file exists + -- inserts shell commands (sources ~/.config/nvim/functions.sh to source predefined functions) + vim.keymap.set({ "n", "i" }, "tt", function() + vim.ui.input({ prompt = "Shell Command: " }, function(input_cmd) + if not input_cmd or input_cmd == "" then + return + end + + -- Construct the command to source your functions and execute + local full_cmd = string.format( + "source %s && %s", -- Uses POSIX-compliant '&&' to fail if source fails + vim.fn.shellescape(shell_script_path), + input_cmd + ) + + -- Execute in bash to ensure function availability + local result = vim.fn.system({ "bash", "--norc", "--noprofile", "-c", full_cmd }) + local exit_code = vim.v.shell_error + + -- Handle errors + if exit_code ~= 0 then + vim.notify( + "Error (" .. exit_code .. "):\n" .. result, + vim.log.levels.ERROR, + { title = "Command Failed" } + ) + return + end + + -- Process output + local lines = vim.split(result:gsub("\n$", ""), "\n") + + -- Insert at cursor position + local row, col = unpack(vim.api.nvim_win_get_cursor(0)) + if #lines == 1 then + vim.api.nvim_set_current_line( + vim.api.nvim_get_current_line():sub(1, col) + .. lines[1] + .. vim.api.nvim_get_current_line():sub(col + 1) + ) + else + vim.api.nvim_buf_set_lines(0, row, row, false, lines) + end + end) + end, { desc = "Insert Shell Output (with functions)" }) +else + vim.notify("Shell functions file not found: " .. shell_script_path, vim.log.levels.WARN) +end diff --git a/lua/pika/core/init.lua b/lua/pika/core/init.lua index 3ab7175..4e41d6e 100644 --- a/lua/pika/core/init.lua +++ b/lua/pika/core/init.lua @@ -1,2 +1,3 @@ require("pika.core.options") require("pika.core.keymaps") +require("pika.core.custom.terminalcmd") diff --git a/lua/pika/core/keymaps.lua b/lua/pika/core/keymaps.lua index f753647..57203c8 100644 --- a/lua/pika/core/keymaps.lua +++ b/lua/pika/core/keymaps.lua @@ -57,56 +57,3 @@ map("n", "R", [[:%s/\<\>//gI]]) -- window management map("n", "sv", "v", { desc = "Split window vertically" }) -- split window vertically map("n", "sh", "s", { desc = "Split window horizontally" }) -- split window horizontally - --- ─< Set path to your shell functions file (update this to your actual path) >───────── -local shell_script_path = vim.fs.joinpath(vim.fn.stdpath("config") .. "/functions.sh") - --- ─< Add this before the keymap definition to check for file existence >─────────────── -if vim.uv.fs_stat(shell_script_path) then -- Check if file exists - -- inserts shell commands (sources ~/.config/nvim/functions.sh to source predefined functions) - vim.keymap.set({ "n", "i" }, "tt", function() - vim.ui.input({ prompt = "Shell Command: " }, function(input_cmd) - if not input_cmd or input_cmd == "" then - return - end - - -- Construct the command to source your functions and execute - local full_cmd = string.format( - "source %s && %s", -- Uses POSIX-compliant '&&' to fail if source fails - vim.fn.shellescape(shell_script_path), - input_cmd - ) - - -- Execute in bash to ensure function availability - local result = vim.fn.system({ "bash", "--norc", "--noprofile", "-c", full_cmd }) - local exit_code = vim.v.shell_error - - -- Handle errors - if exit_code ~= 0 then - vim.notify( - "Error (" .. exit_code .. "):\n" .. result, - vim.log.levels.ERROR, - { title = "Command Failed" } - ) - return - end - - -- Process output - local lines = vim.split(result:gsub("\n$", ""), "\n") - - -- Insert at cursor position - local row, col = unpack(vim.api.nvim_win_get_cursor(0)) - if #lines == 1 then - vim.api.nvim_set_current_line( - vim.api.nvim_get_current_line():sub(1, col) - .. lines[1] - .. vim.api.nvim_get_current_line():sub(col + 1) - ) - else - vim.api.nvim_buf_set_lines(0, row, row, false, lines) - end - end) - end, { desc = "Insert Shell Output (with functions)" }) -else - vim.notify("Shell functions file not found: " .. shell_script_path, vim.log.levels.WARN) -end