Skip to content

Commit f3df59e

Browse files
authored
Merge pull request #7 from mhiro2/fix/list-sessions-notify-when-disabled
fix(commands,persist): Improve session listing UX when persist is disabled
2 parents 61aca7f + a8f4da7 commit f3df59e

4 files changed

Lines changed: 93 additions & 2 deletions

File tree

lua/peekstack/commands.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ local function session_updated_at_text(session)
3232
if type(updated_at) ~= "number" then
3333
return "unknown"
3434
end
35-
return os.date("%Y-%m-%d %H:%M:%S", updated_at)
35+
return vim.fn.strftime("%Y-%m-%d %H:%M:%S", updated_at)
3636
end
3737

3838
---@return string[]

lua/peekstack/persist/init.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,19 @@ function M.restore(name, opts)
195195
end
196196

197197
---List all saved sessions
198-
---@param opts? { on_done?: fun(sessions: table<string, PeekstackSession>) }
198+
---@param opts? { on_done?: fun(sessions: table<string, PeekstackSession>), silent?: boolean }
199199
---@return table<string, PeekstackSession>
200200
function M.list_sessions(opts)
201201
local on_done = opts and opts.on_done or nil
202+
local silent = opts and opts.silent
203+
if silent == nil then
204+
-- Synchronous list calls are mostly used for command completion.
205+
-- Keep them silent to avoid notification spam when persist is disabled.
206+
silent = on_done == nil
207+
end
208+
if not ensure_enabled(silent) then
209+
return {}
210+
end
202211
if on_done then
203212
store.read(SCOPE, {
204213
on_done = function(read_data)

tests/commands_spec.lua

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,35 @@
11
describe("peekstack.commands", function()
22
local commands = require("peekstack.commands")
3+
local config = require("peekstack.config")
34
local persist = require("peekstack.persist")
45
local original_list_sessions = nil
56
local original_select = nil
7+
local original_notify = nil
8+
local original_strftime = nil
69

710
before_each(function()
11+
config.setup({})
812
commands._reset()
913
original_list_sessions = persist.list_sessions
1014
original_select = vim.ui.select
15+
original_notify = vim.notify
16+
original_strftime = vim.fn.strftime
1117
end)
1218

1319
after_each(function()
20+
config.setup({})
1421
if original_list_sessions then
1522
persist.list_sessions = original_list_sessions
1623
end
1724
if original_select then
1825
vim.ui.select = original_select
1926
end
27+
if original_notify then
28+
vim.notify = original_notify
29+
end
30+
if original_strftime then
31+
vim.fn.strftime = original_strftime
32+
end
2033
commands._reset()
2134
end)
2235

@@ -69,6 +82,59 @@ describe("peekstack.commands", function()
6982
assert.equals("broken: 1 items (updated: unknown)", prompts[2])
7083
end)
7184

85+
it("notifies when list sessions is invoked while persist is disabled", function()
86+
config.setup({ persist = { enabled = false } })
87+
88+
local messages = {}
89+
vim.notify = function(msg)
90+
table.insert(messages, msg)
91+
end
92+
93+
commands.setup()
94+
vim.api.nvim_cmd({ cmd = "PeekstackListSessions" }, {})
95+
96+
assert.is_true(vim.list_contains(messages, "peekstack.persist is disabled"))
97+
end)
98+
99+
it("formats session updated_at with vim.fn.strftime", function()
100+
local prompts = {}
101+
local strftime_calls = {}
102+
103+
persist.list_sessions = function(opts)
104+
assert.is_truthy(opts)
105+
assert.is_truthy(opts.on_done)
106+
opts.on_done({
107+
alpha = {
108+
items = {},
109+
meta = { updated_at = 123 },
110+
},
111+
})
112+
return {}
113+
end
114+
115+
vim.fn.strftime = function(fmt, ts)
116+
table.insert(strftime_calls, { fmt = fmt, ts = ts })
117+
return "formatted-time"
118+
end
119+
120+
vim.ui.select = function(_items, opts, on_choice)
121+
table.insert(prompts, opts.prompt)
122+
if opts.prompt == "Select a session" then
123+
on_choice("alpha")
124+
return
125+
end
126+
on_choice("Info only")
127+
end
128+
129+
commands.setup()
130+
vim.api.nvim_cmd({ cmd = "PeekstackListSessions" }, {})
131+
132+
assert.equals(1, #strftime_calls)
133+
assert.equals("%Y-%m-%d %H:%M:%S", strftime_calls[1].fmt)
134+
assert.equals(123, strftime_calls[1].ts)
135+
assert.equals("alpha: 0 items (updated: formatted-time)", prompts[2])
136+
end)
137+
72138
it("includes extended providers in quick peek completion", function()
73139
commands.setup()
74140
local names = vim.fn.getcompletion("PeekstackQuickPeek ", "cmdline")

tests/persist_sessions_spec.lua

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,28 @@ describe("peekstack.persist.sessions", function()
187187

188188
it("should notify when persist is disabled", function()
189189
config.setup({ persist = { enabled = false } })
190+
local original_notify = vim.notify
191+
local messages = {}
192+
vim.notify = function(msg)
193+
table.insert(messages, msg)
194+
end
190195

191196
-- These should not error when persist is disabled
192197
persist.save_current("test")
193198
persist.restore("test")
199+
local sessions = persist.list_sessions()
200+
assert.same({}, sessions)
201+
local callback_called = false
202+
persist.list_sessions({
203+
on_done = function()
204+
callback_called = true
205+
end,
206+
})
207+
assert.is_false(callback_called)
194208
persist.delete_session("test")
195209
persist.rename_session("a", "b")
210+
assert.is_true(vim.list_contains(messages, "peekstack.persist is disabled"))
211+
vim.notify = original_notify
196212
end)
197213

198214
it("should handle non-existent sessions gracefully", function()

0 commit comments

Comments
 (0)