Skip to content

Commit

Permalink
shortcut: toggleterm execute line
Browse files Browse the repository at this point in the history
  • Loading branch information
SirAppSec committed Sep 6, 2024
1 parent 1a66704 commit 740f5f4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lua/hacker-helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@ M.hello = function()
return module.my_first_function(M.config.opt)
end

-- Key mappings for executing in terminal
vim.api.nvim_set_keymap('n', '<leader>e', ':lua require("hacker-helper.module").exec_line_or_selection_in_term()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('v', '<leader>e', ':lua require("hacker-helper.module").exec_line_or_selection_in_term()<CR>',
{ noremap = true, silent = true })

return M
68 changes: 68 additions & 0 deletions lua/hacker-helper/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,73 @@ local M = {}
M.my_first_function = function(greeting)
return greeting
end
-- Initialize the toggleterm plugin if not already done
-- require('toggleterm').setup {
-- direction = 'vertical',
-- size = 50,
-- }

-- Function to execute the current line or visual selection in an existing terminal
function M.exec_line_or_selection_in_term()
-- Initialize the toggleterm plugin if not already done
local ok, toggleterm = pcall(require, 'toggleterm')
if ok then
toggleterm.setup {
direction = 'vertical',
size = 50,
}
else
print("toggleterm is not installed or cannot be loaded.")
return
end

local mode = vim.api.nvim_get_mode().mode
local lines = {}

if mode == 'v' then
-- Get the visually selected lines
local start_pos = vim.fn.getpos("'<")
local end_pos = vim.fn.getpos("'>")
lines = vim.fn.getline(start_pos[2], end_pos[2])
else
-- Get the current line
table.insert(lines, vim.fn.getline('.'))
end

-- Find the existing terminal buffer
local term_bufnr = nil
local buffers = vim.api.nvim_list_bufs()

for _, buf in ipairs(buffers) do
if vim.api.nvim_buf_get_option(buf, 'buftype') == 'terminal' then
term_bufnr = buf
break
end
end

if term_bufnr then
-- Switch to the terminal buffer
vim.api.nvim_set_current_buf(term_bufnr)

-- Safely check if the terminal job ID exists
local term_job_id = vim.b.terminal_job_id
if not term_job_id then
print("No terminal job ID found.")
return
end

-- Send the command to the terminal
for _, line in ipairs(lines) do
vim.api.nvim_chan_send(term_job_id, line .. "\n")
end

-- Switch back to the previous buffer
vim.cmd("b#")
else
print("No terminal buffer found.")
end
end

-- End of Function

return M
5 changes: 5 additions & 0 deletions plugin/hacker-helper.lua
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
vim.api.nvim_create_user_command("HackerHelper", require("hacker-helper").hello, {})
-- Key mappings for executing in terminal
vim.api.nvim_set_keymap('n', '<leader>e', ':lua require("hacker-helper.module").exec_line_or_selection_in_term()<CR>',
{ noremap = true, silent = true })
vim.api.nvim_set_keymap('v', '<leader>e', ':lua require("hacker-helper.module").exec_line_or_selection_in_term()<CR>',
{ noremap = true, silent = true })

0 comments on commit 740f5f4

Please sign in to comment.