From 08e2acbae1b92132f54b1f01213f4e05b8b74f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Sat, 12 Oct 2024 03:49:51 +0200 Subject: [PATCH] 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. --- player/command.c | 13 +++++++++++++ player/lua/stats.lua | 30 ++++++++++++++++++++---------- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/player/command.c b/player/command.c index fc7839418c..2d0f07dd5b 100644 --- a/player/command.c +++ b/player/command.c @@ -6749,6 +6749,17 @@ static void cmd_context_menu(void *p) 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. * 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 @@ -7224,6 +7235,8 @@ const struct mp_cmd_def mp_cmds[] = { { "context-menu", cmd_context_menu }, + { "flush-status-line", cmd_flush_status_line, { {"clear", OPT_BOOL(v.b)} } }, + {0} }; diff --git a/player/lua/stats.lua b/player/lua/stats.lua index f73e69041a..91d9102ed8 100644 --- a/player/lua/stats.lua +++ b/player/lua/stats.lua @@ -1795,19 +1795,29 @@ mp.register_event("video-reconfig", -- --script-opts=stats-bindlist=[-]{yes|} 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 + -- This is a special mode to print key bindings to the terminal, + -- Adjust the print format and level to make it print only the key bindings. + mp.set_property("msg-level", "all=no,statusline=status") + mp.set_property("term-osd", "force") + mp.set_property_bool("msg-module", false) + mp.set_property_bool("msg-time", false) + -- 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.no_ass_indent = " " o.term_size = { w = width , h = 0} - io.write(keybinding_info(false, true) .. "\n") - mp.command("quit") + mp.osd_message(keybinding_info(false, true)) + -- 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