stats.lua: don't use io.write from builtin script

Scripts and especially internal scripts shouln't bypass msg.c logging
code for various resons, ranging from processing the input, filtering
the log levels, truncating the output and so on. io.write() is lazy way
of outputing to stdout without respecting mpv's logging module.

Uses osd message, because this has no prefixes. Added internal
flush-status-line command to flush current output without clearing
before exiting.

This commit will allow us to remove duplicated terminal handling code
from stats.lua, mpv core already handles all that and does it in better
way, without taking shortcuts.
This commit is contained in:
Kacper Michajłow 2024-10-12 03:49:51 +02:00
parent 2df582b839
commit 08e2acbae1
2 changed files with 33 additions and 10 deletions

View File

@ -6749,6 +6749,17 @@ static void cmd_context_menu(void *p)
vo_control(vo, VOCTRL_SHOW_MENU, NULL); vo_control(vo, VOCTRL_SHOW_MENU, NULL);
} }
static void cmd_flush_status_line(void *p)
{
struct mp_cmd_ctx *cmd = p;
struct MPContext *mpctx = cmd->mpctx;
if (!mpctx->log)
return;
mp_msg_flush_status_line(mpctx->log, cmd->args[0].v.b);
}
/* This array defines all known commands. /* This array defines all known commands.
* The first field the command name used in libmpv and input.conf. * The first field the command name used in libmpv and input.conf.
* The second field is the handler function (see mp_cmd_def.handler and * The second field is the handler function (see mp_cmd_def.handler and
@ -7224,6 +7235,8 @@ const struct mp_cmd_def mp_cmds[] = {
{ "context-menu", cmd_context_menu }, { "context-menu", cmd_context_menu },
{ "flush-status-line", cmd_flush_status_line, { {"clear", OPT_BOOL(v.b)} } },
{0} {0}
}; };

View File

@ -1795,19 +1795,29 @@ mp.register_event("video-reconfig",
-- --script-opts=stats-bindlist=[-]{yes|<TERM-WIDTH>} -- --script-opts=stats-bindlist=[-]{yes|<TERM-WIDTH>}
if o.bindlist ~= "no" then if o.bindlist ~= "no" then
mp.command("no-osd set really-quiet yes") -- This is a special mode to print key bindings to the terminal,
if o.bindlist:sub(1, 1) == "-" then -- Adjust the print format and level to make it print only the key bindings.
o.bindlist = o.bindlist:sub(2) mp.set_property("msg-level", "all=no,statusline=status")
o.no_ass_b0 = "" mp.set_property("term-osd", "force")
o.no_ass_b1 = "" mp.set_property_bool("msg-module", false)
end mp.set_property_bool("msg-time", false)
local width = max(40, math.floor(tonumber(o.bindlist) or 79)) -- wait for all other scripts to finish init
mp.add_timeout(0, function() -- wait for all other scripts to finish init mp.add_timeout(0, function()
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))
o.ass_formatting = false o.ass_formatting = false
o.no_ass_indent = " " o.no_ass_indent = " "
o.term_size = { w = width , h = 0} o.term_size = { w = width , h = 0}
io.write(keybinding_info(false, true) .. "\n") mp.osd_message(keybinding_info(false, true))
mp.command("quit") -- wait for next tick to print status line and flush it without clearing
mp.add_timeout(0, function()
mp.command("flush-status-line no")
mp.command("quit")
end)
end) end)
end end