Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Install using your favorite package manager. Here is an example with [lazy.nvim]
keys = {
{ "<leader>r", function () require("runner-nvim").runLast() end, desc = "Run last cmd" },
{ "<leader>o", function () require("runner-nvim").run() end, desc = "Run cmd" },
{ "<leader>b", function () require("runner-nvim").runBuild() end, desc = "Run build cmd"},
{ "<leader>B", function () require("runner-nvim").changeBuild() end, desc = "Change build cmd"},
{ "<leader>t", function () require("runner-nvim").toggle() end, desc = "Toggle terminal"},
}
}
Expand All @@ -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., `<leader>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., `<leader>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., `<leader>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., `<leader>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., `<leader>t`) to show or hide the terminal window.
- **Close Terminal**: Press `q` in Normal mode inside the terminal window to close it.
57 changes: 48 additions & 9 deletions lua/runner-nvim/init.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
local M = {}

---@class CmdInfo
---@field cmd string
---@field lastcmd string?
---@field build string?
---@field time integer

---@type string
Expand All @@ -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<string, CmdInfo>
local function removeExtraEntries(data)

local maxEntries = 100
local count = 0
Expand All @@ -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
Expand All @@ -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?
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down