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 1612da1
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 5 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,14 @@ A few Snippets to help hacking


```
{ "SirAppsec/hacker-helper.nvim", lazy = true }
{
"SirAppsec/hacker-helper.nvim",
opts = {
keys = {
run_exec = "<leader>re", -- Run: Execute Command from Line Selection
}
}
}
```


2 changes: 1 addition & 1 deletion doc/hacker-helper-docs.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*my-template-docs.txt* For Neovim >= 0.8.0 Last change: 2024 January 18
*hacker-helper-docs.txt* For Neovim >= 0.8.0 Last change: 2024 Sptember 06

==============================================================================
Table of Contents *my-template-docs-table-of-contents*
Expand Down
24 changes: 21 additions & 3 deletions lua/hacker-helper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ local module = require("hacker-helper.module")

---@class Config
---@field opt string Your config option
---@field keys table<string, string> Key mappings
local config = {
prefix = "<leader>r", -- Default prefix for Hacker Helper
keys = {
run_exec = "e", -- Default mapping for executing in terminal
},
opt = "Hello!",
}

Expand All @@ -13,11 +18,24 @@ local M = {}
---@type Config
M.config = config

---@param args Config?
-- you can define your setup function here. Usually configurations can be merged, accepting outside params and
-- you can also put some validation here for those.
M.setup = function(args)
M.config = vim.tbl_deep_extend("force", M.config, args or {})
---@param user_config Config? User-provided configuration
M.setup = function(user_config)
-- Merge user configuration with defaults
M.config = vim.tbl_deep_extend("force", M.config, user_config or {})

-- Ensure no extra spaces in the prefix and key
local full_run_exec_mapping = vim.trim(M.config.prefix) .. vim.trim(M.config.keys.run_exec)
-- Prefix groupname
vim.keymap.set("n", M.config.prefix, function() end, { noremap = true, silent = true, desc = "Hacker Helper" })
-- Set key mappings using vim.keymap.set, including the description
vim.keymap.set("n", full_run_exec_mapping, function()
module.exec_line_or_selection_in_term()
end, { noremap = true, silent = true, desc = "Execute Command" })
vim.keymap.set("v", full_run_exec_mapping, function()
module.exec_line_or_selection_in_term()
end, { noremap = true, silent = true, desc = "Execute Command" })
end

M.hello = function()
Expand Down
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
7 changes: 7 additions & 0 deletions plugin/hacker-helper.lua
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
vim.api.nvim_create_user_command("HackerHelper", require("hacker-helper").hello, {})
-- Key mappings for executing in terminal

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

0 comments on commit 1612da1

Please sign in to comment.