Code

release v0.29
[ncmpc.git] / src / progress_bar.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2017 The Music Player Daemon Project
3  * Project homepage: http://musicpd.org
4  *
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.
9  *
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.
14  *
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.
18  */
20 #include "progress_bar.h"
22 #include <assert.h>
24 void
25 progress_bar_paint(const struct progress_bar *p)
26 {
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');
38         }
40         wnoutrefresh(p->window.w);
41 }
43 static bool
44 progress_bar_calc(struct progress_bar *p)
45 {
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;
54 }
56 void
57 progress_bar_resize(struct progress_bar *p, unsigned width, int y, int x)
58 {
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);
66 }
68 bool
69 progress_bar_set(struct progress_bar *p, unsigned current, unsigned max)
70 {
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;
82 }