Skip to content

Commit

Permalink
feat: configurable sort order in load session selector (#27)
Browse files Browse the repository at this point in the history
* feat: UX config options.

* change: instead of dir_only let's use detail_only, to follow the naming conventions.

* fix: All linting warnings cleared.

* comment conventions

* deleted: style options

* deleted: extra line

* delete: extra lines

* fix(linter warnings): Check nil.

* fix: Typo
  • Loading branch information
Zeioth authored Sep 11, 2023
1 parent bf7647c commit 6f13bd0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lua/resession/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ local default_config = {
-- Show more detail about the sessions when selecting one to load.
-- Disable if it causes lag.
load_detail = true,
-- List order ["modification_time", "creation_time", "filename"]
load_order = "modification_time",
-- Configuration for extensions
extensions = {
quickfix = {},
Expand Down
22 changes: 22 additions & 0 deletions lua/resession/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ end
---@return string[]
M.list = function(opts)
opts = opts or {}
local config = require("resession.config")
local files = require("resession.files")
local util = require("resession.util")
local session_dir = util.get_session_dir(opts.dir)
Expand All @@ -106,6 +107,27 @@ M.list = function(opts)
entries = uv.fs_readdir(fd)
end
uv.fs_closedir(fd)
-- Order options
if config.load_order == "filename" then
-- Sort by filename
table.sort(ret)
elseif config.load_order == "modification_time" then
-- Sort by modification_time
local default = { mtime = { sec = 0 } }
table.sort(ret, function(a, b)
local file_a = uv.fs_stat(session_dir .. "/" .. a .. ".json") or default
local file_b = uv.fs_stat(session_dir .. "/" .. b .. ".json") or default
return file_a.mtime.sec > file_b.mtime.sec
end)
elseif config.load_order == "creation_time" then
-- Sort by creation_time in descending order (most recent first)
local default = { birthtime = { sec = 0 } }
table.sort(ret, function(a, b)
local file_a = uv.fs_stat(session_dir .. "/" .. a .. ".json") or default
local file_b = uv.fs_stat(session_dir .. "/" .. b .. ".json") or default
return file_a.birthtime.sec > file_b.birthtime.sec
end)
end
return ret
end

Expand Down

0 comments on commit 6f13bd0

Please sign in to comment.