@@ -10,9 +10,11 @@ local uv = vim.uv or vim.loop
1010
1111--- Fire an event
1212--- @param event string
13+ --- @param data ? table
1314--- @return nil
14- function M .fire (event )
15- vim .api .nvim_exec_autocmds (" User" , { pattern = " Persisted" .. event })
15+ function M .fire (event , data )
16+ data = data or {}
17+ vim .api .nvim_exec_autocmds (" User" , { pattern = " Persisted" .. event , data = data })
1618end
1719
1820--- Get the current session for the current working directory and git branch
@@ -126,17 +128,17 @@ end
126128--- Delete a session
127129--- @param opts ? { path ?: string }
128130--- @return nil
129- function M .delete (opts )
131+ function M .delete_current (opts )
130132 opts = opts or {}
131133 local session = opts .path or M .current ()
132134
133135 if session and uv .fs_stat (session ) ~= 0 then
134- M .fire (" DeletePre" )
136+ M .fire (" DeletePre" , { path = session } )
135137 vim .schedule (function ()
136138 M .stop ()
137139 vim .fn .delete (vim .fn .expand (session ))
138140 end )
139- M .fire (" DeletePost" )
141+ M .fire (" DeletePost" , { path = session } )
140142 end
141143end
142144
@@ -149,9 +151,11 @@ function M.branch()
149151 end
150152end
151153
152- --- Select a session to load
154+ --- Handle the vim.ui.select behaviour
155+ --- @param opts { prompt : string , handler : function }
153156--- @return nil
154- function M .select ()
157+ function M .handle_selected (opts )
158+ --- @type { session : string , dir : string , branch ?: string } []
155159 local items = {} --- @type { session : string , dir : string , branch ?: string } []
156160 local found = {} --- @type table<string , boolean>
157161 for _ , session in ipairs (M .list ()) do
@@ -169,7 +173,7 @@ function M.select()
169173 end
170174 end
171175 vim .ui .select (items , {
172- prompt = " Load a session: " ,
176+ prompt = opts . prompt ,
173177 format_item = function (item )
174178 local name = vim .fn .fnamemodify (item .dir , " :p:~" )
175179 if item .branch then
@@ -179,12 +183,34 @@ function M.select()
179183 end ,
180184 }, function (item )
181185 if item then
186+ opts .handler (item )
187+ end
188+ end )
189+ end
190+
191+ --- Load a session from the list
192+ --- @return nil
193+ function M .select ()
194+ M .handle_selected ({
195+ prompt = " Load a session: " ,
196+ handler = function (item )
182197 M .fire (" SelectPre" )
183198 vim .fn .chdir (item .dir )
184199 M .load ()
185200 M .fire (" SelectPost" )
186- end
187- end )
201+ end ,
202+ })
203+ end
204+
205+ --- Delete a session from the list
206+ --- @return nil
207+ function M .delete ()
208+ M .handle_selected ({
209+ prompt = " Delete a session: " ,
210+ handler = function (item )
211+ M .delete_current ({ path = item .session })
212+ end ,
213+ })
188214end
189215
190216--- Determines whether to load, start or stop a session
0 commit comments