stats.lua: exit with ESC if being toggled

If not opened in oneshot mode, close the stats with ESC. Especially now
that ? toggles showing key bindings by default, this provides an
intuitive way to close the stats.

Also do some minor reformatting of key binding documentation.
This commit is contained in:
Guido Cella 2024-10-17 09:20:04 +02:00 committed by Kacper Michajłow
parent 7e834ce870
commit 50faafd8bd
2 changed files with 35 additions and 5 deletions

View File

@ -30,6 +30,12 @@ stats:
0 Internal stuff (scroll)
==== ==================
If stats were displayed by toggling, these key bindings are also active:
==== ==================
ESC Close the stats
==== ==================
On pages which support scroll, these key bindings are also active:
==== ==================
@ -37,8 +43,11 @@ UP Scroll one line up
DOWN Scroll one line down
==== ==================
Page 4 also binds ``/`` to search for input bindings by typing part of a binding
or command.
On page 4, these key bindings are also active:
==== ==================
/ Search key bindings
==== ==================
Configuration
-------------
@ -62,6 +71,8 @@ Configurable Options
Default: 5
``key_page_0``
Default: 0
``key_exit``
Default: ESC
Key bindings for page switching while stats are displayed.

View File

@ -24,6 +24,7 @@ local o = {
key_scroll_up = "UP",
key_scroll_down = "DOWN",
key_search = "/",
key_exit = "ESC",
scroll_lines = 1,
duration = 4,
@ -135,6 +136,7 @@ local function init_buffers()
end
local cache_ahead_buf, cache_speed_buf
local perf_buffers = {}
local process_key_binding
local function graph_add_value(graph, value)
graph.pos = (graph.pos % graph.len) + 1
@ -1631,6 +1633,21 @@ local function unbind_search()
mp.remove_key_binding("__forced_"..o.key_search)
end
local function bind_exit()
-- Don't bind in oneshot mode because if ESC is pressed right when the stats
-- stop being displayed, it would unintentionally trigger any user-defined
-- ESC binding.
if not display_timer.oneshot then
mp.add_forced_key_binding(o.key_exit, "__forced_" .. o.key_exit, function ()
process_key_binding(false)
end)
end
end
local function unbind_exit()
mp.remove_key_binding("__forced_" .. o.key_exit)
end
local function update_scroll_bindings(k)
if pages[k].scroll then
bind_scroll()
@ -1660,6 +1677,7 @@ local function add_page_bindings()
mp.add_forced_key_binding(k, "__forced_"..k, a(k), {repeatable=true})
end
update_scroll_bindings(curr_page)
bind_exit()
end
@ -1670,10 +1688,11 @@ local function remove_page_bindings()
end
unbind_scroll()
unbind_search()
unbind_exit()
end
local function process_key_binding(oneshot)
process_key_binding = function(oneshot)
reset_scroll_offsets()
-- Stats are already being displayed
if display_timer:is_enabled() then