From 8708f4dc918b1bc005149c50abb3d54d66ed3047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Michaj=C5=82ow?= Date: Sun, 19 Nov 2023 21:22:24 +0100 Subject: m_property: add `>` for fixed precision floating-point expansion This enhancement makes it easier to create constant width property expansions, useful for the `--term-status-msg`. Additionally, it changes to `%f` printing with manual zero trimming, which is easier to control than `%g`. With this method, we can directly specify precision, not just significant numbers. This approach also avoids overly high precision for values less than 1, which is not necessary for a generic floating-point print function. A new print helper function is added, which can be used with adjusted precision for specific cases where a different default is needed. This also unifies the code slightly. --- player/command.c | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) (limited to 'player') diff --git a/player/command.c b/player/command.c index ce7d6d5541..b5f9ad8065 100644 --- a/player/command.c +++ b/player/command.c @@ -414,9 +414,9 @@ static int mp_property_playback_speed(void *ctx, struct m_property *prop, int action, void *arg) { MPContext *mpctx = ctx; - if (action == M_PROPERTY_PRINT) { - double speed = mpctx->opts->playback_speed; - *(char **)arg = talloc_asprintf(NULL, "%.2f", speed); + if (action == M_PROPERTY_PRINT || action == M_PROPERTY_FIXED_LEN_PRINT) { + *(char **)arg = mp_format_double(NULL, mpctx->opts->playback_speed, 2, + false, false, action != M_PROPERTY_FIXED_LEN_PRINT); return M_PROPERTY_OK; } return mp_property_generic_option(mpctx, prop, action, arg); @@ -434,8 +434,9 @@ static int mp_property_av_speed_correction(void *ctx, struct m_property *prop, default: MP_ASSERT_UNREACHABLE(); } - if (action == M_PROPERTY_PRINT) { - *(char **)arg = talloc_asprintf(NULL, "%+.3g%%", (val - 1) * 100); + if (action == M_PROPERTY_PRINT || action == M_PROPERTY_FIXED_LEN_PRINT) { + *(char **)arg = mp_format_double(NULL, (val - 1) * 100, 2, true, + true, action != M_PROPERTY_FIXED_LEN_PRINT); return M_PROPERTY_OK; } @@ -667,13 +668,9 @@ static int mp_property_avsync(void *ctx, struct m_property *prop, MPContext *mpctx = ctx; if (!mpctx->ao_chain || !mpctx->vo_chain) return M_PROPERTY_UNAVAILABLE; - if (action == M_PROPERTY_PRINT) { - // Truncate anything < 1e-4 to avoid switching to scientific notation - if (fabs(mpctx->last_av_difference) < 1e-4) { - *(char **)arg = talloc_strdup(NULL, "0"); - } else { - *(char **)arg = talloc_asprintf(NULL, "%+.2g", mpctx->last_av_difference); - } + if (action == M_PROPERTY_PRINT || action == M_PROPERTY_FIXED_LEN_PRINT) { + *(char **)arg = mp_format_double(NULL, mpctx->last_av_difference, 4, + true, false, action != M_PROPERTY_FIXED_LEN_PRINT); return M_PROPERTY_OK; } return m_property_double_ro(action, arg, mpctx->last_av_difference); @@ -3754,8 +3751,9 @@ static int do_op_udata(struct udata_ctx* ctx, int action, void *arg) assert(node); m_option_copy(&udata_type, arg, node); return M_PROPERTY_OK; + case M_PROPERTY_FIXED_LEN_PRINT: case M_PROPERTY_PRINT: { - char *str = m_option_pretty_print(&udata_type, node); + char *str = m_option_pretty_print(&udata_type, node, action == M_PROPERTY_FIXED_LEN_PRINT); *(char **)arg = str; return str != NULL; } -- cgit v1.2.3