From f8f07ca39bd617ddaeb2149f138b12aa7a6532bf Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Thu, 11 Apr 2024 16:17:46 +0300 Subject: Multiline printing & fixes to output space padding --- config-parser.c | 8 +++++++- config-parser.h | 3 ++- stdu.c | 38 ++++++++++++++++++++++++-------------- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/config-parser.c b/config-parser.c index 075ef47..3cebc73 100644 --- a/config-parser.c +++ b/config-parser.c @@ -1,5 +1,4 @@ - #include #include #include @@ -16,6 +15,7 @@ Result parse_config(int argc, char* argv[]) { res.success = false; Config tmp; tmp.precision = 0; + tmp.multiline = false; tmp.human_readable = false; tmp.help = false; @@ -35,6 +35,11 @@ Result parse_config(int argc, char* argv[]) { || strcmp(argument, "-h") == 0 ) { tmp.human_readable = true; + } else if ( + strcmp(argument, "--multiline") == 0 + || strcmp(argument, "-m") == 0 + ) { + tmp.multiline = true; } else if ( comp1 || strncmp(argument, "-p", 2) == 0 ) { @@ -124,6 +129,7 @@ Result parse_config(int argc, char* argv[]) { Config* conf = malloc(sizeof(int) + sizeof(bool)); conf->help = tmp.help; conf->human_readable = tmp.human_readable; + conf->multiline = tmp.multiline; conf->precision = tmp.precision; res.success = true; diff --git a/config-parser.h b/config-parser.h index 08aa934..3446826 100644 --- a/config-parser.h +++ b/config-parser.h @@ -1,8 +1,9 @@ struct Config { - int precision; bool human_readable; bool help; + bool multiline; + int precision; }; typedef struct Config Config; struct Result { diff --git a/stdu.c b/stdu.c index 466d2f5..0c3c7a6 100644 --- a/stdu.c +++ b/stdu.c @@ -29,10 +29,11 @@ int main (int argc, char* argv[]) { if (conf->help) { printf( -"Usage: stdu [-h] [-p n]\n\ +"Usage: stdu [-hm?] [-p n]\n\ Print amount of data piped to stdin.\n\ --help | -?: print this help message\n\ --human-readable | -h: output in a human readable format\n\ +--multiline | -m: output with line breaks\n\ --precision n | -p n | -pn: output with n significant digits\n " ); return 0; @@ -44,6 +45,11 @@ Print amount of data piped to stdin.\n\ base = 1024; } + char* endl = ""; + if (conf->multiline) { + endl = "\n"; + } + int blocksize = 1; size_t bufsize = 1; int bufsize_tries = 10; @@ -85,35 +91,39 @@ Print amount of data piped to stdin.\n\ ); if (res < 0) { printf("\r%llu \ - (error when getting prefix)", - bytes_read); + (error when getting prefix)%s", + bytes_read, endl); continue; } printf( - "\r%*sB", - previous_line_strlen, - stdout_buffer + "\r%sB%*s", + stdout_buffer, + int_max(previous_line_strlen - res, 0), + endl ); previous_line_strlen = int_max(res, previous_line_strlen); } else if (conf->human_readable && base == 1024) { - int success = ull_floored_with_binary_prefix( + int res = ull_floored_with_binary_prefix( &stdout_buffer, &stdout_buffer_size, bytes_read ); - if (success < 0) { + if (res < 0) { printf("\r%llu \ - (error when getting prefix)", - bytes_read); + (error when getting prefix)%s", + bytes_read, endl); } printf( - "\r%*sB", - 4 + (bytes_read > 1024), - stdout_buffer + "\r%sB%*s", + stdout_buffer, + int_max(previous_line_strlen - res, 0), + endl ); + previous_line_strlen + = int_max(res, previous_line_strlen); } else { - printf("\r%llu", bytes_read); + printf("\r%llu%s", bytes_read, endl); } if (fflush(stdout) == EOF) { printf("\n"); -- cgit v1.2.3