diff --git a/README.md b/README.md index cedde0b..49e210e 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,14 @@ A few Snippets to help hacking ``` -{ "SirAppsec/hacker-helper.nvim", lazy = true } +{ + "SirAppsec/hacker-helper.nvim", + opts = { + keys = { + run_exec = "re", -- Run: Execute Command from Line Selection + } + } +} ``` diff --git a/doc/hacker-helper-docs.txt b/doc/hacker-helper-docs.txt index 64a6749..03e5920 100644 --- a/doc/hacker-helper-docs.txt +++ b/doc/hacker-helper-docs.txt @@ -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* diff --git a/lua/hacker-helper.lua b/lua/hacker-helper.lua index 249e0a5..23da91a 100644 --- a/lua/hacker-helper.lua +++ b/lua/hacker-helper.lua @@ -3,7 +3,12 @@ local module = require("hacker-helper.module") ---@class Config ---@field opt string Your config option +---@field keys table Key mappings local config = { + prefix = "r", -- Default prefix for Hacker Helper + keys = { + run_exec = "e", -- Default mapping for executing in terminal + }, opt = "Hello!", } @@ -13,11 +18,41 @@ 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 {}) + + -- Set the key mappings dynamically based on the prefix + local full_run_exec_mapping = M.config.prefix .. M.config.keys.run_exec + vim.api.nvim_set_keymap( + "n", + full_run_exec_mapping, + ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', + { noremap = true, silent = true, desc = "Execute Command" } + ) + vim.api.nvim_set_keymap( + "v", + full_run_exec_mapping, + ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', + { noremap = true, silent = true, desc = "Execute Command" } + ) + + -- Register with which-key if available + if package.loaded["which-key"] then + -- Register the prefix r with a name + require("which-key").add({ + [M.config.prefix] = { + name = "Hacker Helper", -- Prefix name + [M.config.keys.run_exec] = { + ":lua require('hacker-helper.module').exec_line_or_selection_in_term()", + "Execute in Terminal", + }, + }, + }) + end end M.hello = function() diff --git a/lua/hacker-helper/module.lua b/lua/hacker-helper/module.lua index 78a9962..f29c881 100644 --- a/lua/hacker-helper/module.lua +++ b/lua/hacker-helper/module.lua @@ -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 diff --git a/plugin/hacker-helper.lua b/plugin/hacker-helper.lua index 2e3cd2e..27269cc 100644 --- a/plugin/hacker-helper.lua +++ b/plugin/hacker-helper.lua @@ -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', 're', ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', + { noremap = true, silent = true }) +vim.api.nvim_set_keymap('v', 're', ':lua require("hacker-helper.module").exec_line_or_selection_in_term()', + { noremap = true, silent = true })