diff options
author | 2016-12-13 10:38:12 -0500 | |
---|---|---|
committer | 2016-12-13 10:38:12 -0500 | |
commit | 2461b39eb7c963ef145a7da21133e49b9858b7c6 (patch) | |
tree | d7e43e64245b697486bbc71ab016cbf75fd0d1be | |
parent | 0a61f0be8b0f92a222a3903203ff5553e7644c9f (diff) | |
parent | 1b462f74345f0549157d3c57c80d562bf19010b3 (diff) |
Merge pull request #1830 from npwoods/imgtool_dir_formatting_fix
[Imgtool] Cleaned up the output of the 'dir' command
-rw-r--r-- | src/tools/imgtool/main.cpp | 29 |
1 files changed, 21 insertions, 8 deletions
diff --git a/src/tools/imgtool/main.cpp b/src/tools/imgtool/main.cpp index 79eec751834..5416b6db435 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]; std::string 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), "<DIR>"); - else - snprintf(buf, sizeof(buf), "%u", (unsigned int) ent.filesize); + std::string filesize_string = ent.directory + ? "<DIR>" + : 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, " <hl>"); - 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); |