@@ -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
4244end
@@ -45,19 +47,70 @@ if not config.options.store.sync_tasks then
4547 vim .api .nvim_create_autocmd (" VimLeave" , { callback = sync , })
4648end
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 ()
5198end
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+
53108function State .done ()
54109 table.remove (State .tasks , 1 )
55- return config .options .store .sync_tasks and sync ()
56110end
57111
58112function State .set (tasks )
59113 State .tasks = tasks
60- return config .options .store .sync_tasks and sync ()
61114end
62115
63116return State
0 commit comments