aboutsummaryrefslogtreecommitdiff
path: root/lua
diff options
context:
space:
mode:
authorDefaultGen <gravel.justness270@slmail.me>2024-06-09 03:48:26 -0400
committerDefaultGen <gravel.justness270@slmail.me>2024-06-09 03:48:26 -0400
commit38738a6527d2d571d149bda36e74bdb9e2f0743c (patch)
treee553cb718a2ac5b80a64a38da3fe70b9d1752137 /lua
parent15389262bfb4ba280fc1aa1b0dd688947aa00f80 (diff)
downloadsimple-denote.nvim-38738a6527d2d571d149bda36e74bdb9e2f0743c.tar.gz
Added cursed search function
Diffstat (limited to 'lua')
-rw-r--r--lua/simple-denote.lua7
-rw-r--r--lua/simple-denote/api.lua1
-rwxr-xr-xlua/simple-denote/scripts/search.sh20
-rw-r--r--lua/simple-denote/search.lua40
4 files changed, 67 insertions, 1 deletions
diff --git a/lua/simple-denote.lua b/lua/simple-denote.lua
index 3f199fe..12ba1f6 100644
--- a/lua/simple-denote.lua
+++ b/lua/simple-denote.lua
@@ -1,7 +1,8 @@
local M = {}
-local api = require("simple-denote.api")
local config = require("simple-denote.config")
+local api = require("simple-denote.api")
+local search = require ("simple-denote.search")
function M.load_cmd(options)
vim.api.nvim_create_user_command("Denote", function(opts)
@@ -13,6 +14,10 @@ function M.load_cmd(options)
api.tag(options)
elseif opts.fargs[1] == "signature" then
api.signature(options)
+ elseif opts.fargs[1] == "search" then
+ search.search(options)
+ elseif opts.fargs[1] == "test" then
+ search.test()
else
error("Unsupported operation " .. opts.fargs[1])
end
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<prefix>\/.*\/)?(?P<datetime>\d{8}T\d{6})(==(?P<sig>[a-z0-9=]+))?(--(?P<title>[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