Просмотр исходного кода

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>
Guillem Jover лет назад: 17
Родитель
Сommit
8c4a5bf6fa
4 измененных файлов с 128 добавлено и 0 удалено
  1. 8 0
      ChangeLog
  2. 1 0
      lib/Makefile.am
  3. 73 0
      lib/progress.c
  4. 46 0
      lib/progress.h

+ 8 - 0
ChangeLog

@@ -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

+ 1 - 0
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 \

+ 73 - 0
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);
+}
+

+ 46 - 0
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
+