Skip to content

Commit

Permalink
Merge pull request #16 from Hashino/next
Browse files Browse the repository at this point in the history
Next
  • Loading branch information
Hashino authored Jan 5, 2025
2 parents 8d04108 + 215050a commit 00a859e
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 158 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ this plugin was originally a fork of [nocksock/do.nvim](https://github.com/nocks

### Adding Tasks

- `:Do add {task}`
- `:Do` will ask user input for `{task}`
- `:Do {task}`
- `:Do "{task}"`
- `:Do add {task}`

will all add `{task}` to the end of the tasklist

- `:Do! add {task}`
- `:Do!` will ask user input for `{task}`
- `:Do! {task}`
- `:Do! "{task}"`
- `:Do! add {task}`

will all add `{task}` to the start of the tasklist

Expand All @@ -48,9 +50,9 @@ lazy.nvim:

## Configuration

### Default Configs
### Default Options

[see the source code for default configs](https://github.com/Hashino/doing.nvim/blob/e4639e848b1503c14a591e3bfc6862560eeccefb/lua/doing/state.lua#L18-L45)
[see the source code for default options](https://github.com/Hashino/doing.nvim/blob/main/lua/doing/config.lua)

### Example Config

Expand Down
40 changes: 21 additions & 19 deletions lua/doing.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
-- A tinier task manager that helps you stay on track.
local state = require("doing.state")
local utils = require("doing.utils")
local edit = require("doing.edit")
local config = require("doing.config")
local state = require("doing.state")
local utils = require("doing.utils")
local edit = require("doing.edit")

local Doing = {}
local Doing = {}

---setup doing.nvim
---@brief setup doing.nvim
---@param opts? DoingOptions
function Doing.setup(opts)
state.options = vim.tbl_deep_extend("force", state.default_opts, opts or {})
state.tasks = state.init(state.options.store)
config.options = vim.tbl_deep_extend("force", config.default_opts, opts or {})

state.tasks = state.init(config.options.store.file_name)

-- doesn't touch the winbar if disabled so other plugins can manage
-- it without interference
if state.options.winbar.enabled then
if config.options.winbar.enabled then
state.auGroupID = vim.api.nvim_create_augroup("doing_nvim", { clear = true, })

vim.api.nvim_create_autocmd({ "BufEnter", }, {
group = state.auGroupID,
callback = function()
-- gives time to process filetype
-- HACK: gives time to process filetype
vim.defer_fn(function()
utils.update_winbar()
end, 100)
Expand All @@ -28,16 +29,17 @@ function Doing.setup(opts)
end
end

---add a task to the list
---@brief add a task to the list
---@param task? string task to add
---@param to_front? boolean whether to add task to front of list
function Doing.add(task, to_front)
if not state.tasks then
Doing.setup()
end

if task then
if task:sub(1,1) == '"' and task:sub(-1,-1) == '"' then
if task ~= nil and task ~= "" then
-- remove quotes if present
if task:sub(1, 1) == '"' and task:sub(-1, -1) == '"' then
task = task:sub(2, -2)
end

Expand Down Expand Up @@ -70,11 +72,11 @@ function Doing.done()
end

if state.tasks:count() > 0 then
state.tasks:pop()
state.tasks:done()

if state.tasks:count() == 0 then
utils.show_message("All tasks done ")
elseif not state.options.show_remaining then
elseif not config.options.show_remaining then
utils.show_message(state.tasks:count() .. " tasks left.")
else
utils.task_modified()
Expand All @@ -84,8 +86,8 @@ function Doing.done()
end
end

-- returns current plugin task/message
-- @param force boolean displays the message even if the plugin display is turned off
---@param force? boolean return status even if the plugin is toggled off
---@return string current current plugin task or message
function Doing.status(force)
if not state.tasks then
Doing.setup()
Expand All @@ -99,11 +101,11 @@ function Doing.status(force)
local count = state.tasks:count()

-- append task count number if there is more than 1 task
if state.options.show_remaining and count > 1 then
if config.options.show_remaining and count > 1 then
tasks_left = " +" .. (state.tasks:count() - 1) .. " more"
end

return state.options.doing_prefix .. state.tasks:current() .. tasks_left
return config.options.doing_prefix .. state.tasks:current() .. tasks_left
elseif force then
return "Not doing any tasks"
end
Expand Down
3 changes: 0 additions & 3 deletions lua/doing/api.lua

This file was deleted.

44 changes: 44 additions & 0 deletions lua/doing/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local Config = {}

---@class DoingOptions
---@field ignored_buffers string[]|fun():string[] elements are checked against buffer filetype/filename/filepath
---@field message_timeout integer how many millisecons messages will stay on status
---@field doing_prefix string prefix to show before the task
---@field winbar.enabled boolean if plugin should manage the winbar
---@field store.file_name string name of the task file
---@field store.auto_delete_file boolean auto delete tasks file
---@field show_remaining boolean show "+n more" when there are more than 1 tasks
---@field edit_win_config table<string, any> window configs of the floating editor

Config.default_opts = {
message_timeout = 2000,
doing_prefix = "Doing: ",

-- doesn"t display on buffers that match filetype/filename/filepath to
-- entries. can be either a string array or a function that returns a
-- string array. filepath can be relative to cwd or absolute
ignored_buffers = { "NvimTree", },

-- if should append "+n more" to the status when there's tasks remaining
show_remaining = true,

-- window configs of the floating tasks editor
-- see :h nvim_open_win() for available options
edit_win_config = {
width = 50,
height = 15,
border = "rounded",
},

-- if plugin should manage the winbar
winbar = { enabled = true, },

store = {
-- name of tasks file
file_name = ".tasks",
},
}

Config.options = Config.default_opts

return Config
91 changes: 40 additions & 51 deletions lua/doing/edit.lua
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
local config = require("doing.config")
local state = require("doing.state")

local global_win = nil
local global_buf = nil

local Edit = {}

--- Get all the tasks currently in the pop up window
Edit.win = nil
Edit.buf = nil

---get a tasks table from the buffer lines
local function get_buf_tasks()
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
local indices = {}
local tasks = {}

for _, line in pairs(lines) do
if line:gsub("%s", "") ~= "" then
table.insert(indices, line)
if Edit.buf then
local lines = vim.api.nvim_buf_get_lines(Edit.buf, 0, -1, true)

for _, line in pairs(lines) do
-- checks if line is just spaces
if line:gsub("%s", "") ~= "" then
table.insert(tasks, line)
end
end
end

return indices
return tasks
end

-- creates window
local function get_floating_window()
local bufnr = vim.api.nvim_create_buf(false, false)
---creates window
local function setup_floating_window()
Edit.buf = vim.api.nvim_create_buf(false, false)

local width = state.options.edit_win_config.width
local height = state.options.edit_win_config.height
local width = config.options.edit_win_config.width
local height = config.options.edit_win_config.height

-- Get the current screen size
local screen_width = vim.o.columns
local screen_height = vim.o.lines

Expand All @@ -44,72 +48,57 @@ local function get_floating_window()
noautocmd = true,
}

local win = vim.api.nvim_open_win(bufnr, true,
vim.tbl_extend("force", default_win_config, state.options.edit_win_config))

vim.api.nvim_set_option_value("winhl", "Normal:NormalFloat", {})

return {
buf = bufnr,
win = win,
}
Edit.win = vim.api.nvim_open_win(Edit.buf, true,
vim.tbl_extend("force", default_win_config, config.options.edit_win_config))
end

-- closes the window
---closes the window
local function close_edit(callback)
if callback then
callback(get_buf_tasks())
end

vim.api.nvim_win_close(0, true)
global_win = nil
global_buf = nil
if Edit.win then
vim.api.nvim_win_close(Edit.win, true)
Edit.win = nil
end
end

-- opens a float window to manage tasks
---@brief open floating window to edit tasks
---@param tasks table list of tasks
---@param callback function function to call when window is closed
function Edit.open_edit(tasks, callback)
if global_win ~= nil and vim.api.nvim_win_is_valid(global_win) then
close_edit()
return
if Edit.win then
return close_edit()
end

local win_info = get_floating_window()
global_win = win_info.win
global_buf = win_info.buf
setup_floating_window()

vim.api.nvim_set_option_value("number", true, {})
vim.api.nvim_set_option_value("swapfile", false, {})
vim.api.nvim_set_option_value("filetype", "doing_tasks", {})
vim.api.nvim_set_option_value("buftype", "acwrite", {})
vim.api.nvim_set_option_value("bufhidden", "delete", {})
vim.api.nvim_buf_set_name(global_buf, "do-edit")
vim.api.nvim_buf_set_lines(global_buf, 0, #tasks, false, tasks)
vim.api.nvim_buf_set_name(Edit.buf, "do-edit")

vim.api.nvim_buf_set_lines(Edit.buf, 0, #tasks, false, tasks)

vim.keymap.set("n", "q", function()
close_edit(callback)
end, { buffer = global_buf, })
end, { buffer = Edit.buf, })

vim.keymap.set("n", "<Esc>", function()
close_edit(callback)
end, { buffer = global_buf, })
end, { buffer = Edit.buf, })

-- event after tasks from pop up has been written to
-- save tasks when buffer is written
vim.api.nvim_create_autocmd("BufWriteCmd", {
group = state.auGroupID,
buffer = global_buf,
buffer = Edit.buf,
callback = function()
local new_todos = get_buf_tasks()
state.tasks:set(new_todos)
end,
})

vim.api.nvim_create_autocmd("BufModifiedSet", {
group = state.auGroupID,
buffer = global_buf,
callback = function()
vim.api.nvim_set_option_value("modified", false, {})
end,
})
end

return Edit
Loading

0 comments on commit 00a859e

Please sign in to comment.