diff options
author | Joel Kronqvist <joelkronqvist@proton.me> | 2024-04-11 16:17:46 +0300 |
---|---|---|
committer | Joel Kronqvist <joelkronqvist@proton.me> | 2024-04-13 18:31:30 +0300 |
commit | f8f07ca39bd617ddaeb2149f138b12aa7a6532bf (patch) | |
tree | a47e8624bad148977b9f6014fcb90641a996c8a8 /stdu.c | |
parent | a793df3e2a07c65f36fb2f5f43d2c51ab0c47bf2 (diff) | |
download | stdu-f8f07ca39bd617ddaeb2149f138b12aa7a6532bf.tar.gz stdu-f8f07ca39bd617ddaeb2149f138b12aa7a6532bf.zip |
Multiline printing & fixes to output space padding
Diffstat (limited to 'stdu.c')
-rw-r--r-- | stdu.c | 38 |
1 files changed, 24 insertions, 14 deletions
@@ -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"); |