diff options
author | Guillem Jover <guillem@debian.org> | 2009-02-26 07:18:38 +0200 |
---|---|---|
committer | Guillem Jover <guillem@debian.org> | 2009-02-26 07:25:33 +0200 |
commit | 8c4a5bf6fa2d265ea037f72f82afe891c6b6475e (patch) | |
tree | a348df9f9736b88a8172d84c880723dca3f92892 /lib | |
parent | 73c189a330b4878ab7be880d4b943fe9fbb3b06d (diff) | |
download | dpkg-8c4a5bf6fa2d265ea037f72f82afe891c6b6475e.tar.gz |
libdpkg: Add progress reporting support
Initial support to report progress from dpkg. For now it only changes
behaviour if printing on a tty, later on it could be made pluggable so
that external applications can monitor the internal progress on
expensive dpkg actions.
Based-on-patch-by: Romain Francoise <rfrancoise@debian.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Makefile.am | 1 | ||||
-rw-r--r-- | lib/progress.c | 73 | ||||
-rw-r--r-- | lib/progress.h | 46 |
3 files changed, 120 insertions, 0 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am index 193de3250..85c04ec1e 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -39,6 +39,7 @@ libdpkg_a_SOURCES = \ parsehelp.c \ parsedump.h \ path.c \ + progress.c progress.h \ showpkg.c \ string.c \ subproc.c \ diff --git a/lib/progress.c b/lib/progress.c new file mode 100644 index 000000000..65b11e998 --- /dev/null +++ b/lib/progress.c @@ -0,0 +1,73 @@ +/* + * dpkg - main program for package management + * progress.c - generic progress reporting + * + * Copyright © 2009 Romain Francoise <rfrancoise@debian.org> + * Copyright © 2009 Guillem Jover <guillem@debian.org> + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2, + * or (at your option) any later version. + * + * This is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with dpkg; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#include <config.h> +#include <compat.h> + +#include <dpkg-i18n.h> + +#include <unistd.h> +#include <stdio.h> + +#include "progress.h" + +void +progress_init(struct progress *progress, const char *text, int max) +{ + progress->text = text; + progress->max = max; + progress->cur = 0; + progress->last_percent = 0; + + progress->on_tty = isatty(1); + + printf("%s", text); +} + +void +progress_step(struct progress *progress) +{ + int cur_percent; + + if (!progress->on_tty) + return; + + progress->cur++; + + cur_percent = (progress->cur * 100) / progress->max; + if (cur_percent <= progress->last_percent) + return; + if (cur_percent % 5) + return; + + progress->last_percent = cur_percent; + + printf("\r%s%3d%%", progress->text, cur_percent); +} + +void +progress_done(struct progress *progress) +{ + if (progress->on_tty) + printf("\r%s", progress->text); +} + diff --git a/lib/progress.h b/lib/progress.h new file mode 100644 index 000000000..dad6f3c44 --- /dev/null +++ b/lib/progress.h @@ -0,0 +1,46 @@ +/* + * dpkg - main program for package management + * progress.c - generic progress reporting + * + * Copyright © 2009 Guillem Jover <guillem@debian.org> + * + * This is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2, + * or (at your option) any later version. + * + * This is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with dpkg; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#ifndef DPKG_PROGRESS_H +#define DPKG_PROGRESS_H + +#include <dpkg-def.h> + +DPKG_BEGIN_DECLS + +struct progress { + const char *text; + + int max; + int cur; + int last_percent; + + int on_tty; +}; + +void progress_init(struct progress *progress, const char *text, int max); +void progress_step(struct progress *progress); +void progress_done(struct progress *progress); + +DPKG_END_DECLS + +#endif + |