Skip to content

Commit d8a543a

Browse files
committed
loadfile: truncate long track URL titles
When a track title is a URL longer than 90 characters, truncate it to not make track information too verbose. This uses term_disp_width() which is convenient even for ASS output because it avoids cutting in the middle of UTF-8 characters. So wide unicode characters will count as 2 characters. Long track URLs like reported in #10975 no longer flood the terminal with useless information. Long track URLs also no longer push track metadata beyond the track menu, without having to implement UTF-8 aware functions in select.lua to avoid cutting titles in the middle of UTF-8 characters. The full URLs are always available in track-list/N/external-filename. --osd-max-track-title-url-length configures this behavior.
1 parent 2e5e293 commit d8a543a

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add `--osd-track-title-url-max-length`

DOCS/man/options.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4698,6 +4698,13 @@ OSD
46984698

46994699
Default: ``title``.
47004700

4701+
``--osd-track-title-url-max-length=<length>``
4702+
Truncate URL track titles to ``length`` characters. Wide unicode characters
4703+
count as multiple characters to make this work correctly with terminal
4704+
output. Set to 0 to disable.
4705+
4706+
Default: 90.
4707+
47014708
``--osd-bar-align-x=<-1-1>``
47024709
Position of the OSD bar. -1 is far left, 0 is centered, 1 is far right.
47034710
Fractional values (like 0.5) are allowed.

options/options.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,8 @@ static const m_option_t mp_opts[] = {
888888

889889
{"osd-playlist-entry", OPT_CHOICE(playlist_entry_name,
890890
{"title", 0}, {"filename", 1}, {"both", 2})},
891+
{"osd-track-title-url-max-length", OPT_INT(max_track_title_url_max_length),
892+
M_RANGE(0, INT_MAX)},
891893

892894
{"video-osd", OPT_BOOL(video_osd), .flags = UPDATE_OSD},
893895

@@ -999,6 +1001,7 @@ static const struct MPOpts mp_default_opts = {
9991001
.osd_level = 1,
10001002
.osd_on_seek = 1,
10011003
.osd_duration = 1000,
1004+
.max_track_title_url_max_length = 90,
10021005
#if HAVE_LUA
10031006
.lua_load_osc = true,
10041007
.lua_load_ytdl = true,

options/options.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ typedef struct MPOpts {
277277
char *osd_status_msg;
278278
char *osd_msg[3];
279279
int playlist_entry_name;
280+
int max_track_title_url_max_length;
280281
int player_idle_mode;
281282
char **input_commands;
282283
bool consolecontrols;

player/loadfile.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include "mpv_talloc.h"
2727

28+
#include "misc/codepoint_width.h"
2829
#include "misc/thread_pool.h"
2930
#include "misc/thread_tools.h"
3031
#include "osdep/io.h"
@@ -909,8 +910,18 @@ int mp_add_external_file(struct MPContext *mpctx, char *filename,
909910
bstr title = bstr0(mp_basename(disp_filename));
910911
bstr_eatstart(&title, parent);
911912
bstr_eatstart(&title, bstr0("."));
912-
if (title.len)
913+
914+
const unsigned char *cut_pos = NULL;
915+
if (mp_is_url(bstr0(disp_filename))
916+
&& mpctx->opts->max_track_title_url_max_length)
917+
term_disp_width(title, mpctx->opts->max_track_title_url_max_length,
918+
&cut_pos);
919+
if (cut_pos) {
920+
title.len = cut_pos - title.start;
921+
t->title = talloc_asprintf(t, "%.*s\u2026", BSTR_P(title));
922+
} else if (title.len) {
913923
t->title = bstrdup0(t, title);
924+
}
914925
}
915926
t->external_filename = mp_normalize_user_path(t, mpctx->global, filename);
916927
t->no_default = sh->type != filter;

0 commit comments

Comments
 (0)