From 1b462f74345f0549157d3c57c80d562bf19010b3 Mon Sep 17 00:00:00 2001 From: Nathan Woods Date: Mon, 12 Dec 2016 20:13:28 -0500 Subject: [PATCH] [Imgtool] Cleaned up the output of the 'dir' command --- src/tools/imgtool/main.cpp | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/tools/imgtool/main.cpp b/src/tools/imgtool/main.cpp index f1087798c0d..6ea34d6d6d7 100644 --- a/src/tools/imgtool/main.cpp +++ b/src/tools/imgtool/main.cpp @@ -177,12 +177,21 @@ static int cmd_dir(const struct command *c, int argc, char *argv[]) imgtool::partition::ptr partition; imgtool::directory::ptr imgenum; imgtool_dirent ent; - char buf[512]; char last_modified[19]; const char *path; int partition_index = 0; std::string info; + // build the separator + const int columnwidth_filename = 30; + const int columnwidth_filesize = 8; + const int columnwidth_attributes = 15; + const int columnwidth_lastmodified = 18; + std::string separator = std::string(columnwidth_filename, '-') + " " + + std::string(columnwidth_filesize, '-') + " " + + std::string(columnwidth_attributes, '-') + " " + + std::string(columnwidth_lastmodified, '-'); + // attempt to open image err = imgtool::image::open(argv[0], argv[1], OSD_FOPEN_READ, image); if (err) @@ -209,14 +218,14 @@ static int cmd_dir(const struct command *c, int argc, char *argv[]) info = image->info(); if (!info.empty()) fprintf(stdout, "%s\n", info.c_str()); - fprintf(stdout, "------------------------------ -------- --------------- ------------------\n"); + + fprintf(stdout, "%s\n", separator.c_str()); while (((err = imgenum->get_next(ent)) == 0) && !ent.eof) { - if (ent.directory) - snprintf(buf, sizeof(buf), ""); - else - snprintf(buf, sizeof(buf), "%u", (unsigned int) ent.filesize); + std::string filesize_string = ent.directory + ? "" + : string_format("%u", (unsigned int) ent.filesize); if (ent.lastmodified_time != 0) strftime(last_modified, sizeof(last_modified), "%d-%b-%y %H:%M:%S", @@ -225,7 +234,11 @@ static int cmd_dir(const struct command *c, int argc, char *argv[]) if (ent.hardlink) strcat(ent.filename, " "); - fprintf(stdout, "%-30s %8s %15s %18s\n", ent.filename, buf, ent.attr, last_modified); + fprintf(stdout, "%*s %*s %*s %*s\n", + -columnwidth_filename, ent.filename, + columnwidth_filesize, filesize_string.c_str(), + columnwidth_attributes, ent.attr, + columnwidth_lastmodified, last_modified); if (ent.softlink && ent.softlink[0] != '\0') fprintf(stdout, "-> %s\n", ent.softlink); @@ -244,7 +257,7 @@ static int cmd_dir(const struct command *c, int argc, char *argv[]) if (err) goto done; - fprintf(stdout, "------------------------ ------ ---------------\n"); + fprintf(stdout, "%s\n", separator.c_str()); fprintf(stdout, "%8i File(s) %8i bytes\n", total_count, total_size); if (!freespace_err) fprintf(stdout, " %8u bytes free\n", (unsigned int) freespace);