diff --git a/src/p7/main.c b/src/p7/main.c index b0d6970..c11c888 100644 --- a/src/p7/main.c +++ b/src/p7/main.c @@ -72,7 +72,6 @@ static const char error_nospace[] = * @return if the file overwriting is confirmed */ -static int sendfile_display_initialized = 0; static int sendfile_confirm(void) { char line[10]; @@ -99,6 +98,7 @@ static int sendfile_confirm(void) * @arg total total number of data packets */ +static int sendfile_display_initialized = 0; static void sendfile_display(p7ushort_t id, p7ushort_t total) { /* here's the buffer */ diff --git a/src/p7os/args.c b/src/p7os/args.c index 6b7b838..9ceb74a 100644 --- a/src/p7os/args.c +++ b/src/p7os/args.c @@ -105,7 +105,7 @@ int parse_args(int ac, char **av, args_t *args) }; /* define options */ - const char shopts[] = "hvo:"; + const char shopts[] = "hvo:#"; const struct option longopts[] = { {"help", no_argument, NULL, 'h'}, {"version", no_argument, NULL, 'v'}, diff --git a/src/p7os/main.c b/src/p7os/main.c index b915f06..c919588 100644 --- a/src/p7os/main.c +++ b/src/p7os/main.c @@ -35,6 +35,61 @@ static const char error_unsupported[] = "Required operation was unsupported by the calculator.\n" "If you did not prepare, perhaps you should prepare?\n"; +/* ************************************************************************** */ +/* Auxiliary functions */ +/* ************************************************************************** */ +/** + * osdisp: + * Nice little loading bar. + * + * Taken from `src/p7/main.c`. + * "Initialization" is when id > total (called in main). + * + * @arg id data packet ID. + * @arg total total number of packets. + */ + +static int osdisp_init = 0; +static void osdisp(p7ushort_t id, p7ushort_t total) +{ + /* here's the buffer */ + static char buf[50] = + "\r|---------------------------------------| 00.00%"; + static char *bar = &buf[2]; + + /* initialize */ + static int pos; + + /* if is initialize, fill */ + if (id > total) { + pos = 0; + /* indicate that is has been initialized */ + osdisp_init = 1; + /* put initial buffer */ + fputs(buf, stdout); + /* save cursor position */ + fputs("\x1B[s", stdout); + /* we're done */ + return ; + } + + /* id and total start from 1, let them start from zero */ + id--; total--; + + /* modify buffer */ + /* - # - */ + int current = 38 * id / total; + while (pos <= current) bar[pos++] = '#'; + /* - % - */ + unsigned int percent = 10000 * id / total; + sprintf(&buf[43], "%02u.%02u", percent / 100, percent % 100); + + /* put it */ + fputs(buf, stdout); + /* force cursor position */ + fputs("\x1B""8", stdout); +} + /* ************************************************************************** */ /* Main function */ /* ************************************************************************** */ @@ -64,8 +119,13 @@ int main(int ac, char **av) /* prepare */ if (!args.noprepare) { /* make the preparation thing */ - if ((err = prepare_ops(handle, args.uexe))) + printf("Uploading the Update.Exe.\n"); + if ((err = prepare_ops(handle, args.uexe, osdisp))) goto fail; + if (osdisp_init) { + osdisp_init = 0; + puts("\b\b\b\b\b\bTransfer complete."); + } handle = NULL; /* was only about preparing? */ @@ -73,12 +133,16 @@ int main(int ac, char **av) return (0); /* sleep a little, in case */ + printf("Waiting for the Update.Exe to be setup...\n"); sleep(1); /* re-open the handle */ if (args.com) err = p7_cominit(&handle, initflags, args.com); else err = p7_init(&handle, initflags); - if (err) goto fail; + if (err) { + p7_exit(handle); + goto fail; + } } /* check according to menu */ @@ -86,7 +150,8 @@ int main(int ac, char **av) /* backup the thing menu */ case mn_get: /* get the os */ - err = p7_backup_rom(handle, args.local); + printf("Gathering the OS...\n"); + err = p7_backup_rom(handle, args.local, osdisp); if (err) goto fail; fclose(args.local); break; @@ -99,6 +164,10 @@ int main(int ac, char **av) return (0); fail: + /* interrupt loading bar */ + if (osdisp_init) + puts("\b\b\b\b\b\bError !"); + /* displaying error */ if (err > 0) switch (err) { case p7_error_nocalc: log(error_noconnexion); break; diff --git a/src/p7os/main.h b/src/p7os/main.h index da7dfb4..733ab86 100644 --- a/src/p7os/main.h +++ b/src/p7os/main.h @@ -42,7 +42,7 @@ int parse_args(int ac, char **av, args_t *args); /* Actual things */ /* ************************************************************************** */ /* Main functions */ -int prepare_ops(p7_handle_t *handle, FILE *uexe); +int prepare_ops(p7_handle_t *handle, FILE *uexe, void (*disp)()); int get_os(p7_handle_t *handle, FILE *dest); #endif /* MAIN_H */ diff --git a/src/p7os/prepare.c b/src/p7os/prepare.c index 89e0eac..36a3f11 100644 --- a/src/p7os/prepare.c +++ b/src/p7os/prepare.c @@ -36,7 +36,7 @@ extern char _binary_cake_exe_bin_end[]; * @return the error (-1 if not a libp7 error, 0 if ok) */ -int prepare_ops(p7_handle_t *handle, FILE *uexe) +int prepare_ops(p7_handle_t *handle, FILE *uexe, void (*disp)()) { int err; size_t usize; @@ -58,7 +58,7 @@ int prepare_ops(p7_handle_t *handle, FILE *uexe) /* send the thing */ if ((err = p7_sendexe_stream(handle, uexe, (p7uint_t)usize, - 0x88030000, 0x88030000))) { + 0x88030000, 0x88030000, disp))) { fclose(uexe); return (err); }