Skip to content

Commit

Permalink
feat: running in the terminal as 4th mode
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgrieser committed Dec 17, 2024
1 parent fd7615e commit 4d63425
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
7 changes: 7 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ demo-success:
sleep 0.1
exit 0
# asks for input
demo-in-terminal:
#!/usr/bin/env zsh
echo "Enter something: "
read -r input
echo
echo "Input was: $input"
29 changes: 19 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@ Just an integration of [`just`](https://github.com/casey/just) in nvim.
## Features
- **Quick-select** recipes via keys shown at the left of the window. Running
recipes thus requires only 2–3 keystrokes.
- Runs asynchronously and outputs results in a notification window.
- Supports streaming output (e.g., for recipes that display a progress bar).
- Can alternatively run asynchronously and send individual recipe results to the
**quickfix list**.
- Different run modes
1. Default: **asynchronously** and outputs results in a notification window
2. Streaming output: useful for recipes with progress bars
3. Quickfix list: synchronously
4. Terminal: useful for recipes that require input
- Inspect recipes and variable values.
- Hide specific recipes, useful to always exclude recipes that require user
input.
Expand Down Expand Up @@ -65,18 +66,25 @@ require("justice").setup {
recipes = {
-- All strings are checked via `string.find`, that is as lua patterns.
-- (Note that in lua patterns, a `-` needs to escaped as `%-`.)
ignore = { -- hides them from the nvim-justice selection window
name = { "fzf", "^_" }, -- ...if the name contains "fzf" or starts with "_"
comment = { "interactive" }, -- ...if the comment contains "interactive"
},
streaming = { -- streams output, e.g. for progress bars (requires `snacks.nvim`)
streaming = { -- streams output, useful for progress bars (requires `snacks.nvim`)
name = { "download" },
comment = { "streaming", "curl" }, -- comment with "streaming" or "curl"
comment = { "streaming", "curl" }, -- comment contains "streaming" or "curl"
},
terminal = { -- runs in terminal, useful for recipes with input
name = {},
comment = { "input" },
},
quickfix = { -- runs synchronously and sends output to quickfix list
name = { "%-qf$" }, -- name ending with "-qf"
comment = { "quickfix" },
},
ignore = { -- hides them from the nvim-justice selection window
name = { "fzf", "^_" }, -- ...if the name contains with "_"
comment = {},
},
},
terminal = {
height = 10,
},
keymaps = {
next = "<Tab>",
Expand All @@ -100,6 +108,7 @@ require("justice").setup {
streaming = "",
quickfix = "",
ignore = "󰈉",
terminal = "",
},
}
```
Expand Down
19 changes: 17 additions & 2 deletions lua/justice/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ function M.runRecipe(recipe)
end

-- PRE-RUN NOTIFICATION
if package.loaded["snacks"] then u.showRunningNotification(recipe) end
if package.loaded["snacks"] and recipe.type ~= "terminal" then
u.showRunningNotification(recipe)
end

-- 2) STREAMING
if recipe.type == "streaming" then
Expand All @@ -85,7 +87,20 @@ function M.runRecipe(recipe)
return
end

-- 3) DEFAULT
-- 3) TERMINAL
if recipe.type == "terminal" then
vim.cmd.new() -- bottom split new window
vim.cmd.terminal()
local height = require("justice.config").config.terminal.height
vim.api.nvim_win_set_height(0, height)

local argStr = table.concat(recipe:justArgs(recipe.name), " ")
vim.api.nvim_chan_send(vim.bo.channel, argStr .. "\n") -- `\n` to send
vim.cmd.startinsert { bang = true }
return
end

-- 4) DEFAULT
vim.system(
recipe:justArgs(recipe.name),
{},
Expand Down
20 changes: 14 additions & 6 deletions lua/justice/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ local defaultConfig = {
recipes = {
-- All strings are checked via `string.find`, that is as lua patterns.
-- (Note that in lua patterns, a `-` needs to escaped as `%-`.)
ignore = { -- hides them from the nvim-justice selection window
name = { "fzf", "^_" }, -- ...if the name contains "fzf" or starts with "_"
comment = { "interactive" }, -- ...if the comment contains "interactive"
},
streaming = { -- streams output, e.g. for progress bars (requires `snacks.nvim`)
streaming = { -- streams output, useful for progress bars (requires `snacks.nvim`)
name = { "download" },
comment = { "streaming", "curl" }, -- comment with "streaming" or "curl"
comment = { "streaming", "curl" }, -- comment contains "streaming" or "curl"
},
terminal = { -- runs in terminal, useful for recipes with input
name = {},
comment = { "input" },
},
quickfix = { -- runs synchronously and sends output to quickfix list
name = { "%-qf$" }, -- name ending with "-qf"
comment = { "quickfix" },
},
ignore = { -- hides them from the nvim-justice selection window
name = { "fzf", "^_" }, -- ...if the name contains with "_"
comment = {},
},
},
terminal = {
height = 10,
},
keymaps = {
next = "<Tab>",
Expand All @@ -41,6 +48,7 @@ local defaultConfig = {
streaming = "",
quickfix = "",
ignore = "󰈉",
terminal = "",
},
}

Expand Down
2 changes: 1 addition & 1 deletion lua/justice/get-recipes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ local Recipe = {
comment = "",
displayText = "",

---@type "streaming"|"quickfix"|"ignore"|nil
---@type "streaming"|"quickfix"|"ignore"|"terminal"|nil
type = nil,
---@type string?
justfile = nil,
Expand Down

0 comments on commit 4d63425

Please sign in to comment.