diff options
author | Avi Halachmi (:avih) <avihpit@yahoo.com> | 2021-07-16 12:06:20 +0300 |
---|---|---|
committer | avih <avih@users.noreply.github.com> | 2021-07-19 22:06:50 +0300 |
commit | 2335ee55146caebd40698596484befa867470188 (patch) | |
tree | e9b1a2faf23694b15d59fce5070c7f2e112b0ca4 | |
parent | 9fb200b6419c2707a5ec8d0786ae24ed02194297 (diff) | |
download | mpv-2335ee55146caebd40698596484befa867470188.tar.bz2 mpv-2335ee55146caebd40698596484befa867470188.tar.xz |
stats.lua: page 4 (keys): support help-like terminal printout
While --input-test is useful, and so is page 4 of stats, until now
there was no way to simply print the list in a help-like fashion.
This commit adds such printout, invoked by the script opt
stats-bindlist=yes, which uses the existing page 4 code. This prints
the list on startup and quits immediately - like any help page.
It's awkward to invoke compared to other help pages, and it does
require the stats page to be enabled (it is by default), however
it is a script-generated output, and currently there's no other
method to print a help page generated by a script.
The printout itself is performed using lua's io.write. While reliable,
it's not the standard way for mpv to print to the terminal.
Other possible printout methods are mp.msg.info - which also prints
"[stats]" prefix on each line (ugly), or forcing term-osd and setting
an osd-message which mpv will then print at the terminal - however
that's printed to stderr, and could also be subject to timing concerns
since we quit right afterwards.
In the future we can consider changing/integrating the invocation so
that mpv itself could print a help page generated by a script, thus
solving both the awkward invocation and printout-method issues.
-rw-r--r-- | DOCS/man/stats.rst | 6 | ||||
-rw-r--r-- | player/lua/stats.lua | 25 |
2 files changed, 29 insertions, 2 deletions
diff --git a/DOCS/man/stats.rst b/DOCS/man/stats.rst index 3684922189..9e6b7bc453 100644 --- a/DOCS/man/stats.rst +++ b/DOCS/man/stats.rst @@ -197,6 +197,12 @@ The keys are grouped automatically using a simple analysis of the command string, and one should not expect documentation-level grouping accuracy, however, it should still be reasonably useful. +Using ``--idle --script-opts=stats-bindlist=yes`` will print the list to the +terminal and quit immediately. By default long lines are shortened to 79 chars, +and terminal escape sequences are enabled. A different length limit can be +set by changing ``yes`` to a number (at least 40), and escape sequences can be +disabled by adding ``-`` before the value, e.g. ``...=-yes`` or ``...=-120``. + Like with ``--input-test``, the list includes bindings from ``input.conf`` and from user scripts. Use `--no-config`` to list only built-in bindings. diff --git a/player/lua/stats.lua b/player/lua/stats.lua index 367c019cd4..1d629e08ad 100644 --- a/player/lua/stats.lua +++ b/player/lua/stats.lua @@ -78,6 +78,8 @@ local o = { no_ass_b0 = "\027[0m", no_ass_it1 = "\027[3m", no_ass_it0 = "\027[0m", + + bindlist = "no", -- print page 4 to the terminal on startup and quit mpv } options.read_options(o) @@ -395,7 +397,7 @@ local function cmd_subject(cmd) return subw:len() > 1 and subw or "[unknown]" end -local function get_kbinfo_lines() +local function get_kbinfo_lines(width) -- active keys: only highest priotity of each key, and not our (stats) keys local bindings = mp.get_property_native("input-bindings", {}) local active = {} -- map: key-name -> bind-info @@ -458,7 +460,7 @@ local function get_kbinfo_lines() o.font_mono, kspaces, o.font, 1.3*o.font_size) local spost = term and "" or format("{\\u0\\fs%d}", o.font_size) local _, itabs = o.indent:gsub("\t", "") - local cutoff = term and 79 - o.indent:len() - itabs * 7 - spre:len() + local cutoff = term and (width or 79) - o.indent:len() - itabs * 7 - spre:len() -- create the display lines local info_lines = {} @@ -1147,3 +1149,22 @@ mp.register_event("video-reconfig", print_page(curr_page) end end) + +-- --script-opts=stats-bindlist=[-]{yes|<TERM-WIDTH>} +if o.bindlist ~= "no" then + mp.command("no-osd set really-quiet yes") + if o.bindlist:sub(1, 1) == "-" then + o.bindlist = o.bindlist:sub(2) + o.no_ass_b0 = "" + o.no_ass_b1 = "" + end + local width = max(40, math.floor(tonumber(o.bindlist) or 79)) + mp.add_timeout(0, function() -- wait for all other scripts to finish init + o.ass_formatting = false + o.no_ass_indent = " " + eval_ass_formatting() + io.write(pages[o.key_page_4].desc .. ":" .. + table.concat(get_kbinfo_lines(width)) .. "\n") + mp.command("quit") + end) +end |