Skip to content

Commit

Permalink
feat: autosave in background
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc-stripe committed Sep 10, 2022
1 parent adaa2ba commit 9e6b70f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ This is a work in progress

TODO:

- periodic saving
- tab-scoped sessions
- documentation
- make filepaths relative so sessions are portable
26 changes: 26 additions & 0 deletions lua/resession/config.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
local M = {}

local default_config = {
autosave = {
enabled = false,
-- How often to save (in seconds)
interval = 60,
-- Notify when autosaved
notify = true,
},
buffers = {
-- Only save buffers with these buftypes
buftypes = { "", "acwrite", "help" },
Expand Down Expand Up @@ -64,11 +71,30 @@ local default_config = {
extensions = {},
}

local autosave_timer
M.setup = function(config)
local resession = require("resession")
local newconf = vim.tbl_deep_extend("force", default_config, config)
for k, v in pairs(newconf) do
M[k] = v
end
if autosave_timer then
autosave_timer:close()
autosave_timer = nil
end
if M.autosave.enabled then
autosave_timer = vim.loop.new_timer()
timer = vim.loop.new_timer()
timer:start(
M.autosave.interval * 1000,
M.autosave.interval * 1000,
vim.schedule_wrap(function()
if resession.get_current() then
resession.save(nil, { notify = M.autosave.notify })
end
end)
)
end
end

---@return string
Expand Down
8 changes: 6 additions & 2 deletions lua/resession/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,9 @@ end

local function close_everything()
for _, bufnr in ipairs(vim.api.nvim_list_bufs()) do
vim.api.nvim_buf_delete(bufnr, { force = true })
if vim.bo[bufnr].buflisted then
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end
vim.cmd("silent! tabonly")
vim.cmd("silent! only")
Expand Down Expand Up @@ -283,7 +285,9 @@ M.load = function(name, opts)
for k, v in pairs(data.global.options) do
vim.o[k] = v
end
if not opts.detach then
if opts.detach then
current_session = nil
else
current_session = name
end
end
Expand Down

0 comments on commit 9e6b70f

Please sign in to comment.