Skip to content
Merged
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ Run shell commands(tasks) in dedicated terminal windows.
- install using your favorite plugin manager

[lazy.nvim](https://github.com/folke/lazy.nvim)

```lua
{
"eumis/tasks.nvim"
}
```

[mini.deps](https://github.com/echasnovski/mini.nvim/blob/main/readmes/mini-deps.md)

```lua
add({
source = "eumis/tasks.nvim"
})
```

[packer](https://github.com/wbthomason/packer.nvim)

```lua
use {
"eumis/tasks.nvim"
Expand All @@ -47,6 +50,9 @@ tasks.open_last()

-- Open/close list of tasks
tasks.toggle_list()

-- Open list of tasks in telescope
require("telescope").extensions.tasks.all()
```

```vim
Expand All @@ -67,6 +73,7 @@ require "tasks".setup {
run_keys = { "r", "<cr>" }, -- keys to run task from tasks list
open_keys = { "o" }, -- keys to open task buffer from tasks list
get_list_win_config = get_float_win_config, -- returns config for vim.api.nvim_open_win for list window
get_task_win_config = get_float_win_config -- returns config for vim.api.nvim_open_win for task window
get_task_win_config = get_float_win_config, -- returns config for vim.api.nvim_open_win for task window
sort = "order", -- "order" - tasks in added order, "recent" - tasks run most recently are first
}
```
23 changes: 19 additions & 4 deletions lua/tasks/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,18 @@ local M = {}
---@field win integer
---@field sort_order integer
---@field channel integer
---@field last_run number

---@alias sort
---| "recent"
---| "order"

---@class Options
---@field run_keys? string[]
---@field open_keys? string[]
---@field get_list_win_config? fun(): vim.api.keyset.win_config
---@field get_task_win_config? fun(): vim.api.keyset.win_config
---@field sort sort?

local state = {
list_buf = -1,
Expand Down Expand Up @@ -68,7 +74,8 @@ function M.add(name, cmd, params)
buf = -1,
win = -1,
sort_order = state.tasks_count,
channel = -1
last_run = -state.tasks_count,
channel = -1,
}
if existing ~= nil then
new_task.buf = existing.buf
Expand All @@ -94,7 +101,11 @@ function M.get_all()
for _, task in pairs(state.tasks) do
table.insert(tasks, task)
end
table.sort(tasks, function(left, right) return left.sort_order < right.sort_order end)
if M.opts.sort == "recent" then
table.sort(tasks, function(left, right) return left.last_run > right.last_run end)
else
table.sort(tasks, function(left, right) return left.sort_order < right.sort_order end)
end
return tasks
end

Expand Down Expand Up @@ -146,9 +157,9 @@ function M.run(name, bufnr)
vim.fn.chansend(task.channel, { cmd, "" })
vim.cmd("normal G")
state.last_run_task = name
task.last_run = os.time()
end


function M.run_last()
local task = state.tasks[state.last_run_task]
if task == nil then error("task not run yet") end
Expand Down Expand Up @@ -186,7 +197,11 @@ function M.open_list()
for name, _ in pairs(state.tasks) do
table.insert(tasks, name)
end
table.sort(tasks, function(left, right) return state.tasks[left].sort_order < state.tasks[right].sort_order end)
if M.opts.sort == "recent" then
table.sort(tasks, function(left, right) return state.tasks[left].last_run > state.tasks[right].last_run end)
else
table.sort(tasks, function(left, right) return state.tasks[left].sort_order < state.tasks[right].sort_order end)
end
vim.api.nvim_buf_set_lines(state.list_buf, 0, -1, false, tasks)

state.list_win = vim.api.nvim_open_win(state.list_buf, true, M.opts.get_list_win_config())
Expand Down
75 changes: 75 additions & 0 deletions lua/telescope/_extensions/tasks.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
local has_telescope, telescope = pcall(require, "telescope")

if not has_telescope then
return
end

local action_state = require("telescope.actions.state")
local finders = require("telescope.finders")
local conf = require("telescope.config").values
local previewers = require("telescope.previewers")

local get_task_finder = function()
return finders.new_table({
results = require "tasks".get_all(),
---@param entry Task
entry_maker = function(entry)
return {
value = entry,
ordinal = entry.name,
display = entry.name
}
end,
})
end

local define_preview = function(self, entry, _)
---@type Task
local task = entry.value
local lines = {}

if task.cwd ~= nil then
table.insert(lines, "cd " .. task.cwd)
end

local cmd = task.cmd
if type(cmd) == "function" then
cmd = cmd(self.state.bufnr)
end
if type(cmd) == "string" then
cmd = { cmd }
end
for _, c in ipairs(cmd) do
table.insert(lines, c)
end

vim.api.nvim_buf_set_lines(self.state.bufnr, 0, -1, false, lines)
require('telescope.previewers.utils').highlighter(self.state.bufnr, "bash")
end

local attach_mappings = function(_, map)
map("i", "<cr>", function() require "tasks".run(action_state.get_selected_entry().value.name) end)
map("n", "<cr>", function() require "tasks".run(action_state.get_selected_entry().value.name) end)

map("i", "<c-o>", function() require "tasks".open(action_state.get_selected_entry().value.name) end)
map("n", "<c-o>", function() require "tasks".open(action_state.get_selected_entry().value.name) end)

return true
end

return telescope.register_extension({
exports = {
all = function(opts)
opts = opts or {}
require("telescope.pickers").new(opts, {
prompt_title = "Tasks",
finder = get_task_finder(),
sorter = conf.generic_sorter({}),
previewer = previewers.new_buffer_previewer({
define_preview = define_preview
}),
attach_mappings = attach_mappings
}):find()
end,
},
})