Skip to content

Commit eae91b8

Browse files
author
jghauser
committed
feat: initially sort telescope entries by time-added
1 parent 572e741 commit eae91b8

File tree

4 files changed

+55
-9
lines changed

4 files changed

+55
-9
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,12 @@ init_filetypes = { "markdown", "norg", "yaml" },
283283
-- Configuration of the search module.
284284
["search"] = {
285285

286-
-- Wether to enable line wrap in the telescope previewer.
286+
-- Whether to enable line wrap in the telescope previewer.
287287
wrap = true,
288288

289+
-- Whether to initially sort entries by time-added.
290+
initial_sort_by_time_added = true,
291+
289292
-- What keys to search for matches.
290293
search_keys = { "author", "editor", "year", "title", "tags" },
291294

lua/papis/config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ local default_config = {
111111
},
112112
["search"] = {
113113
wrap = true,
114+
initial_sort_by_time_added = true,
114115
search_keys = { "author", "editor", "year", "title", "tags" }, -- also possible: "type"
115116
preview_format = {
116117
{ "author", "%s", "PapisPreviewAuthor" },

lua/papis/search/data.lua

+21-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@ if not db then
1111
end
1212
local config = require("papis.config")
1313
local search_keys = config["search"]["search_keys"]
14-
local preview_format = config["search"]["preview_format"]
1514
local results_format = config["search"]["results_format"]
1615
local utils = require("papis.utils")
17-
local required_db_keys = utils:get_required_db_keys({ search_keys, preview_format, results_format })
1816

1917
---Creates a string that is used to search among entries (not displayed)
2018
---@param entry table #A papis entry
@@ -50,13 +48,29 @@ local function format_search_string(entry)
5048
return search_string
5149
end
5250

51+
---Creates a timestamp (in secs since epoch), which is used for initial sorting
52+
---@param entry table #A papis entry
53+
---@return integer #The timestamp (date when entry was added in secs since epoch or 1 if missing)
54+
local function make_timestamp(entry)
55+
local timestamp = entry["time_added"]
56+
if timestamp then
57+
local year, month, day, hour, min, sec = timestamp:match("(%d+)-(%d+)-(%d+)-(%d+):(%d+):(%d+)")
58+
local t = { year = year, month = month, day = day, hour = hour, min = min, sec = sec }
59+
timestamp = os.time(t)
60+
else
61+
timestamp = 1
62+
end
63+
return timestamp
64+
end
65+
5366
---Initialises all the tables and methods used by the papis.nvim search module
5467
local function init_tbl()
5568
db.search = db:tbl("search", {
5669
id = true,
5770
items = { "luatable" },
5871
displayer_tbl = { "luatable" },
5972
search_string = { "text" },
73+
timestamp = { "integer" },
6074
entry = {
6175
type = "integer",
6276
unique = true,
@@ -76,6 +90,7 @@ local function init_tbl()
7690
"items",
7791
"displayer_tbl",
7892
"search_string",
93+
"timestamp",
7994
},
8095
})
8196
end
@@ -84,8 +99,7 @@ local function init_tbl()
8499
---@param id number #The id of a papis entry
85100
function db.search:update(id)
86101
local entry = db["data"]:__get({
87-
where = { id = id },
88-
select = required_db_keys,
102+
where = { id = id }
89103
})[1]
90104
local display_strings = utils:format_display_strings(entry, results_format)
91105
local search_string = format_search_string(entry)
@@ -98,12 +112,15 @@ local function init_tbl()
98112
end
99113
table.insert(items, { remaining = true })
100114

115+
local timestamp = make_timestamp(entry)
116+
101117
self:__update({
102118
where = { id = id },
103119
set = {
104120
displayer_tbl = displayer_tbl,
105121
items = items,
106122
search_string = search_string,
123+
timestamp = timestamp,
107124
entry = id,
108125
},
109126
})

lua/telescope/_extensions/papis.lua

+29-4
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ if not db then
2121
return nil
2222
end
2323

24-
local wrap, preview_format, required_db_keys
25-
2624
---Gets the cite format for the filetype
2725
---@return string #The cite format for the filetype (or fallback if undefined)
2826
local function parse_format_string()
@@ -33,24 +31,49 @@ local function parse_format_string()
3331
return cite_format
3432
end
3533

34+
local wrap, preview_format, required_db_keys, initial_sort_by_time_added
35+
3636
---Defines the papis.nvim telescope picker
3737
---@param opts table #Options for the papis picker
3838
local function papis_picker(opts)
3939
opts = opts or {}
4040

4141
local results = db.data:get(nil, required_db_keys)
4242
local format_string = parse_format_string()
43+
44+
-- amend the generic_sorter so that we can change initial sorting
45+
local generic_sorter = telescope_config.generic_sorter(opts)
46+
local papis_sorter = {}
47+
setmetatable(papis_sorter, { __index = generic_sorter })
48+
49+
if initial_sort_by_time_added then
50+
---@param prompt string
51+
---@param line string
52+
---@return number score number from 1 to 0. lower the number the better. -1 will filter out the entry though.
53+
function papis_sorter:scoring_function(prompt, line, entry)
54+
local score = generic_sorter.scoring_function(self, prompt, line)
55+
if #prompt == 0 then
56+
local min_timestamp = 0
57+
local max_timestamp = os.time()
58+
local timestamp = entry["timestamp"]
59+
60+
score = 1 - (timestamp - min_timestamp) / (max_timestamp - min_timestamp)
61+
end
62+
return score
63+
end
64+
end
65+
4366
pickers
4467
.new(opts, {
4568
prompt_title = "Papis References",
4669
finder = finders.new_table({
4770
results = results,
4871
entry_maker = function(entry)
4972
local entry_pre_calc = db["search"]:get(entry["id"])[1]
73+
local timestamp = entry_pre_calc["timestamp"]
5074
local items = entry_pre_calc["items"]
5175

5276
local displayer_tbl = entry_pre_calc["displayer_tbl"]
53-
5477
local displayer = entry_display.create({
5578
separator = "",
5679
items = items,
@@ -65,6 +88,7 @@ local function papis_picker(opts)
6588
value = search_string,
6689
ordinal = search_string,
6790
display = make_display,
91+
timestamp = timestamp,
6892
id = entry,
6993
}
7094
end,
@@ -91,7 +115,7 @@ local function papis_picker(opts)
91115
vim.api.nvim_set_option_value("wrap", wrap, { win = status.preview_win })
92116
end,
93117
}),
94-
sorter = telescope_config.generic_sorter(opts),
118+
sorter = papis_sorter,
95119
attach_mappings = function(_, map)
96120
actions.select_default:replace(papis_actions.ref_insert(format_string))
97121
map("i", "<c-o>f", papis_actions.open_file(), { desc = "Open file" })
@@ -112,6 +136,7 @@ end
112136
return telescope.register_extension({
113137
setup = function(opts)
114138
wrap = opts["wrap"]
139+
initial_sort_by_time_added = opts["initial_sort_by_time_added"]
115140
preview_format = opts["preview_format"]
116141
local search_keys = opts["search_keys"]
117142
local results_format = opts["results_format"]

0 commit comments

Comments
 (0)