This patch adds some additional commands, and implements corresponding
progress callbacks that produce output for the 'dialog' utility:
http://invisible-island.net/dialog/dialog.html

The simple "*-with-gauge" just emits percentage numbers, line by line,
while "-witch-xgauge" outputs extended information to update the
dialog prompt. Both are meant for directly piping theoutput:
sunxi-fel write-with-gauge <...> | dialog --gauge "FEL upload" 6 70

Signed-off-by: Bernhard Nortmann <bernhard.nortm...@web.de>
---
 fel.c      | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 progress.c | 44 ++++++++++++++++++++++++++++++++++++++++++++
 progress.h |  2 ++
 3 files changed, 106 insertions(+)

diff --git a/fel.c b/fel.c
index 13565f3..2e948fd 100644
--- a/fel.c
+++ b/fel.c
@@ -1272,8 +1272,13 @@ int main(int argc, char **argv)
                        "       read address length file        Write memory 
contents into file\n"
                        "       write address file              Store file 
contents into memory\n"
                        "       write-with-progress addr file   \"write\" with 
progress bar\n"
+                       "       write-with-gauge addr file      Output progress 
for \"dialog --gauge\"\n"
+                       "       write-with-xgauge addr file     Extended gauge 
output (updates prompt)\n"
                        "       multi[write] # addr file ...    
\"write-with-progress\" multiple files,\n"
                        "                                       sharing a 
common progress status\n"
+                       "       multi[write]-with-gauge ...     like their 
\"write-with-*\" counterpart,\n"
+                       "       multi[write]-with-xgauge ...      but following 
the 'multi' syntax:\n"
+                       "                                         <#> addr file 
[addr file [...]]\n"
                        "       ver[sion]                       Show BROM 
version\n"
                        "       clear address length            Clear memory\n"
                        "       fill address length value       Fill memory\n"
@@ -1348,6 +1353,19 @@ int main(int argc, char **argv)
                                file_upload(handle, argv[3], strtoul(argv[2], 
NULL, 0));
                        }
                        skip=3;
+               } else if (strcmp(argv[1], "write-with-gauge") == 0 && argc > 
3) {
+                       size_t size = file_size(argv[3]);
+                       if (size > 0) {
+                               progress_start(progress_gauge, size);
+                               file_upload(handle, argv[3], strtoul(argv[2], 
NULL, 0));
+                       }
+                       skip=3;
+               } else if (strcmp(argv[1], "write-with-xgauge") == 0 && argc > 
3) {
+                       size_t size = file_size(argv[3]);
+                       if (size > 0) {
+                               progress_start(progress_gauge_xxx, size);
+                               file_upload(handle, argv[3], strtoul(argv[2], 
NULL, 0));
+                       }
                } else if ((strcmp(argv[1], "multiwrite") == 0 ||
                            strcmp(argv[1], "multi") == 0) && argc > 4) {
                        size_t count = strtoul(argv[2], NULL, 0); /* file count 
*/
@@ -1369,6 +1387,48 @@ int main(int argc, char **argv)
 
                                skip += count * 2;
                        }
+               } else if ((strcmp(argv[1], "multiwrite-with-gauge") == 0 ||
+                           strcmp(argv[1], "multi-with-gauge") == 0) && argc > 
4) {
+                       size_t count = strtoul(argv[2], NULL, 0); /* file count 
*/
+                       skip = 2;
+                       if (count > 0) {
+                               unsigned int i;
+
+                               /* get all file sizes, keeping track of total 
bytes */
+                               size_t total_bytes = 0;
+                               for (i = 0; i < count; i++)
+                                       total_bytes += file_size(argv[4 + i*2]);
+
+                               progress_start(progress_gauge, total_bytes);
+
+                               /* now transfer each file in turn */
+                               for (i = 0; i < count; i++)
+                                       file_upload(handle, argv[4 + i*2],
+                                                   strtoul(argv[3 + i*2], 
NULL, 0));
+
+                               skip += count * 2;
+                       }
+               } else if ((strcmp(argv[1], "multiwrite-with-xgauge") == 0 ||
+                           strcmp(argv[1], "multi-with-xgauge") == 0) && argc 
> 4) {
+                       size_t count = strtoul(argv[2], NULL, 0); /* file count 
*/
+                       skip = 2;
+                       if (count > 0) {
+                               unsigned int i;
+
+                               /* get all file sizes, keeping track of total 
bytes */
+                               size_t total_bytes = 0;
+                               for (i = 0; i < count; i++)
+                                       total_bytes += file_size(argv[4 + i*2]);
+
+                               progress_start(progress_gauge_xxx, total_bytes);
+
+                               /* now transfer each file in turn */
+                               for (i = 0; i < count; i++)
+                                       file_upload(handle, argv[4 + i*2],
+                                                   strtoul(argv[3 + i*2], 
NULL, 0));
+
+                               skip += count * 2;
+                       }
                } else if (strcmp(argv[1], "read") == 0 && argc > 4) {
                        size_t size = strtoul(argv[3], NULL, 0);
                        void *buf = malloc(size);
diff --git a/progress.c b/progress.c
index 05d8f8b..47ea893 100644
--- a/progress.c
+++ b/progress.c
@@ -119,3 +119,47 @@ void progress_bar(size_t total, size_t done)
 
        fflush(stdout);
 }
+
+/*
+ * Progress callback that emits percentage numbers, each on a separate line.
+ * The output is suitable for piping it into "dialog --gauge".
+ *
+ * sunxi-fel multiwrite-with-gauge <...> \
+ *     | dialog --title "FEL upload progress" \
+ *              --gauge "" 5 70
+ */
+void progress_gauge(size_t total, size_t done)
+{
+       if (total > 0) {
+               printf("%.0f\n", (float)done / total * 100);
+               fflush(stdout);
+       }
+}
+
+/*
+ * A more sophisticated version of progress_gauge() that also updates the
+ * prompt (caption) with additional information. This uses a feature of
+ * the dialog utility that parses "XXX" delimiters - see 'man dialog'.
+ *
+ * sunxi-fel multiwrite-with-xgauge <...> \
+ *     | dialog --title "FEL upload progress" \
+ *              --backtitle "Please wait..." \
+ *              --gauge "" 6 70
+ */
+void progress_gauge_xxx(size_t total, size_t done)
+{
+       if (total > 0) {
+               double speed = rate(done, progress_elapsed());
+               double eta = estimate(total - done, speed);
+               printf("XXX\n");
+               printf("%.0f\n", (float)done / total * 100);
+               if (done < total)
+                       printf("%zu of %zu, %.1f kB/s, ETA %s\n",
+                               done, total, kilo(speed), format_ETA(eta));
+               else
+                       printf("Done: %.1f kB, at %.1f kB/s\n",
+                               kilo(done), kilo(speed));
+               printf("XXX\n");
+               fflush(stdout);
+       }
+}
diff --git a/progress.h b/progress.h
index 5eb767f..ccd2945 100644
--- a/progress.h
+++ b/progress.h
@@ -36,5 +36,7 @@ void progress_update(size_t bytes_done);
 
 /* progress callback implementations for various display styles */
 void progress_bar(size_t total, size_t done);
+void progress_gauge(size_t total, size_t done);
+void progress_gauge_xxx(size_t total, size_t done);
 
 #endif /* _SUNXI_TOOLS_PROGRESS_H */
-- 
2.4.6

-- 
You received this message because you are subscribed to the Google Groups 
"linux-sunxi" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to linux-sunxi+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to