Finer progress bar thanks to Unicode

This commit is contained in:
Přemysl Eric Janouch 2016-10-02 03:17:07 +02:00
parent e4f3f8ebf0
commit 7e35164e77
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 31 additions and 7 deletions

View File

@ -787,6 +787,35 @@ app_write_time (struct row_buffer *buf, int seconds, chtype attrs)
str_free (&s);
}
static void
app_write_gauge (struct row_buffer *buf, float ratio, int width)
{
// Always compute it in exactly eight times the resolution,
// because sometimes Unicode is even useful
int len_left = ratio * width * 8 + 0.5;
static const char *partials[] = { " ", "", "", "", "", "", "", ""};
int remainder = len_left % N_ELEMENTS (partials);
len_left /= N_ELEMENTS (partials);
// Assuming that if we can show the 1/8 box then we can show them all
const char *partial = NULL;
// TODO: cache this setting and make it configurable since it doesn't seem
// to work 100% (e.g. incompatible with undelining in urxvt)
if (!app_is_character_in_locale (L''))
len_left += remainder >= (int) N_ELEMENTS (partials) / 2;
else
partial = partials[remainder];
int len_right = width - len_left;
while (len_left-- > 0)
row_buffer_append (buf, " ", APP_ATTR (ELAPSED));
if (partial && len_right-- > 0)
row_buffer_append (buf, partial, APP_ATTR (REMAINS));
while (len_right-- > 0)
row_buffer_append (buf, " ", APP_ATTR (REMAINS));
}
static void
app_redraw_status (void)
{
@ -842,13 +871,8 @@ app_redraw_status (void)
if (!stopped && g_ctx.song_elapsed >= 0 && g_ctx.song_duration >= 1
&& remaining > 0)
{
int len_elapsed = (int) ((float) g_ctx.song_elapsed
/ g_ctx.song_duration * remaining + 0.5);
int len_remains = remaining - len_elapsed;
while (len_elapsed-- > 0)
row_buffer_append (&buf, " ", APP_ATTR (ELAPSED));
while (len_remains-- > 0)
row_buffer_append (&buf, " ", APP_ATTR (REMAINS));
app_write_gauge (&buf,
(float) g_ctx.song_elapsed / g_ctx.song_duration, remaining);
}
else while (remaining-- > 0)
row_buffer_append (&buf, " ", a_normal);