🌱 tiny neovim plugin keeping your session safe
1
fork

Configure Feed

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

refactor: move main fn to lua module

- allow access to core api

robin f62ae577 cb7a457f

+94 -97
+86 -1
lua/nivvie/init.lua
··· 1 1 local nivvie = {} 2 2 3 - ---@param cfg? nivvie.config 3 + ---@param cfg? nivvie.config.proto 4 4 function nivvie.setup(cfg) 5 5 cfg = cfg or {} 6 6 require('nivvie.config').set(cfg) 7 + end 8 + 9 + nivvie.stdin = false 10 + 11 + function nivvie.get_uri() 12 + return string.gsub(vim.fn.getcwd(), '[^a-zA-Z0-9_.-]', function(s) 13 + return '<' .. vim.fn.char2nr(s) 14 + end) 15 + end 16 + 17 + ---@param name? string 18 + function nivvie.get_path(name) 19 + local sessiondir = require('nivvie.config').get().session_dir 20 + return vim.fs.joinpath(sessiondir, (name or nivvie.get_uri()) .. '.vim') 21 + end 22 + 23 + ---@param name? string 24 + function nivvie.save(name) 25 + local session_file = nivvie.get_path(name) 26 + 27 + local sessiondir = require('nivvie.config').get().session_dir 28 + vim.fn.mkdir(sessiondir, 'p') 29 + 30 + vim.api.nvim_cmd({ 31 + cmd = 'mksession', 32 + bang = true, 33 + args = { session_file }, 34 + }, {}) 35 + end 36 + 37 + function nivvie.clean() 38 + vim.iter(ipairs(vim.api.nvim_list_bufs())):each(function(_, bufnr) 39 + if vim.bo[bufnr].buftype ~= '' then 40 + vim.api.nvim_buf_delete(bufnr, { force = true }) 41 + end 42 + end) 43 + end 44 + 45 + ---@param name? string 46 + function nivvie.restore(name) 47 + local path = nivvie.get_path(name) 48 + if vim.uv.fs_stat(path) then 49 + vim.api.nvim_cmd({ 50 + cmd = 'source', 51 + args = { path }, 52 + }, {}) 53 + vim.api.nvim_exec_autocmds('SessionLoadPost', {}) 54 + end 55 + end 56 + 57 + ---- 58 + 59 + function nivvie.isemptysession() 60 + if nivvie.stdin then 61 + return false 62 + end 63 + 64 + if vim.fn.argc() > 0 then 65 + return false 66 + end 67 + 68 + local bufs = vim.api.nvim_list_bufs() 69 + bufs = vim 70 + .iter(bufs) 71 + :filter(function(buf) 72 + return vim.api.nvim_buf_is_valid(buf) 73 + and vim.bo[buf].buftype == '' 74 + and #vim.api.nvim_buf_get_name(buf) > 0 75 + end) 76 + :totable() 77 + 78 + return #bufs == 0 79 + end 80 + 81 + --- only restore if necessary 82 + function nivvie.autorestore() 83 + if not require('nivvie.config').get().autorestore then 84 + return 85 + end 86 + 87 + if not nivvie.isemptysession() then 88 + return 89 + end 90 + 91 + nivvie.restore() 7 92 end 8 93 9 94 return nivvie
+8 -96
plugin/nivvie.lua
··· 4 4 5 5 vim.g.loaded_session = true 6 6 7 - local session = {} 8 - 9 - ---@private 10 - session.stdin = false 11 - 12 - function session.get_uri() 13 - return string.gsub(vim.fn.getcwd(), '[^a-zA-Z0-9_.-]', function(s) 14 - return '<' .. vim.fn.char2nr(s) 15 - end) 16 - end 17 - 18 - ---@param name? string 19 - function session.get_path(name) 20 - local sessiondir = require('nivvie.config').get().session_dir 21 - return vim.fs.joinpath(sessiondir, (name or session.get_uri()) .. '.vim') 22 - end 23 - 24 - ---@param name? string 25 - function session.save(name) 26 - local session_file = session.get_path(name) 27 - 28 - local sessiondir = require('nivvie.config').get().session_dir 29 - vim.fn.mkdir(sessiondir, 'p') 30 - 31 - vim.api.nvim_cmd({ 32 - cmd = 'mksession', 33 - bang = true, 34 - args = { session_file }, 35 - }, {}) 36 - end 37 - 38 - function session.clean() 39 - vim.iter(ipairs(vim.api.nvim_list_bufs())):each(function(_, bufnr) 40 - if vim.bo[bufnr].buftype ~= '' then 41 - vim.api.nvim_buf_delete(bufnr, { force = true }) 42 - end 43 - end) 44 - end 45 - 46 - ---@param name? string 47 - function session.restore(name) 48 - local path = session.get_path(name) 49 - if vim.uv.fs_stat(path) then 50 - vim.api.nvim_cmd({ 51 - cmd = 'source', 52 - args = { path }, 53 - }, {}) 54 - vim.api.nvim_exec_autocmds('SessionLoadPost', {}) 55 - end 56 - end 57 - 58 - ---- 59 - 60 - function session.isemptysession() 61 - if session.stdin then 62 - return false 63 - end 64 - 65 - if vim.fn.argc() > 0 then 66 - return false 67 - end 68 - 69 - local bufs = vim.api.nvim_list_bufs() 70 - bufs = vim 71 - .iter(bufs) 72 - :filter(function(buf) 73 - return vim.api.nvim_buf_is_valid(buf) 74 - and vim.bo[buf].buftype == '' 75 - and #vim.api.nvim_buf_get_name(buf) > 0 76 - end) 77 - :totable() 78 - 79 - return #bufs == 0 80 - end 81 - 82 - --- only restore if necessary 83 - function session.autorestore() 84 - if not require('nivvie.config').get().autorestore then 85 - return 86 - end 87 - 88 - if not session.isemptysession() then 89 - return 90 - end 91 - 92 - session.restore() 93 - end 94 - 95 7 local group = vim.api.nvim_create_augroup('nivvie', { clear = true }) 96 8 97 9 vim.api.nvim_create_autocmd({ 'VimLeavePre' }, { ··· 100 12 if not require('nivvie.config').get().autosave then 101 13 return 102 14 end 103 - session.clean() 104 - session.save() 15 + require('nivvie').clean() 16 + require('nivvie').save() 105 17 end, 106 18 }) 107 19 108 20 vim.api.nvim_create_autocmd({ 'StdinReadPost' }, { 109 21 group = group, 110 22 callback = function() 111 - session.stdin = true 23 + require('nivvie').stdin = true 112 24 end, 113 25 }) 114 26 115 27 vim.schedule(function() 116 28 vim.api.nvim_create_user_command('Nivvie', function(args) 117 29 if args.fargs[1] == 'save' then 118 - session.save(args.fargs[2]) 30 + require('nivvie').save(args.fargs[2]) 119 31 return 120 32 elseif args.fargs[1] == 'restore' then 121 - session.restore(args.fargs[2]) 33 + require('nivvie').restore(args.fargs[2]) 122 34 return 123 35 elseif args.fargs[1] == 'delete' then 124 - local path = session.get_path(args.fargs[2]) 36 + local path = require('nivvie').get_path(args.fargs[2]) 125 37 if vim.uv.fs_stat(path) then 126 38 vim.uv.fs_unlink(path) 127 39 end ··· 142 54 143 55 if vim.v.vim_did_enter > 0 then 144 56 vim.schedule(function() 145 - session.autorestore() 57 + require('nivvie').autorestore() 146 58 end) 147 59 return 148 60 end ··· 151 63 group = group, 152 64 callback = function() 153 65 vim.schedule(function() 154 - session.autorestore() 66 + require('nivvie').autorestore() 155 67 end) 156 68 end, 157 69 })