#define DFU_IOQ_MAXLEN 5
+#define DFU_PROG_BAR_NAME_WIDTH 20
+#define DFU_PROG_BAR_WIDTH 50
+
enum dfu_entry_attr {
DFU_INFO_MODE = 0,
DFU_INFO_PARTITION,
free(e);
}
+static inline void print_bar_label(const char *label)
+{
+ fprintf(stdout, "\n[ %*s |", DFU_PROG_BAR_NAME_WIDTH, label);
+}
+
+static void print_bar(uint64_t progress, uint64_t total, int *prev_rate)
+{
+ int rate = (progress * DFU_PROG_BAR_WIDTH) / total;
+ int bar_margin = DFU_PROG_BAR_NAME_WIDTH + 4;
+ int i;
+
+ if (*prev_rate == rate) {
+ fprintf(stdout, "\x1b[%dC", bar_margin + DFU_PROG_BAR_WIDTH);
+ goto skip_bar;
+ }
+
+ *prev_rate = rate;
+
+ fprintf(stdout, "\x1b[%dC", bar_margin);
+
+ for (i = 0; i < DFU_PROG_BAR_WIDTH; i++) {
+ if (i < rate)
+ fputc('=', stdout);
+ else if (i == rate)
+ fputc('>', stdout);
+ else
+ fputc(' ', stdout);
+ }
+
+skip_bar:
+ fprintf(stdout, "] (%5.1f%%)",
+ (double)progress / (double)total * 100.0F);
+}
+
+static void print_progress(struct dfu_entry *e, uint64_t progress)
+{
+ struct dfu_context *ctx = e->ctx;
+ static int nth_entry = 1;
+ static uint64_t completed;
+ static int prev_total_rate;
+ static int prev_entry_rate;
+ uint64_t total_progress;
+
+ total_progress = completed + progress;
+
+ fprintf(stdout, "\x1b[%dA\n\x1b[1A", nth_entry);
+ print_bar(total_progress, ctx->total_size, &prev_total_rate);
+
+ fprintf(stdout, "\x1b[%dB\n\x1b[1A", nth_entry);
+ print_bar(progress, e->file_size, &prev_entry_rate);
+
+ /* prepare for next entry if current is finished */
+ if (progress == e->file_size) {
+ completed += e->file_size;
+ nth_entry++;
+ }
+
+ if (total_progress == ctx->total_size)
+ fprintf(stdout, "\nDownload Finished\n");
+ fflush(stdout);
+}
+
static void *dfu_thread_main(void *ptr)
{
struct dfu_entry *e = ptr;
ctx->ioq_len--;
pthread_mutex_unlock(&ctx->mutex);
- fprintf(stdout, "#");
- fflush(stdout);
+ print_progress(e, progress);
free(frame->buf);
free(frame);
{
ctx->total_size = size;
- fprintf(stdout, "Start Download (Total: %.2lfMB)\n",
+ fprintf(stdout, "Start Download (Total: %.2lfMB)",
(double)size / (1024.0f * 1024.0f));
+
+ print_bar_label("Total");
}
struct dfu_entry *dfu_start(struct dfu_context *ctx,
return NULL;
}
- fprintf(stdout, "Start download: %s...", filename);
- fflush(stdout);
+ print_bar_label(filename);
e->ctx = ctx;
pthread_create(&ctx->thread, NULL, dfu_thread_main, e);
umount_dev();
pthread_cond_signal(&ctx->sync_done);
-
- fprintf(stdout, "finished\n");
}
static int parse_dfu_info(char *buf, char **info)