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,15 +30,24 @@ stats:
0 Internal stuff (scroll) 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: On pages which support scroll, these key bindings are also active:
==== ================== ==== ==================
UP Scroll one line up UP Scroll one line up
DOWN Scroll one line down DOWN Scroll one line down
==== ================== ==== ==================
Page 4 also binds ``/`` to search for input bindings by typing part of a binding On page 4, these key bindings are also active:
or command.
==== ==================
/ Search key bindings
==== ==================
Configuration Configuration
------------- -------------
@ -62,6 +71,8 @@ Configurable Options
Default: 5 Default: 5
``key_page_0`` ``key_page_0``
Default: 0 Default: 0
``key_exit``
Default: ESC
Key bindings for page switching while stats are displayed. Key bindings for page switching while stats are displayed.

View File

@ -24,6 +24,7 @@ local o = {
key_scroll_up = "UP", key_scroll_up = "UP",
key_scroll_down = "DOWN", key_scroll_down = "DOWN",
key_search = "/", key_search = "/",
key_exit = "ESC",
scroll_lines = 1, scroll_lines = 1,
duration = 4, duration = 4,
@ -135,6 +136,7 @@ local function init_buffers()
end end
local cache_ahead_buf, cache_speed_buf local cache_ahead_buf, cache_speed_buf
local perf_buffers = {} local perf_buffers = {}
local process_key_binding
local function graph_add_value(graph, value) local function graph_add_value(graph, value)
graph.pos = (graph.pos % graph.len) + 1 graph.pos = (graph.pos % graph.len) + 1
@ -1631,6 +1633,21 @@ local function unbind_search()
mp.remove_key_binding("__forced_"..o.key_search) mp.remove_key_binding("__forced_"..o.key_search)
end 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) local function update_scroll_bindings(k)
if pages[k].scroll then if pages[k].scroll then
bind_scroll() bind_scroll()
@ -1660,6 +1677,7 @@ local function add_page_bindings()
mp.add_forced_key_binding(k, "__forced_"..k, a(k), {repeatable=true}) mp.add_forced_key_binding(k, "__forced_"..k, a(k), {repeatable=true})
end end
update_scroll_bindings(curr_page) update_scroll_bindings(curr_page)
bind_exit()
end end
@ -1670,10 +1688,11 @@ local function remove_page_bindings()
end end
unbind_scroll() unbind_scroll()
unbind_search() unbind_search()
unbind_exit()
end end
local function process_key_binding(oneshot) process_key_binding = function(oneshot)
reset_scroll_offsets() reset_scroll_offsets()
-- Stats are already being displayed -- Stats are already being displayed
if display_timer:is_enabled() then if display_timer:is_enabled() then