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 --- stdu.c | 38 ++++++++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 14 deletions(-) (limited to 'stdu.c') 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