From 4735cef711a386912a322a4943cce40a9fa50ad7 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Fri, 26 Nov 2021 02:03:10 +0100 Subject: [PATCH] lua-native sorting of dialy, weekly notes - thx to plenary.scan_dir()!!! --- BACKLOG.md | 6 +++++- README.md | 6 ------ doc/telekasten.txt | 6 +++--- lua/telekasten.lua | 36 ++++++++++++++++++++++++++++++++---- 4 files changed, 40 insertions(+), 14 deletions(-) diff --git a/BACKLOG.md b/BACKLOG.md index 90b3c82..2f0ebba 100644 --- a/BACKLOG.md +++ b/BACKLOG.md @@ -1,9 +1,13 @@ # Backlog -- [ ] implement sorting of the file list that works as well as the `daily_finder.sh` we abandoned +- [ ]: (silly idea) check if we can paste imgs into nvim + + - [ ] yt video ## Dones +- [x] implement sorting of the file list that works as well as the `daily_finder.sh` we abandoned + - `plenary.scan_dir()` to the rescue! - [x] vimhelp - [x] Honor day, month, year etc when creating a note via calendar! - [x] awesome calendar support diff --git a/README.md b/README.md index d50d035..bc2377d 100644 --- a/README.md +++ b/README.md @@ -35,12 +35,6 @@ Since this plugin uses [telescope.nvim](https://github.com/nvim-telescope/telesc [Neovim (v0.5.1)](https://github.com/neovim/neovim/releases/tag/v0.5.1) or the latest neovim nighly commit is required for `telescope.nvim` to work. -#### Ripgrep -For proper sort order of daily notes, the `rg` executable ([Ripgrep](https://github.com/BurntSushi/ripgrep)) is required and needs to be installed so that nvim can find it. So make sure it's in your path. - -If rg isn't found at `setup()` time, it will not be used. In that case, the sort order of daily and weekly notes are likely to be reversed or total garbage. I do accept pull requests, though, for a lua implementation 😁! - -If you can't use `rg`, I recommend using `goto_today()` and `goto_thisweek()` instead of `find_daily_notes()` and `find_weekly_notes()`, as this pre-fills the search field, which makes the results list look a bit more sane. #### calendar-vim Plugin (optional) diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 15aac1b..c945c3b 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -264,8 +264,8 @@ telekasten.find_notes()~ *telekasten.find_daily_notes()* telekasten.find_daily_notes()~ - Find daily notes by date, via Telescope. File names are sorted by file - creation time, most recent file first. + Find daily notes by date, via Telescope. File names are sorted by file name, + most recent file first. If today's daily note is not present, will be created if `dailies_create_nonexisting` is set to `true`. @@ -302,7 +302,7 @@ telekasten.goto_today()~ *telekasten.find_weekly_notes()* telekasten.find_weekly_notes()~ Find weekly notes by week, via Telescope. File names are sorted by file - creation time, most recent file first. + name, most recent file first. If this week's daily note is not present, will be created if `weeklies_create_nonexisting` is set to `true`. diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 409a23f..d08d8e2 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -1,6 +1,10 @@ local builtin = require("telescope.builtin") local actions = require("telescope.actions") local action_state = require("telescope.actions.state") +local pickers = require("telescope.pickers") +local finders = require("telescope.finders") +local conf = require("telescope.config").values +local scan = require("plenary.scandir") -- declare locals for the nvim api stuff to avoid more lsp warnings local vim = vim @@ -146,6 +150,24 @@ local path_to_linkname = function(p) return fn end +local order_numeric = function(a, b) + return a > b +end + +local find_files_sorted = function(opts) + opts = opts or {} + + local file_list = scan.scan_dir(opts.cwd, {}) + table.sort(file_list, order_numeric) + pickers.new(opts, { + finder = finders.new_table({ + results = file_list, + }), + sorter = conf.generic_sorter(opts), + previewer = conf.file_previewer(opts), + }):find() +end + -- -- FindDailyNotes: -- --------------- @@ -164,7 +186,8 @@ local FindDailyNotes = function(opts) create_note_from_template(today, fname, note_type_templates.daily) end - builtin.find_files({ + -- builtin.find_files({ + find_files_sorted({ prompt_title = "Find daily note", cwd = ZkCfg.dailies, find_command = ZkCfg.find_command, @@ -189,7 +212,8 @@ local FindWeeklyNotes = function(opts) create_note_from_template(title, fname, note_type_templates.weekly) end - builtin.find_files({ + -- builtin.find_files({ + find_files_sorted({ prompt_title = "Find weekly note", cwd = ZkCfg.weeklies, find_command = ZkCfg.find_command, @@ -280,7 +304,8 @@ local GotoToday = function(opts) create_note_from_template(word, fname, note_type_templates.daily, opts) end - builtin.find_files({ + -- builtin.find_files({ + find_files_sorted({ prompt_title = "Goto day", cwd = ZkCfg.home, default_text = word, @@ -418,7 +443,8 @@ local GotoThisWeek = function(opts) create_note_from_template(title, fname, note_type_templates.weekly) end - builtin.find_files({ + -- builtin.find_files({ + find_files_sorted({ prompt_title = "Goto this week:", cwd = ZkCfg.weeklies, default_text = title, @@ -519,6 +545,8 @@ local Setup = function(cfg) ZkCfg[k] = v end end + + -- TODO: this is obsolete: if vim.fn.executable("rg") then ZkCfg.find_command = { "rg", "--files", "--sortr", "created" } else