diff options
author | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-06-22 02:05:44 +0200 |
---|---|---|
committer | Raymaekers Luca <raymaekers.luca@gmail.com> | 2024-06-22 02:05:44 +0200 |
commit | 36d2972c60ec86b873fa496d1f5ea95cf748cf49 (patch) | |
tree | a6d6750fa17c2964cd241afa8e963cac6106b390 /config/essentials/vis/cursors.lua | |
parent | 4914b43f642e2772a140a8f9b1f26b4e555ed88b (diff) | |
parent | 32256e087aaf7744348a5ba33e802d5c8d9d97dd (diff) |
Merge branch 'main' of db:dotfiles
Diffstat (limited to 'config/essentials/vis/cursors.lua')
-rw-r--r-- | config/essentials/vis/cursors.lua | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/config/essentials/vis/cursors.lua b/config/essentials/vis/cursors.lua new file mode 100644 index 0000000..5b3d43b --- /dev/null +++ b/config/essentials/vis/cursors.lua @@ -0,0 +1,105 @@ +local M = {} +local cursors = {} +local files = {} + +-- default maxsize +M.maxsize = 1000 + +-- get the default system cache directory +local function get_default_cache_path() + local HOME = os.getenv('HOME') + local XDG_CACHE_HOME = os.getenv('XDG_CACHE_HOME') + local BASE = XDG_CACHE_HOME or HOME + return BASE .. '/.vis-cursors' +end + +-- default save path +M.path = get_default_cache_path() + +local function read_files() + + -- read file + local file = io.open(M.path) + if file == nil then return end + + files = {} + + -- read positions per file path + for line in file:lines() do + for path, pos in string.gmatch(line, '(.+)[,%s](%d+)') do + cursors[path] = pos + table.insert(files, path) + end + end + + file:close() +end + +-- read cursors from file on init +local function on_init() read_files() end + +-- apply cursor pos on win open +local function on_win_open(win) + + if win.file == nil or win.file.path == nil then return end + + -- init cursor path if nil + local pos = cursors[win.file.path] + if pos == nil then + cursors[win.file.path] = win.selection.pos + return + end + + -- set current cursor + win.selection.pos = tonumber(pos) + + -- center view around cursor + vis:feedkeys("zz") +end + +-- set cursor pos on close +local function on_win_close(win) + + if win.file == nil or win.file.path == nil then return end + + -- re-read files in case they've changed + read_files() + + -- remove old occurences of current path + for i, path in ipairs(files) do + if path == win.file.path then table.remove(files, i) end + end + + -- ignore files with cursor at the beginning + if win.selection.pos == 0 then return end + + -- insert current path to top of files + table.insert(files, 1, win.file.path) + + -- set cursor pos for current file path + cursors[win.file.path] = win.selection.pos +end + +-- write cursors to file on quit +local function on_quit() + + local file = io.open(M.path, 'w+') + if file == nil then return end + + -- buffer cursors string + local buffer = {} + for _, path in ipairs(files) do + table.insert(buffer, string.format('%s,%d', path, cursors[path])) + if M.maxsize and #buffer >= M.maxsize then break end + end + local output = table.concat(buffer, '\n') + file:write(output) + file:close() +end + +vis.events.subscribe(vis.events.INIT, on_init) +vis.events.subscribe(vis.events.WIN_OPEN, on_win_open) +vis.events.subscribe(vis.events.WIN_CLOSE, on_win_close) +vis.events.subscribe(vis.events.QUIT, on_quit) + +return M |