summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorColin Walters <walters@verbum.org>2017-12-12 14:06:08 -0500
committerColin Walters <walters@verbum.org>2017-12-12 15:24:42 -0500
commita8f96bd5f7e5a0cec6e4c40774e0eea99d0bd0c8 (patch)
tree4a84b321332730248245851b0fbaf29c3fe8e7ef
parent31ef1961ec6ee57db910911b3af13698535ac7b3 (diff)
downloadlibglnx-a8f96bd5f7e5a0cec6e4c40774e0eea99d0bd0c8.tar.gz
console: Add an "n items" API
It's often really useful to see the counts, not just the percentage.
-rw-r--r--glnx-console.c26
-rw-r--r--glnx-console.h6
2 files changed, 31 insertions, 1 deletions
diff --git a/glnx-console.c b/glnx-console.c
index c6d9331..3874765 100644
--- a/glnx-console.c
+++ b/glnx-console.c
@@ -280,6 +280,32 @@ glnx_console_progress_text_percent (const char *text,
text_percent_internal (text, percentage);
}
+/**
+ * glnx_console_progress_n_items:
+ * @text: Show this text before the progress bar
+ * @current: An integer for how many items have been processed
+ * @total: An integer for how many items there are total
+ *
+ * On a tty, print to the console @text followed by [@current/@total],
+ * then an ASCII art progress bar, like glnx_console_progress_text_percent().
+ *
+ * You must have called glnx_console_lock() before invoking this
+ * function.
+ */
+void
+glnx_console_progress_n_items (const char *text,
+ guint current,
+ guint total)
+{
+ g_return_if_fail (current <= total);
+ g_return_if_fail (total > 0);
+
+ g_autofree char *newtext = g_strdup_printf ("%s (%u/%u)", text, current, total);
+ /* Special case current == total to ensure we end at 100% */
+ int percentage = (current == total) ? 100 : (((double)current) / total * 100);
+ glnx_console_progress_text_percent (newtext, percentage);
+}
+
void
glnx_console_text (const char *text)
{
diff --git a/glnx-console.h b/glnx-console.h
index 8c1d811..108dc40 100644
--- a/glnx-console.h
+++ b/glnx-console.h
@@ -36,7 +36,11 @@ void glnx_console_lock (GLnxConsoleRef *ref);
void glnx_console_text (const char *text);
void glnx_console_progress_text_percent (const char *text,
- guint percentage);
+ guint percentage);
+
+void glnx_console_progress_n_items (const char *text,
+ guint current,
+ guint total);
void glnx_console_unlock (GLnxConsoleRef *ref);