🌱 tiny neovim plugin keeping your session safe
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat: add config

robin fef426b5 60b7f325

+73 -15
+42
lua/nivvie/config.lua
··· 1 + local config = {} 2 + 3 + ---@class nivvie.config.proto 4 + ---@field session_dir? string 5 + ---@field autosave? boolean 6 + ---@field autorestore? boolean 7 + 8 + ---@class nivvie.config 9 + ---@field session_dir string 10 + ---@field autosave boolean 11 + ---@field autorestore boolean 12 + 13 + ---@type nivvie.config 14 + config.default = { 15 + -- where sessions are stored 16 + session_dir = vim.fn.stdpath 'state' .. '/sessions', 17 + -- save on exit 18 + autosave = true, 19 + -- load on start 20 + autorestore = true, 21 + } 22 + 23 + ---@type nivvie.config.proto 24 + config.current = {} 25 + 26 + ---@return nivvie.config 27 + function config.get() 28 + return vim.tbl_deep_extend('force', config.default, config.current) 29 + end 30 + 31 + ---@param cfg nivvie.config.proto 32 + ---@return nivvie.config 33 + function config.override(cfg) 34 + return vim.tbl_deep_extend('force', config.default, cfg) 35 + end 36 + 37 + ---@param cfg nivvie.config.proto 38 + function config.set(cfg) 39 + config.current = cfg 40 + end 41 + 42 + return config
+9
lua/nivvie/init.lua
··· 1 + local nivvie = {} 2 + 3 + ---@param cfg? nivvie.config 4 + function nivvie.setup(cfg) 5 + cfg = cfg or {} 6 + require('nivvie.config').set(cfg) 7 + end 8 + 9 + return nivvie
+22 -15
plugin/nivvie.lua
··· 15 15 end) 16 16 end 17 17 18 - local sessiondir = vim.g.sessiondir 19 - or vim.fs.joinpath(vim.fn.stdpath 'state', 'sessions') 20 - 21 18 ---@param name? string 22 19 function session.get_path(name) 20 + local sessiondir = require('nivvie.config').get().session_dir 23 21 return vim.fs.joinpath(sessiondir, (name or session.get_uri()) .. '.vim') 24 22 end 25 23 ··· 27 25 function session.save(name) 28 26 local session_file = session.get_path(name) 29 27 28 + local sessiondir = require('nivvie.config').get().session_dir 30 29 vim.fn.mkdir(sessiondir, 'p') 31 30 32 31 vim.api.nvim_cmd({ ··· 45 44 end 46 45 47 46 ---@param name? string 48 - function session.load(name) 47 + function session.restore(name) 49 48 local path = session.get_path(name) 50 49 if vim.uv.fs_stat(path) then 51 50 vim.api.nvim_cmd({ ··· 58 57 59 58 ---- 60 59 61 - function session.shouldload() 60 + function session.isemptysession() 62 61 if session.stdin then 63 62 return false 64 63 end ··· 78 77 return #bufs == 0 79 78 end 80 79 81 - --- only load if necessary 82 - function session.autoload() 83 - if not session.shouldload() then 80 + --- only restore if necessary 81 + function session.autorestore() 82 + if not require('nivvie.config').get().autorestore then 83 + return 84 + end 85 + 86 + if not session.isemptysession() then 84 87 return 85 88 end 86 - session.load() 89 + 90 + session.restore() 87 91 end 88 92 89 93 local group = vim.api.nvim_create_augroup('nivvie', { clear = true }) ··· 91 95 vim.api.nvim_create_autocmd({ 'VimLeavePre' }, { 92 96 group = group, 93 97 callback = function() 98 + if not require('nivvie.config').get().autosave then 99 + return 100 + end 94 101 session.clean() 95 102 session.save() 96 103 end, ··· 108 115 if args.fargs[1] == 'save' then 109 116 session.save(args.fargs[2]) 110 117 return 111 - elseif args.fargs[1] == 'load' then 112 - session.load(args.fargs[2]) 118 + elseif args.fargs[1] == 'restore' then 119 + session.restore(args.fargs[2]) 113 120 return 114 121 elseif args.fargs[1] == 'delete' then 115 122 local path = session.get_path(args.fargs[2]) ··· 121 128 complete = function() 122 129 return { 123 130 'save', 124 - 'load', 131 + 'restore', 125 132 'delete', 126 133 } 127 134 end, ··· 132 139 133 140 if vim.v.vim_did_enter > 0 then 134 141 vim.schedule(function() 135 - session.autoload() 142 + session.autorestore() 136 143 end) 137 144 return 138 145 end ··· 141 148 group = group, 142 149 callback = function() 143 150 vim.schedule(function() 144 - session.autoload() 151 + session.autorestore() 145 152 end) 146 153 end, 147 154 }) 148 155 149 - return session 156 + return