diff options
-rw-r--r-- | ChangeLog | 8 | ||||
-rw-r--r-- | lib/Makefile.am | 1 | ||||
-rw-r--r-- | lib/progress.c | 73 | ||||
-rw-r--r-- | lib/progress.h | 46 |
4 files changed, 128 insertions, 0 deletions
@@ -1,3 +1,11 @@ +2009-02-26 Romain Francoise <rfrancoise@debian.org>, + Guillem Jover <guillem@debian.org> + + * lib/Makefile.am (libdpkg_a_SOURCES): Add 'progress.h' and + 'progress.c'. + * lib/progress.h: New file. + * lib/progress.c: Likewise. + 2009-02-22 Raphael Hertzog <hertzog@debian.org> * scripts/update-alternatives.pl: Add new option --log to 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 + |