From 38738a6527d2d571d149bda36e74bdb9e2f0743c Mon Sep 17 00:00:00 2001 From: DefaultGen Date: Sun, 9 Jun 2024 03:48:26 -0400 Subject: Added cursed search function --- lua/simple-denote/api.lua | 1 + lua/simple-denote/scripts/search.sh | 20 +++++++++++++++++++ lua/simple-denote/search.lua | 40 +++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100755 lua/simple-denote/scripts/search.sh create mode 100644 lua/simple-denote/search.lua (limited to 'lua/simple-denote') diff --git a/lua/simple-denote/api.lua b/lua/simple-denote/api.lua index 05d645a..36ab8c8 100644 --- a/lua/simple-denote/api.lua +++ b/lua/simple-denote/api.lua @@ -1,6 +1,7 @@ local M = {} local internal = require("simple-denote.internal") +local search = require ("simple-denote.search") ---@param options table ---@param title string|nil diff --git a/lua/simple-denote/scripts/search.sh b/lua/simple-denote/scripts/search.sh new file mode 100755 index 0000000..799e081 --- /dev/null +++ b/lua/simple-denote/scripts/search.sh @@ -0,0 +1,20 @@ +#!/usr/bin/sh +fd . $1 |\ +sd '(?P\/.*\/)?(?P\d{8}T\d{6})(==(?P[a-z0-9=]+))?(--(?P[a-z0-9-]+))(__(?P<tags>[a-z0-9_]+))?(?P<ext>\.\w+)' '$prefix|$datetime|$sig|$title|$tags|$ext' |\ +sd '[\=\-\_]' ' ' |\ +sd '(\d{4})(\d{2})(\d{2})(T\d{6})' '$1-$2-$3|$4' |\ +sd '\|\|' '|.|'|\ +column -t -s "|" -N Prefix,Date,Time,Sig,Title,Tags,Ext |\ +fzf --delimiter='\s{2,}' --nth=1..3 --with-nth=2,5,6 --header-lines=1 --reverse --info=hidden --no-separator --preview-window=bottom \ +--preview=' +sig={4}; sig=$(echo $sig | sd "\W" "" | sd " " "="); if [ ! -z ${sig} ] ;then sig="==${sig}"; fi; \ +title={5}; title=$(echo $title | sd " " "-"); if [ ! -z ${title} ] ;then title="--${title}"; fi; \ +tags={6}; tags=$(echo $tags | sd " " "_" | sd "\." ""); if [ ! -z ${tags} ] ;then tags="__${tags}"; fi; \ +ext={7}; \ +echo {1}\|{2}\|{3}\|{4}\|{5}\|{6}\|{7} | sd "(?P<prefix>\/.*\/)?\|(?P<d1>\d\d\d\d)-(?P<d2>\d\d)-(?P<d3>\d\d)\|(?P<time>T\d\d\d\d\d\d)\|.*" "\$prefix\$d1\$d2\$d3\$time$sig$title$tags$ext" | xargs cat' \ +--bind='enter:execute( \ +sig={4}; sig=$(echo $sig | sd "\W" "" | sd " " "="); if [ ! -z ${sig} ] ;then sig="==${sig}"; fi; \ +title={5}; title=$(echo $title | sd " " "-"); if [ ! -z ${title} ] ;then title="--${title}"; fi; \ +tags={6}; tags=$(echo $tags | sd " " "_" | sd "\." ""); if [ ! -z ${tags} ] ;then tags="__${tags}"; fi; \ +ext={7}; \ +echo {1}\|{2}\|{3}\|{4}\|{5}\|{6}\|{7} | sd "(?P<prefix>\/.*\/)?\|(?P<d1>\d\d\d\d)-(?P<d2>\d\d)-(?P<d3>\d\d)\|(?P<time>T\d\d\d\d\d\d)\|.*" "\$prefix\$d1\$d2\$d3\$time$sig$title$tags$ext" | cat)+abort' diff --git a/lua/simple-denote/search.lua b/lua/simple-denote/search.lua new file mode 100644 index 0000000..eab25dd --- /dev/null +++ b/lua/simple-denote/search.lua @@ -0,0 +1,40 @@ +local M = {} + +function M.search(options) + local width = vim.o.columns - 8 + local height = vim.o.lines - 8 + + vim.api.nvim_open_win( + vim.api.nvim_create_buf(false, true), + true, + { + relative = 'editor', + style = 'minimal', + border = 'rounded', + noautocmd = true, + width = width, + height = height, + col = math.min((vim.o.columns - width) / 2), + row = math.min((vim.o.lines - height) / 2 - 1), + } + ) + local file = vim.fn.tempname() + -- This feels like the wrong way to call a script from a relative path + local lua_file_path = debug.getinfo(1, "S").source:sub(2) + local lua_file_dir = vim.fn.fnamemodify(lua_file_path, ":h") + local script_path = lua_file_dir .. "/scripts/search.sh" + if not script_path then + error("Script not found") + end + vim.api.nvim_command('startinsert') + vim.fn.termopen(script_path .. " " .. options.dir .. " > " .. file, { + on_exit = function(jobid, data, event) + vim.api.nvim_command('bdelete!') + local f = io.open(file, 'r') + local stdout = f:read('*all') + f:close() + os.remove(file) + vim.api.nvim_command('edit ' .. stdout) + end}) +end +return M -- cgit v1.2.3