Branch data Line data Source code
1 : : /*
2 : : * libdpkg - Debian packaging suite library routines
3 : : * progress.c - generic progress reporting
4 : : *
5 : : * Copyright © 2009 Romain Francoise <rfrancoise@debian.org>
6 : : * Copyright © 2009 Guillem Jover <guillem@debian.org>
7 : : *
8 : : * This is free software; you can redistribute it and/or modify
9 : : * it under the terms of the GNU General Public License as published by
10 : : * the Free Software Foundation; either version 2 of the License, or
11 : : * (at your option) any later version.
12 : : *
13 : : * This is distributed in the hope that it will be useful,
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : * GNU General Public License for more details.
17 : : *
18 : : * You should have received a copy of the GNU General Public License
19 : : * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 : : */
21 : :
22 : : #include <config.h>
23 : : #include <compat.h>
24 : :
25 : : #include <unistd.h>
26 : : #include <stdio.h>
27 : :
28 : : #include <dpkg/i18n.h>
29 : :
30 : : #include "progress.h"
31 : :
32 : : void
33 : 0 : progress_init(struct progress *progress, const char *text, int max)
34 : : {
35 : 0 : progress->text = text;
36 : 0 : progress->max = max;
37 : 0 : progress->cur = 0;
38 : 0 : progress->last_percent = 0;
39 : :
40 : 0 : progress->on_tty = isatty(1);
41 : :
42 : 0 : fputs(text, stdout);
43 [ # # ]: 0 : if (progress->on_tty)
44 : 0 : putchar('\r');
45 : 0 : }
46 : :
47 : : void
48 : 0 : progress_step(struct progress *progress)
49 : : {
50 : : int cur_percent;
51 : :
52 [ # # ]: 0 : if (!progress->on_tty)
53 : 0 : return;
54 : :
55 : 0 : progress->cur++;
56 : :
57 : 0 : cur_percent = (progress->cur * 100) / progress->max;
58 [ # # ]: 0 : if (cur_percent <= progress->last_percent)
59 : 0 : return;
60 [ # # ]: 0 : if (cur_percent % 5)
61 : 0 : return;
62 : :
63 : 0 : progress->last_percent = cur_percent;
64 : :
65 : 0 : fputs(progress->text, stdout);
66 : : /* TRANSLATORS: This is part of the progress output, it is a decimal
67 : : * percentage. */
68 : 0 : printf(_("%d%%"), cur_percent);
69 : 0 : putchar('\r');
70 : : }
71 : :
72 : : void
73 : 0 : progress_done(struct progress *progress)
74 : : {
75 [ # # ]: 0 : if (progress->on_tty)
76 : 0 : fputs(progress->text, stdout);
77 : 0 : }
|