summaryrefslogtreecommitdiff
path: root/yazi-enter.yazi/main.lua
diff options
context:
space:
mode:
Diffstat (limited to 'yazi-enter.yazi/main.lua')
-rw-r--r--yazi-enter.yazi/main.lua89
1 files changed, 86 insertions, 3 deletions
diff --git a/yazi-enter.yazi/main.lua b/yazi-enter.yazi/main.lua
index b31a7b0..c14d16b 100644
--- a/yazi-enter.yazi/main.lua
+++ b/yazi-enter.yazi/main.lua
@@ -9,6 +9,8 @@ local CASE_SENSITIVE = "sensitive"
local CASE_INSENSITIVE = "insensitive"
local CASE_SMART = "smart"
local FIND_ACTION = "find_do"
+local SORT_ACTION = "sort"
+local SORT_ALPHABETICAL = "alphabetical"
local ACTION_ENTER = "enter"
local ACTION_OPEN_HOVERED = "open-hovered"
local ACTION_NONE = "none"
@@ -131,6 +133,49 @@ local function decide_target(mode, is_dir)
return ACTION_NONE
end
+local function new_alphabetical_sort_request()
+ return {
+ by = SORT_ALPHABETICAL,
+ reverse = false,
+ }
+end
+
+local acquire_transient_sort = ya.sync(function(state)
+ local lease = state.transient_sort_lease
+ if lease ~= nil then
+ lease.count = lease.count + 1
+ return
+ end
+
+ local preference = cx.active.pref
+ state.transient_sort_lease = {
+ count = 1,
+ restore = {
+ by = tostring(preference.sort_by),
+ reverse = preference.sort_reverse,
+ dir_first = preference.sort_dir_first,
+ sensitive = preference.sort_sensitive,
+ translit = preference.sort_translit,
+ fallback = tostring(preference.sort_fallback),
+ },
+ }
+end)
+
+local release_transient_sort = ya.sync(function(state)
+ local lease = state.transient_sort_lease
+ if lease == nil or type(lease.count) ~= "number" or lease.count < 1 then
+ error("yazi-enter: transient sort lease is invalid", 0)
+ end
+
+ if lease.count > 1 then
+ lease.count = lease.count - 1
+ return
+ end
+
+ ya.emit(SORT_ACTION, lease.restore)
+ state.transient_sort_lease = nil
+end)
+
local commit = ya.sync(function(_, mode, submitted_query)
local tab = cx.active
local finder = tab.finder
@@ -177,6 +222,10 @@ local function apply_find(query, search_policy)
return UPDATE_FAILURE, reply_or_error
end
+local function apply_alphabetical_sort()
+ return ya.exec(SORT_ACTION, new_alphabetical_sort_request())
+end
+
local function new_prompt_specification(direction)
return {
name = COMMON_PROMPT_DATA.name,
@@ -248,7 +297,10 @@ local function new_application_boundaries(
prompt_capability,
find_update_capability,
commit_capability,
- target_dispatch_capability
+ target_dispatch_capability,
+ sort_acquire_capability,
+ alphabetical_sort_capability,
+ sort_release_capability
)
return {
prompt = function(prompt_data)
@@ -287,6 +339,15 @@ local function new_application_boundaries(
end
target_dispatch_capability(decision)
end,
+ sort_acquire = function()
+ return sort_acquire_capability()
+ end,
+ sort_alphabetically = function()
+ return alphabetical_sort_capability()
+ end,
+ sort_release = function()
+ return sort_release_capability()
+ end,
}
end
@@ -336,6 +397,25 @@ local function run_selection_session(intent, boundaries)
end
end
+local function run_transiently_sorted_session(intent, boundaries)
+ boundaries.sort_acquire()
+
+ local session_ok, result_or_error = pcall(function()
+ boundaries.sort_alphabetically()
+ return run_selection_session(intent, boundaries)
+ end)
+
+ local release_ok, release_error = pcall(boundaries.sort_release)
+ if not release_ok then
+ error(release_error, 0)
+ end
+ if not session_ok then
+ error(result_or_error, 0)
+ end
+
+ return result_or_error
+end
+
local function run_entry(job)
local intent = parse_invocation_intent(job.args)
local boundaries = new_application_boundaries(
@@ -344,9 +424,12 @@ local function run_entry(job)
commit,
function()
error("yazi-enter: target dispatch is owned by the commit transaction", 0)
- end
+ end,
+ acquire_transient_sort,
+ apply_alphabetical_sort,
+ release_transient_sort
)
- return run_selection_session(intent, boundaries)
+ return run_transiently_sorted_session(intent, boundaries)
end
return {