summaryrefslogtreecommitdiff
path: root/config/essentials/vis/fzf-mru.lua
diff options
context:
space:
mode:
authorRaymaekers Luca <raymaekers.luca@gmail.com>2024-06-22 02:05:44 +0200
committerRaymaekers Luca <raymaekers.luca@gmail.com>2024-06-22 02:05:44 +0200
commit36d2972c60ec86b873fa496d1f5ea95cf748cf49 (patch)
treea6d6750fa17c2964cd241afa8e963cac6106b390 /config/essentials/vis/fzf-mru.lua
parent4914b43f642e2772a140a8f9b1f26b4e555ed88b (diff)
parent32256e087aaf7744348a5ba33e802d5c8d9d97dd (diff)
Merge branch 'main' of db:dotfiles
Diffstat (limited to 'config/essentials/vis/fzf-mru.lua')
-rw-r--r--config/essentials/vis/fzf-mru.lua75
1 files changed, 75 insertions, 0 deletions
diff --git a/config/essentials/vis/fzf-mru.lua b/config/essentials/vis/fzf-mru.lua
new file mode 100644
index 0000000..6c2510d
--- /dev/null
+++ b/config/essentials/vis/fzf-mru.lua
@@ -0,0 +1,75 @@
+local module = {}
+module.fzfmru_filepath = os.getenv("XDG_CACHE_HOME") .. "/vis-fzf-mru"
+module.fzfmru_path = "fzf"
+module.fzfmru_args = "--height=40%"
+module.fzfmru_history = 20
+
+local function read_mru()
+ local mru = {}
+ local f = io.open(module.fzfmru_filepath)
+ if f == nil then return end
+ for line in f:lines() do table.insert(mru, line) end
+ f:close()
+
+ return mru
+end
+
+local function write_mru(win)
+ local file_path = win.file.path
+ local mru = read_mru()
+
+ -- check if mru data exists
+ if mru == nil then mru = {} end
+ -- check if we opened any file
+ if file_path == nil then return end
+ -- check duplicate
+ if file_path == mru[1] then return end
+
+ local f = io.open(module.fzfmru_filepath, "w+")
+ if f == nil then return end
+
+ table.insert(mru, 1, file_path)
+
+ for i, k in ipairs(mru) do
+ if i > module.fzfmru_history then break end
+ if i == 1 or k ~= file_path then
+ f:write(string.format("%s\n", k))
+ end
+ end
+
+ f:close()
+end
+
+vis.events.subscribe(vis.events.WIN_OPEN, write_mru)
+
+vis:command_register("fzfmru", function(argv)
+ local command = "cat " .. module.fzfmru_filepath .. " | " ..
+ module.fzfmru_path .. " " .. module.fzfmru_args .. " " ..
+ table.concat(argv, " ")
+
+ local file = io.popen(command)
+ local output = file:read()
+ local _, _, status = file:close()
+
+ if status == 0 then
+ vis:command(string.format("e '%s'", output))
+ elseif status == 1 then
+ vis:info(string.format(
+ "fzf-open: No match. Command %s exited with return value %i.",
+ command, status))
+ elseif status == 2 then
+ vis:info(string.format(
+ "fzf-open: Error. Command %s exited with return value %i.",
+ command, status))
+ elseif status ~= 130 then
+ vis:info(string.format(
+ "fzf-open: Unknown exit status %i. command %s exited with return value %i",
+ status, command, status, status))
+ end
+
+ vis:redraw()
+
+ return true
+end)
+
+return module