From e3c2d6dcc587bf2fb3cbe0bfa71287842b47b7f8 Mon Sep 17 00:00:00 2001 From: Sira0909 Date: Wed, 4 Mar 2026 21:56:54 -0500 Subject: [PATCH 1/2] Build command --- README.md | 4 +++ lua/runner-nvim/init.lua | 64 +++++++++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 8bcf13a..db6ee7d 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ Install using your favorite package manager. Here is an example with [lazy.nvim] keys = { { "r", function () require("runner-nvim").runLast() end, desc = "Run last cmd" }, { "o", function () require("runner-nvim").run() end, desc = "Run cmd" }, + { "b", function () require("runner-nvim").runBuild() end, desc = "Run build cmd"}, + { "B", function () require("runner-nvim").changeBuild() end, desc = "Change build cmd"}, { "t", function () require("runner-nvim").toggle() end, desc = "Toggle terminal"}, } } @@ -31,5 +33,7 @@ Install using your favorite package manager. Here is an example with [lazy.nvim] - **Run Command**: Trigger the run command keybinding (e.g., `o`). A prompt will appear. Type your shell command (e.g., `npm test`, `cargo build`, `make`) and press Enter. - **Run Last Command**: Trigger the run last command keybinding (e.g., `r`). The plugin will execute the last command used in the current working directory. If no command is found, it will prompt you for one. +- **Change Build Command**: Trigger the run command keybinding (e.g., `B`). A prompt will appear. Type your shell command (e.g., `npm test`, `cargo build`, `make`) and press Enter. +- **Run Build Command**: Trigger the build command keybinding (e.g., `b`). The plugin will execute the build command for the current working directory. If no build command is found, it will prompt you for one. - **Toggle Terminal**: Use the toggle keybinding (e.g., `t`) to show or hide the terminal window. - **Close Terminal**: Press `q` in Normal mode inside the terminal window to close it. diff --git a/lua/runner-nvim/init.lua b/lua/runner-nvim/init.lua index c6b4480..64f3a5d 100644 --- a/lua/runner-nvim/init.lua +++ b/lua/runner-nvim/init.lua @@ -1,7 +1,8 @@ local M = {} ---@class CmdInfo ----@field cmd string +---@field lastcmd string? +---@field build string? ---@field time integer ---@type string @@ -16,8 +17,11 @@ end ---@return table local function readHistory() local jsonString = vim.fn.readfile(historyPath) - - return vim.json.decode(jsonString[1]) + if(jsonString) then + return vim.json.decode(jsonString[1]) + else + return {} + end end ---@return string @@ -28,11 +32,8 @@ local function getCwd() return result end ----@param cmd string -local function updateHistory(cmd) - local data = readHistory() - - data[getCwd()] = { cmd = cmd, time = os.time() } +---@param data table +local function removeExtraEntries(data) local maxEntries = 100 local count = 0 @@ -48,7 +49,7 @@ local function updateHistory(cmd) end table.sort(items, function(a, b) - return a.time < b.time + return a.recent.time < b.recent.time end) local toRemove = count - maxEntries @@ -61,6 +62,24 @@ local function updateHistory(cmd) saveHistory(data) end +---@param cmd string +local function updateHistory(cmd) + local data = readHistory() + + + data[getCwd()] = { lastcmd = cmd, build = (data[getCwd()]~=nil and data[getCwd()].build) or nil, time = os.time()} + + removeExtraEntries(data) +end +---@param cmd string +local function updateBuild(cmd) + local data = readHistory() + + data[getCwd()] = { lastcmd=(data[getCwd()] and data[getCwd()].lastcmd) or nil, build=cmd, time = os.time()} + + removeExtraEntries(data) +end + ---@class Terminal ---@field terminalBuf integer? ---@field terminalWin integer? @@ -169,6 +188,16 @@ function Terminal:run(cmd) updateHistory(cmd) end +---@param cmd string +function Terminal:build(cmd) + self:open() + + vim.schedule(function () + vim.api.nvim_chan_send(self.jobId, cmd .. "\r") + end) + + updateBuild(cmd) +end ---@type Terminal local terminal @@ -217,17 +246,30 @@ end function M.run() askCmd(function (cmd) terminal:run(cmd) end) end +function M.changeBuild() + askCmd(function (cmd) terminal:build(cmd) end) +end function M.runLast() local data = readHistory() local cmdInfo = data[getCwd()] - if (cmdInfo and cmdInfo.cmd) then - terminal:run(cmdInfo.cmd) + if (cmdInfo and cmdInfo.lastcmd) then + terminal:run(cmdInfo.lastcmd) else M.run() end end +function M.runBuild() + local data = readHistory() + local cmdInfo = data[getCwd()] + + if (cmdInfo and cmdInfo.build) then + terminal:run(cmdInfo.build) + else + M.changeBuild() + end +end function M.toggle() terminal:toggle() From 349dee173ee24255bdc46dbd709d56b0102cdf9d Mon Sep 17 00:00:00 2001 From: Sira0909 Date: Wed, 4 Mar 2026 22:07:18 -0500 Subject: [PATCH 2/2] small fix --- lua/runner-nvim/init.lua | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lua/runner-nvim/init.lua b/lua/runner-nvim/init.lua index 64f3a5d..f094817 100644 --- a/lua/runner-nvim/init.lua +++ b/lua/runner-nvim/init.lua @@ -17,11 +17,8 @@ end ---@return table local function readHistory() local jsonString = vim.fn.readfile(historyPath) - if(jsonString) then - return vim.json.decode(jsonString[1]) - else - return {} - end + + return vim.json.decode(jsonString[1]) end ---@return string