@@ -12,9 +12,10 @@ local M = {}
1212--- require("nvim-possession").status()
1313--- @param user_opts table
1414M .setup = function (user_opts )
15+ local notification_title = " 📌 nvim-possession"
1516 local fzf_ok , fzf = pcall (require , " fzf-lua" )
1617 if not fzf_ok then
17- print (" fzf-lua required as dependency" )
18+ vim . notify (" fzf-lua required as dependency" , vim . log . levels . WARN , { title = notification_title } )
1819 return
1920 end
2021
@@ -31,7 +32,7 @@ M.setup = function(user_opts)
3132 --- return if path does not exist
3233 M .new = function ()
3334 if vim .fn .finddir (user_config .sessions .sessions_path ) == " " then
34- print (" sessions_path does not exist" )
35+ vim . notify (" sessions_path does not exist" , vim . log . levels . WARN , { title = notification_title } )
3536 return
3637 end
3738
@@ -40,9 +41,13 @@ M.setup = function(user_opts)
4041 if next (vim .fs .find (name , { path = user_config .sessions .sessions_path })) == nil then
4142 vim .cmd .mksession ({ args = { user_config .sessions .sessions_path .. name } })
4243 vim .g [user_config .sessions .sessions_variable ] = vim .fs .basename (name )
43- print (" saved in: " .. user_config .sessions .sessions_path .. name )
44+ vim .notify (
45+ " saved in: " .. user_config .sessions .sessions_path .. name ,
46+ vim .log .levels .INFO ,
47+ { title = notification_title }
48+ )
4449 else
45- print (" session already exists" )
50+ vim . notify (" session already exists" , vim . log . levels . INFO , { title = notification_title } )
4651 end
4752 end
4853 end
@@ -58,10 +63,10 @@ M.setup = function(user_opts)
5863 user_config .save_hook ()
5964 end
6065 vim .cmd .mksession ({ args = { user_config .sessions .sessions_path .. cur_session }, bang = true })
61- print (" updated session: " .. cur_session )
66+ vim . notify (" updated session: " .. cur_session , vim . log . levels . INFO , { title = notification_title } )
6267 end
6368 else
64- print (" no session loaded" )
69+ vim . notify (" no session loaded" , vim . log . levels . INFO , { title = notification_title } )
6570 end
6671 end
6772
@@ -87,69 +92,50 @@ M.setup = function(user_opts)
8792 local confirm = vim .fn .confirm (" delete session?" , " &Yes\n &No" , 2 )
8893 if confirm == 1 then
8994 os.remove (session )
90- print (" deleted " .. session )
95+ vim . notify (" deleted " .. session , vim . log . levels . INFO , { title = notification_title } )
9196 if vim .g [user_config .sessions .sessions_variable ] == vim .fs .basename (session ) then
9297 vim .g [user_config .sessions .sessions_variable ] = nil
9398 end
9499 end
95100 end
96101 fzf .config .set_action_helpstr (M .delete_selected , " delete-session" )
97102
98- -- delete current active session
103+ --- delete current active session
99104 M .delete = function ()
100105 local cur_session = vim .g [user_config .sessions .sessions_variable ]
101106 if cur_session ~= nil then
102107 local confirm = vim .fn .confirm (" delete session " .. cur_session .. " ?" , " &Yes\n &No" , 2 )
103108 if confirm == 1 then
104109 local session_path = user_config .sessions .sessions_path .. cur_session
105110 os.remove (session_path )
106- print (" deleted " .. session_path )
111+ vim . notify (" deleted " .. session_path , vim . log . levels . INFO , { title = notification_title } )
107112 if vim .g [user_config .sessions .sessions_variable ] == vim .fs .basename (session_path ) then
108113 vim .g [user_config .sessions .sessions_variable ] = nil
109114 end
110115 end
111116 else
112- print (" no active session" )
117+ vim . notify (" no active session" , vim . log . levels . WARN , { title = notification_title } )
113118 end
114119 end
115120
116121 --- list all existing sessions and their files
117- --- return fzf picker
118- M .list = function ()
122+ --- @param cwd boolean | nil
123+ M .list = function (cwd )
119124 local iter = vim .uv .fs_scandir (user_config .sessions .sessions_path )
120125 if iter == nil then
121- print (" session folder " .. user_config .sessions .sessions_path .. " does not exist" )
126+ vim .notify (
127+ " session folder " .. user_config .sessions .sessions_path .. " does not exist" ,
128+ vim .log .levels .WARN ,
129+ { title = notification_title }
130+ )
122131 return
123132 end
124- local next = vim .uv .fs_scandir_next (iter )
125- if next == nil then
126- print (" no saved sessions" )
133+ local next_dir = vim .uv .fs_scandir_next (iter )
134+ if next_dir == nil then
135+ vim . notify (" no saved sessions" , vim . log . levels . WARN , { title = notification_title } )
127136 return
128137 end
129138
130- local function list_sessions (fzf_cb )
131- local sessions = {}
132- for name , type in vim .fs .dir (user_config .sessions .sessions_path ) do
133- if type == " file" then
134- local stat = vim .uv .fs_stat (user_config .sessions .sessions_path .. name )
135- if stat then
136- table.insert (sessions , { name = name , mtime = stat .mtime })
137- end
138- end
139- end
140- table.sort (sessions , function (a , b )
141- if type (user_config .sort ) == " function" then
142- return user_config .sort (a , b )
143- else
144- return sort .alpha_sort (a , b )
145- end
146- end )
147- for _ , sess in ipairs (sessions ) do
148- fzf_cb (sess .name )
149- end
150- fzf_cb ()
151- end
152-
153139 local opts = {
154140 user_config = user_config ,
155141 prompt = user_config .sessions .sessions_icon .. user_config .sessions .sessions_prompt ,
@@ -171,11 +157,33 @@ M.setup = function(user_opts)
171157 }
172158 opts = require (" fzf-lua.config" ).normalize_opts (opts , {})
173159 opts = require (" fzf-lua.core" ).set_header (opts , { " actions" })
174- fzf .fzf_exec (list_sessions , opts )
160+
161+ --- autoload mechanism
162+ if cwd then
163+ local sessions_in_cwd = utils .list_sessions (user_config , cwd )
164+ if next (sessions_in_cwd ) == nil then
165+ vim .notify (" no session to autoload" , vim .log .levels .WARN , { title = notification_title })
166+ return nil
167+ elseif # sessions_in_cwd == 1 or not user_config .autoprompt then
168+ vim .cmd .source (user_config .sessions .sessions_path .. sessions_in_cwd [1 ])
169+ vim .g [user_config .sessions .sessions_variable ] = vim .fs .basename (sessions_in_cwd [1 ])
170+ if type (user_config .post_hook ) == " function" then
171+ user_config .post_hook ()
172+ end
173+ return nil
174+ end
175+ end
176+ --- standard list load mechanism
177+ fzf .fzf_exec (function (fzf_cb )
178+ for _ , sess in ipairs (utils .list_sessions (user_config , cwd )) do
179+ fzf_cb (sess )
180+ end
181+ fzf_cb ()
182+ end , opts )
175183 end
176184
177185 if user_config .autoload and vim .fn .argc () == 0 then
178- utils . autoload ( user_config )
186+ M . list ( true )
179187 end
180188
181189 if user_config .autosave then
0 commit comments