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..f094817 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 @@ -28,11 +29,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 +46,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 +59,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 +185,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 +243,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()