Skip to content

Commit 16a155d

Browse files
committed
Merge pull request #21 from Hashino/next
fix: sync tasks
2 parents dcf1c97 + 770b7c3 commit 16a155d

File tree

4 files changed

+70
-60
lines changed

4 files changed

+70
-60
lines changed

lua/doing.lua

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ function Doing.add(task, to_front)
3535
end
3636

3737
state.add(task, to_front)
38-
utils.task_modified()
38+
state.task_modified()
3939
else
4040
vim.ui.input({ prompt = "Enter the new task: ", }, function(input)
4141
state.add(input, to_front)
42-
utils.task_modified()
42+
state.task_modified()
4343
end)
4444
end
4545
end
@@ -55,44 +55,27 @@ function Doing.done()
5555
state.done()
5656

5757
if #state.tasks == 0 then
58-
utils.show_message("All tasks done ")
58+
state.show_message("All tasks done ")
5959
elseif not config.options.show_remaining then
60-
utils.show_message(#state.tasks .. " tasks left.")
60+
state.show_message(#state.tasks .. " tasks left.")
6161
else
62-
utils.task_modified()
62+
state.task_modified()
6363
end
6464
else
65-
utils.show_message("Not doing any task")
65+
state.show_message("Not doing any task")
6666
end
6767
end
6868

6969
---@param force? boolean return status even if the plugin is toggled off
7070
---@return string current current plugin task or message
7171
function Doing.status(force)
72-
if (state.view_enabled or force) and utils.should_display() then
73-
local count = #state.tasks or 0
74-
if state.message then
75-
return state.message
76-
elseif count > 0 then
77-
local tasks_left = ""
78-
79-
-- append task count number if there is more than 1 task
80-
if config.options.show_remaining and count > 1 then
81-
tasks_left = " +" .. (count - 1) .. " more"
82-
end
83-
84-
return config.options.doing_prefix .. state.tasks[1] .. tasks_left
85-
elseif force then
86-
return "Not doing any tasks"
87-
end
88-
end
89-
return ""
72+
return state.status(force)
9073
end
9174

9275
---toggle the visibility of the plugin
9376
function Doing.toggle()
9477
state.view_enabled = not state.view_enabled
95-
utils.task_modified()
78+
state.task_modified()
9679
end
9780

9881
---@return integer number of tasks left

lua/doing/edit.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local win_cfg = require("doing.config").options.edit_win_config
2-
local utils = require("doing.utils")
32
local state = require("doing.state")
43

54
local Edit = {
@@ -25,7 +24,7 @@ function Edit.open_edit()
2524
end
2625

2726
state.set(lines)
28-
vim.defer_fn(utils.task_modified, 0)
27+
vim.defer_fn(state.task_modified, 0)
2928
end,
3029
})
3130
end

lua/doing/state.lua

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ vim.api.nvim_create_autocmd({ "DirChanged", "VimEnter", }, {
1818

1919
local ok, res = pcall(vim.fn.readfile, tasks_file)
2020
State.tasks = ok and res or {}
21-
utils.task_modified()
21+
State.task_modified()
2222
end,
2323
})
2424

@@ -36,7 +36,9 @@ local function sync()
3636
end)()
3737
end
3838

39-
if #State.tasks > 0 and not vim.fn.writefile(State.tasks, tasks_file) then
39+
local ok = pcall(vim.fn.writefile, State.tasks, tasks_file)
40+
41+
if #State.tasks > 0 and not ok then
4042
utils.notify("error writing to tasks file", vim.log.levels.ERROR)
4143
end
4244
end
@@ -45,19 +47,70 @@ if not config.options.store.sync_tasks then
4547
vim.api.nvim_create_autocmd("VimLeave", { callback = sync, })
4648
end
4749

48-
function State.add(str, to_front)
49-
table.insert(State.tasks, to_front and 1 or #State.tasks, str)
50+
---@param force? boolean return status even if the plugin is toggled off
51+
---@return string current current plugin task or message
52+
function State.status(force)
53+
if (State.view_enabled or force) and utils.should_display() then
54+
local count = #State.tasks or 0
55+
if State.message then
56+
return State.message
57+
elseif count > 0 then
58+
local tasks_left = ""
59+
60+
-- append task count number if there is more than 1 task
61+
if config.options.show_remaining and count > 1 then
62+
tasks_left = " +" .. (count - 1) .. " more"
63+
end
64+
65+
return config.options.doing_prefix .. State.tasks[1] .. tasks_left
66+
elseif force then
67+
return "Not doing any tasks"
68+
end
69+
end
70+
return ""
71+
end
72+
73+
---show a message for the duration of `options.message_timeout` or timeout
74+
---@param str string message to show
75+
---@param timeout? number time in ms to show message
76+
function State.show_message(str, timeout)
77+
if config.options.show_messages then
78+
State.message = str
79+
State.task_modified()
80+
81+
vim.defer_fn(function()
82+
State.message = nil
83+
State.task_modified()
84+
end, timeout or config.options.message_timeout)
85+
else
86+
State.task_modified()
87+
end
88+
end
89+
90+
---gets called when a task is added, edited, or removed
91+
function State.task_modified()
92+
if config.options.winbar.enabled then
93+
vim.api.nvim_set_option_value("winbar", State.status(), { scope = "local", })
94+
end
95+
96+
vim.api.nvim_exec_autocmds("User", { pattern = "TaskModified", })
5097
return config.options.store.sync_tasks and sync()
5198
end
5299

100+
function State.add(str, to_front)
101+
if to_front then
102+
table.insert(State.tasks, 1, str)
103+
else
104+
table.insert(State.tasks, str)
105+
end
106+
end
107+
53108
function State.done()
54109
table.remove(State.tasks, 1)
55-
return config.options.store.sync_tasks and sync()
56110
end
57111

58112
function State.set(tasks)
59113
State.tasks = tasks
60-
return config.options.store.sync_tasks and sync()
61114
end
62115

63116
return State

lua/doing/utils.lua

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ local Utils = {}
55
---redraw winbar depending on if there are tasks
66
function Utils.update_winbar()
77
if config.options.winbar.enabled then
8-
local status = require("doing").status()
9-
vim.api.nvim_set_option_value("winbar", status, { scope = "local", })
8+
vim.api.nvim_set_option_value("winbar", require("doing").status(),
9+
{ scope = "local", })
1010
end
1111
end
1212

@@ -57,31 +57,6 @@ function Utils.should_display()
5757
return true
5858
end
5959

60-
---gets called when a task is added, edited, or removed
61-
function Utils.task_modified()
62-
Utils.update_winbar()
63-
vim.api.nvim_exec_autocmds("User", {
64-
pattern = "TaskModified",
65-
})
66-
end
67-
68-
---show a message for the duration of `options.message_timeout` or timeout
69-
---@param str string message to show
70-
---@param timeout? number time in ms to show message
71-
function Utils.show_message(str, timeout)
72-
if config.options.show_messages then
73-
require("doing.state").message = str
74-
Utils.task_modified()
75-
76-
vim.defer_fn(function()
77-
require("doing.state").message = nil
78-
Utils.task_modified()
79-
end, timeout or config.options.message_timeout)
80-
else
81-
Utils.task_modified()
82-
end
83-
end
84-
8560
function Utils.os_path_separator()
8661
local dir_separator = "/"
8762
if (vim.loop or vim.uv).os_uname().sysname:find("Windows") then

0 commit comments

Comments
 (0)