Skip to content

Commit

Permalink
Merge pull request #70 from jghauser/change-telescope-sort-order
Browse files Browse the repository at this point in the history
feat: initially sort telescope entries by time-added
  • Loading branch information
jghauser authored Jun 12, 2024
2 parents 572e741 + 1092bf4 commit 4afaffb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 10 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,12 @@ init_filetypes = { "markdown", "norg", "yaml" },
-- Configuration of the search module.
["search"] = {

-- Wether to enable line wrap in the telescope previewer.
-- Whether to enable line wrap in the telescope previewer.
wrap = true,

-- Whether to initially sort entries by time-added.
initial_sort_by_time_added = true,

-- What keys to search for matches.
search_keys = { "author", "editor", "year", "title", "tags" },

Expand Down
5 changes: 4 additions & 1 deletion doc/papis.txt
Original file line number Diff line number Diff line change
Expand Up @@ -340,9 +340,12 @@ All configuration options (with defaults) ~
-- Configuration of the search module.
["search"] = {

-- Wether to enable line wrap in the telescope previewer.
-- Whether to enable line wrap in the telescope previewer.
wrap = true,

-- Whether to initially sort entries by time-added.
initial_sort_by_time_added = true,

-- What keys to search for matches.
search_keys = { "author", "editor", "year", "title", "tags" },

Expand Down
1 change: 1 addition & 0 deletions lua/papis/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ local default_config = {
},
["search"] = {
wrap = true,
initial_sort_by_time_added = true,
search_keys = { "author", "editor", "year", "title", "tags" }, -- also possible: "type"
preview_format = {
{ "author", "%s", "PapisPreviewAuthor" },
Expand Down
25 changes: 21 additions & 4 deletions lua/papis/search/data.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ if not db then
end
local config = require("papis.config")
local search_keys = config["search"]["search_keys"]
local preview_format = config["search"]["preview_format"]
local results_format = config["search"]["results_format"]
local utils = require("papis.utils")
local required_db_keys = utils:get_required_db_keys({ search_keys, preview_format, results_format })

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

---Creates a timestamp (in secs since epoch), which is used for initial sorting
---@param entry table #A papis entry
---@return integer #The timestamp (date when entry was added in secs since epoch or 1 if missing)
local function make_timestamp(entry)
local timestamp = entry["time_added"]
if timestamp then
local year, month, day, hour, min, sec = timestamp:match("(%d+)-(%d+)-(%d+)-(%d+):(%d+):(%d+)")
local t = { year = year, month = month, day = day, hour = hour, min = min, sec = sec }
timestamp = os.time(t)
else
timestamp = 1
end
return timestamp
end

---Initialises all the tables and methods used by the papis.nvim search module
local function init_tbl()
db.search = db:tbl("search", {
id = true,
items = { "luatable" },
displayer_tbl = { "luatable" },
search_string = { "text" },
timestamp = { "integer" },
entry = {
type = "integer",
unique = true,
Expand All @@ -76,6 +90,7 @@ local function init_tbl()
"items",
"displayer_tbl",
"search_string",
"timestamp",
},
})
end
Expand All @@ -84,8 +99,7 @@ local function init_tbl()
---@param id number #The id of a papis entry
function db.search:update(id)
local entry = db["data"]:__get({
where = { id = id },
select = required_db_keys,
where = { id = id }
})[1]
local display_strings = utils:format_display_strings(entry, results_format)
local search_string = format_search_string(entry)
Expand All @@ -98,12 +112,15 @@ local function init_tbl()
end
table.insert(items, { remaining = true })

local timestamp = make_timestamp(entry)

self:__update({
where = { id = id },
set = {
displayer_tbl = displayer_tbl,
items = items,
search_string = search_string,
timestamp = timestamp,
entry = id,
},
})
Expand Down
33 changes: 29 additions & 4 deletions lua/telescope/_extensions/papis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ if not db then
return nil
end

local wrap, preview_format, required_db_keys

---Gets the cite format for the filetype
---@return string #The cite format for the filetype (or fallback if undefined)
local function parse_format_string()
Expand All @@ -33,24 +31,49 @@ local function parse_format_string()
return cite_format
end

local wrap, preview_format, required_db_keys, initial_sort_by_time_added

---Defines the papis.nvim telescope picker
---@param opts table #Options for the papis picker
local function papis_picker(opts)
opts = opts or {}

local results = db.data:get(nil, required_db_keys)
local format_string = parse_format_string()

-- amend the generic_sorter so that we can change initial sorting
local generic_sorter = telescope_config.generic_sorter(opts)
local papis_sorter = {}
setmetatable(papis_sorter, { __index = generic_sorter })

if initial_sort_by_time_added then
---@param prompt string
---@param line string
---@return number score number from 1 to 0. lower the number the better. -1 will filter out the entry though.
function papis_sorter:scoring_function(prompt, line, entry)
local score = generic_sorter.scoring_function(self, prompt, line)
if #prompt == 0 then
local min_timestamp = 0
local max_timestamp = os.time()
local timestamp = entry["timestamp"]

score = 1 - (timestamp - min_timestamp) / (max_timestamp - min_timestamp)
end
return score
end
end

pickers
.new(opts, {
prompt_title = "Papis References",
finder = finders.new_table({
results = results,
entry_maker = function(entry)
local entry_pre_calc = db["search"]:get(entry["id"])[1]
local timestamp = entry_pre_calc["timestamp"]
local items = entry_pre_calc["items"]

local displayer_tbl = entry_pre_calc["displayer_tbl"]

local displayer = entry_display.create({
separator = "",
items = items,
Expand All @@ -65,6 +88,7 @@ local function papis_picker(opts)
value = search_string,
ordinal = search_string,
display = make_display,
timestamp = timestamp,
id = entry,
}
end,
Expand All @@ -91,7 +115,7 @@ local function papis_picker(opts)
vim.api.nvim_set_option_value("wrap", wrap, { win = status.preview_win })
end,
}),
sorter = telescope_config.generic_sorter(opts),
sorter = papis_sorter,
attach_mappings = function(_, map)
actions.select_default:replace(papis_actions.ref_insert(format_string))
map("i", "<c-o>f", papis_actions.open_file(), { desc = "Open file" })
Expand All @@ -112,6 +136,7 @@ end
return telescope.register_extension({
setup = function(opts)
wrap = opts["wrap"]
initial_sort_by_time_added = opts["initial_sort_by_time_added"]
preview_format = opts["preview_format"]
local search_keys = opts["search_keys"]
local results_format = opts["results_format"]
Expand Down

0 comments on commit 4afaffb

Please sign in to comment.