addet functions.sh to source from when in terminal input mode
This commit is contained in:
parent
a0dfeda288
commit
0b4f569797
2 changed files with 51 additions and 25 deletions
3
functions.sh
Normal file
3
functions.sh
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
get_ip() {
|
||||||
|
ip a | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d/ -f1 | head -n 1
|
||||||
|
}
|
|
@ -58,32 +58,55 @@ map("n", "<leader>R", [[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]])
|
||||||
map("n", "<leader>sv", "<C-w>v", { desc = "Split window vertically" }) -- split window vertically
|
map("n", "<leader>sv", "<C-w>v", { desc = "Split window vertically" }) -- split window vertically
|
||||||
map("n", "<leader>sh", "<C-w>s", { desc = "Split window horizontally" }) -- split window horizontally
|
map("n", "<leader>sh", "<C-w>s", { desc = "Split window horizontally" }) -- split window horizontally
|
||||||
|
|
||||||
vim.keymap.set({ "n", "i" }, "<leader>tt", function()
|
-- ─< Set path to your shell functions file (update this to your actual path) >─────────
|
||||||
-- Frage den Shell-Command ab
|
local shell_script_path = vim.fs.joinpath(vim.fn.stdpath("config") .. "/functions.sh")
|
||||||
vim.ui.input({ prompt = "Shell Command: " }, function(cmd)
|
|
||||||
if cmd == nil or cmd == "" then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local result = vim.fn.system(cmd)
|
-- ─< Add this before the keymap definition to check for file existence >───────────────
|
||||||
local exit_code = vim.v.shell_error
|
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" }, "<leader>tt", function()
|
||||||
|
vim.ui.input({ prompt = "Shell Command: " }, function(input_cmd)
|
||||||
|
if not input_cmd or input_cmd == "" then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
if exit_code ~= 0 then
|
-- Construct the command to source your functions and execute
|
||||||
vim.notify("Shell Error:\n" .. result, vim.log.levels.ERROR, { title = "Shell Command Failed" })
|
local full_cmd = string.format(
|
||||||
return
|
"source %s && %s", -- Uses POSIX-compliant '&&' to fail if source fails
|
||||||
end
|
vim.fn.shellescape(shell_script_path),
|
||||||
|
input_cmd
|
||||||
|
)
|
||||||
|
|
||||||
local lines = vim.split(result, "\n", { trimempty = true })
|
-- 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
|
||||||
|
|
||||||
-- Insert at cursor position
|
-- Handle errors
|
||||||
local row, col = unpack(vim.api.nvim_win_get_cursor(0))
|
if exit_code ~= 0 then
|
||||||
if #lines == 1 then
|
vim.notify(
|
||||||
-- Insert inline if the result is a single line
|
"Error (" .. exit_code .. "):\n" .. result,
|
||||||
local current_line = vim.api.nvim_get_current_line()
|
vim.log.levels.ERROR,
|
||||||
local new_line = current_line:sub(1, col) .. lines[1] .. current_line:sub(col + 1)
|
{ title = "Command Failed" }
|
||||||
vim.api.nvim_set_current_line(new_line)
|
)
|
||||||
else
|
return
|
||||||
vim.api.nvim_buf_set_lines(0, row, row, false, lines)
|
end
|
||||||
end
|
|
||||||
end)
|
-- Process output
|
||||||
end, { desc = "Insert Shell Output at Cursor" })
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue