The following commit has been merged in the master branch:
commit 8c4a5bf6fa2d265ea037f72f82afe891c6b6475e
Author: Guillem Jover <[email protected]>
Date:   Thu Feb 26 07:18:38 2009 +0200

    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 <[email protected]>

diff --git a/ChangeLog b/ChangeLog
index 256d0c3..1c6b4dd 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2009-02-26  Romain Francoise <[email protected]>,
+            Guillem Jover  <[email protected]>
+
+       * 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  <[email protected]>
 
        * scripts/update-alternatives.pl: Add new option --log to
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 193de32..85c04ec 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 0000000..65b11e9
--- /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 <[email protected]>
+ * Copyright © 2009 Guillem Jover <[email protected]>
+ *
+ * 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/test/t-pkginfo.c b/lib/progress.h
similarity index 61%
copy from lib/test/t-pkginfo.c
copy to lib/progress.h
index 0172d8f..dad6f3c 100644
--- a/lib/test/t-pkginfo.c
+++ b/lib/progress.h
@@ -1,6 +1,6 @@
 /*
- * libdpkg - Debian packaging suite library routines
- * t-pkginfo.c - test pkginfo handling
+ * dpkg - main program for package management
+ * progress.c - generic progress reporting
  *
  * Copyright © 2009 Guillem Jover <[email protected]>
  *
@@ -19,26 +19,28 @@
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  */
 
-#include <dpkg-test.h>
-#include <dpkg-db.h>
+#ifndef DPKG_PROGRESS_H
+#define DPKG_PROGRESS_H
 
-static void
-test_pkginfo_informative(void)
-{
-       struct pkginfo pkg;
+#include <dpkg-def.h>
 
-       blankpackage(&pkg);
-       pkg.want = want_purge;
-       test_pass(informative(&pkg, &pkg.installed));
+DPKG_BEGIN_DECLS
 
-       /* FIXME: Complete. */
-}
+struct progress {
+       const char *text;
 
-static void
-test(void)
-{
-       test_pkginfo_informative();
+       int max;
+       int cur;
+       int last_percent;
 
-       /* FIXME: Complete. */
-}
+       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
 

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to