dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / dpkg / progress.c
CommitLineData
1479465f
GJ
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
32void
33progress_init(struct progress *progress, const char *text, int max)
34{
35 progress->text = text;
36 progress->max = max;
37 progress->cur = 0;
38 progress->last_percent = 0;
39
40 progress->on_tty = isatty(1);
41
42 if (progress->on_tty)
43 printf("%s\r", text);
44 else
45 printf("%s", text);
46}
47
48void
49progress_step(struct progress *progress)
50{
51 int cur_percent;
52
53 if (!progress->on_tty)
54 return;
55
56 progress->cur++;
57
58 cur_percent = (progress->cur * 100) / progress->max;
59 if (cur_percent <= progress->last_percent)
60 return;
61 if (cur_percent % 5)
62 return;
63
64 progress->last_percent = cur_percent;
65
66 printf("%s%d%%\r", progress->text, cur_percent);
67}
68
69void
70progress_done(struct progress *progress)
71{
72 if (progress->on_tty)
73 printf("%s", progress->text);
74}