Use '-' instead of '?' for unknown duration

It is less distracting.

Also use mpd_read_time() and load "duration" as well.
This value isn't rounded to whole seconds, so we load
it before "time" as a fail-safe measure.
This commit is contained in:
Přemysl Eric Janouch 2020-10-24 08:44:08 +02:00
parent c0119027b1
commit 791c000791
Signed by: p
GPG Key ID: A0420B94F92B9493
1 changed files with 33 additions and 34 deletions

View File

@ -209,6 +209,35 @@ mpd_parse_kv (char *line, char **value)
return key;
}
static void
mpd_read_time (const char *value, int *sec, int *optional_msec)
{
if (!value)
return;
char *end = NULL;
long n = strtol (value, &end, 10);
if (n < 0 || (*end && *end != '.'))
return;
int msec = 0;
if (*end == '.')
{
// In practice, MPD always uses three decimal digits
size_t digits = strspn (++end, "0123456789");
if (end[digits])
return;
if (digits--) msec += (*end++ - '0') * 100;
if (digits--) msec += (*end++ - '0') * 10;
if (digits--) msec += *end++ - '0';
}
*sec = MIN (INT_MAX, n);
if (optional_msec)
*optional_msec = msec;
}
// --- cURL async wrapper ------------------------------------------------------
// You are meant to subclass this structure, no user_data pointers needed
@ -2239,12 +2268,11 @@ current_tab_on_item_draw (size_t item_index, struct row_buffer *buffer,
row_buffer_align (buffer, width - length_len, attrs);
char *s = NULL;
unsigned long n;
const char *time = compact_map_find (map, "time");
if (!time || !xstrtoul (&n, time, 10) || !(s = app_time_string (n)))
s = xstrdup ("?");
int duration = -1;
mpd_read_time (compact_map_find (map, "duration"), &duration, NULL);
mpd_read_time (compact_map_find (map, "time"), &duration, NULL);
char *s = duration < 0 ? xstrdup ("-") : app_time_string (duration);
char *right_aligned = xstrdup_printf ("%*s", length_len, s);
row_buffer_append (buffer, right_aligned, attrs);
free (right_aligned);
@ -3352,35 +3380,6 @@ debug_tab_init (void)
// --- MPD interface -----------------------------------------------------------
static void
mpd_read_time (const char *value, int *sec, int *optional_msec)
{
if (!value)
return;
char *end = NULL;
long n = strtol (value, &end, 10);
if (n < 0 || (*end && *end != '.'))
return;
int msec = 0;
if (*end == '.')
{
// In practice, MPD always uses three decimal digits
size_t digits = strspn (++end, "0123456789");
if (end[digits])
return;
if (digits--) msec += (*end++ - '0') * 100;
if (digits--) msec += (*end++ - '0') * 10;
if (digits--) msec += *end++ - '0';
}
*sec = MIN (INT_MAX, n);
if (optional_msec)
*optional_msec = msec;
}
static void
mpd_update_playlist_time (void)
{