From 791c000791726a396cc6bd7e4b033e3678b57193 Mon Sep 17 00:00:00 2001 From: Přemysl Eric Janouch
Date: Sat, 24 Oct 2020 08:44:08 +0200 Subject: 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. --- nncmpp.c | 67 ++++++++++++++++++++++++++++++++-------------------------------- 1 file changed, 33 insertions(+), 34 deletions(-) diff --git a/nncmpp.c b/nncmpp.c index 895667b..4ad5b86 100644 --- a/nncmpp.c +++ b/nncmpp.c @@ -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) { -- cgit v1.2.3-70-g09d2