main: check for strfsong() failure, add fallback
[ncmpc.git] / src / progress_bar.c
blob9df7a510ebd305a842ec8d368efba8bfe32a1044
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "progress_bar.h"
22 #include <assert.h>
24 void
25 progress_bar_paint(const struct progress_bar *p)
27 assert(p != NULL);
29 mvwhline(p->window.w, 0, 0, ACS_HLINE, p->window.cols);
31 if (p->max > 0) {
32 assert(p->width < p->window.cols);
34 if (p->width > 0)
35 whline(p->window.w, '=', p->width);
37 mvwaddch(p->window.w, 0, p->width, 'O');
40 wnoutrefresh(p->window.w);
43 static bool
44 progress_bar_calc(struct progress_bar *p)
46 if (p->max == 0)
47 return false;
49 unsigned old_width = p->width;
50 p->width = (p->window.cols * p->current) / (p->max + 1);
51 assert(p->width < p->window.cols);
53 return p->width != old_width;
56 void
57 progress_bar_resize(struct progress_bar *p, unsigned width, int y, int x)
59 assert(p != NULL);
61 p->window.cols = width;
62 wresize(p->window.w, 1, width);
63 mvwin(p->window.w, y, x);
65 progress_bar_calc(p);
68 bool
69 progress_bar_set(struct progress_bar *p, unsigned current, unsigned max)
71 assert(p != NULL);
73 if (current > max)
74 current = max;
76 bool modified = (max == 0) != (p->max == 0);
78 p->max = max;
79 p->current = current;
81 return progress_bar_calc(p) || modified;