From 77167e292006421eb681ab97b57665ab09526e72 Mon Sep 17 00:00:00 2001 From: Vitaliy Filippov Date: Wed, 28 Feb 2024 21:22:19 +0300 Subject: [PATCH] Do not use \r if output is not a terminal --- src/cli.cpp | 2 +- src/cli_common.cpp | 8 +++++++- src/cli_merge.cpp | 16 ++++++++++++---- src/cli_rm_data.cpp | 4 +++- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/src/cli.cpp b/src/cli.cpp index 717e2398..45ea6fce 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -125,7 +125,7 @@ static const char* help_text = " --parallel_osds M Work with M osds in parallel when possible (default 4)\n" " --progress 1|0 Report progress (default 1)\n" " --cas 1|0 Use CAS writes for flatten, merge, rm (default is decide automatically)\n" - " --no-color Disable colored output\n" + " --color 1|0 Enable/disable colored output and CR symbols (default 1 if stdout is a terminal)\n" " --json JSON output\n" ; diff --git a/src/cli_common.cpp b/src/cli_common.cpp index e5b9b9b1..71b465ac 100644 --- a/src/cli_common.cpp +++ b/src/cli_common.cpp @@ -1,6 +1,7 @@ // Copyright (c) Vitaliy Filippov, 2019+ // License: VNPL-1.1 (see README.md for details) +#include #include "str_util.h" #include "cluster_client.h" #include "cli.h" @@ -113,7 +114,12 @@ void cli_tool_t::parse_config(json11::Json::object & cfg) else kv_it++; } - color = !cfg["no_color"].bool_value(); + if (cfg.find("no_color") != cfg.end()) + color = !cfg["no_color"].bool_value(); + else if (cfg.find("color") != cfg.end()) + color = cfg["color"].bool_value(); + else + color = isatty(1); json_output = cfg["json"].bool_value(); iodepth = cfg["iodepth"].uint64_value(); if (!iodepth) diff --git a/src/cli_merge.cpp b/src/cli_merge.cpp index 697c372d..0a24bf54 100644 --- a/src/cli_merge.cpp +++ b/src/cli_merge.cpp @@ -275,7 +275,9 @@ struct snap_merger_t processed++; if (parent->progress && !(processed % 128)) { - printf("\rFiltering target blocks: %ju/%ju", processed, to_process); + fprintf(stderr, parent->color + ? "\rFiltering target blocks: %ju/%ju" + : "Filtering target blocks: %ju/%ju\n", processed, to_process); } } if (in_flight > 0 || oit != merge_offsets.end()) @@ -285,7 +287,9 @@ struct snap_merger_t } if (parent->progress) { - printf("\r%ju full blocks of target filtered out\n", to_process-merge_offsets.size()); + fprintf(stderr, parent->color + ? "\r%ju full blocks of target filtered out\n" + : "%ju full blocks of target filtered out\n", to_process-merge_offsets.size()); } } state = 3; @@ -320,7 +324,9 @@ struct snap_merger_t processed++; if (parent->progress && !(processed % 128)) { - printf("\rOverwriting blocks: %ju/%ju", processed, to_process); + fprintf(stderr, parent->color + ? "\rOverwriting blocks: %ju/%ju" + : "Overwriting blocks: %ju/%ju\n", processed, to_process); } } if (in_flight == 0 && rwo_error.size()) @@ -339,7 +345,9 @@ struct snap_merger_t } if (parent->progress) { - printf("\rOverwriting blocks: %ju/%ju\n", to_process, to_process); + fprintf(stderr, parent->color + ? "\rOverwriting blocks: %ju/%ju\n" + : "Overwriting blocks: %ju/%ju\n", to_process, to_process); } // Done result = (cli_result_t){ .text = "Done, layers from "+from_name+" to "+to_name+" merged into "+target_name }; diff --git a/src/cli_rm_data.cpp b/src/cli_rm_data.cpp index b060a5a5..e88c6baf 100644 --- a/src/cli_rm_data.cpp +++ b/src/cli_rm_data.cpp @@ -213,7 +213,9 @@ struct rm_inode_t } if (parent->progress && total_count > 0 && total_done*1000/total_count != total_prev_pct) { - fprintf(stderr, "\rRemoved %ju/%ju objects, %ju more PGs to list...", total_done, total_count, pgs_to_list); + fprintf(stderr, parent->color + ? "\rRemoved %ju/%ju objects, %ju more PGs to list..." + : "Removed %ju/%ju objects, %ju more PGs to list...\n", total_done, total_count, pgs_to_list); total_prev_pct = total_done*1000/total_count; } if (lists_done && !lists.size())