Skip to content

Commit

Permalink
Add dynamic MAX_PROGRESS_WIDTH global variable to handle long lines (
Browse files Browse the repository at this point in the history
…#435)

* Add dynamic `g_max_process_width` global variable to handle long lines

Fix long line issue when using `uuu` outside of a terminal. eg: python.subprocess.
 `print_auto_scroll`: resize status when longer than console width

Signed-off-by: Thomas Mahé <[email protected]>
  • Loading branch information
thmahe authored Oct 18, 2024
1 parent f0761dc commit 6c2141e
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions uuu/uuu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const char * g_vt_red = "\x1B[91m";
const char * g_vt_kcyn = "\x1B[36m";
const char * g_vt_boldwhite = "\x1B[97m";

size_t g_max_process_width = 0;

void clean_vt_color() noexcept
{
g_vt_yellow = "";
Expand Down Expand Up @@ -323,9 +325,11 @@ string build_process_bar(size_t width, size_t pos, size_t total)

void print_auto_scroll(string str, size_t len, size_t start)
{
g_max_process_width = max(str.size(), g_max_process_width);
g_max_process_width = min(g_max_process_width, len);
if (str.size() <= len)
{
str.resize(len, ' ');
str.resize(g_max_process_width, ' ');
cout << str;
return;
}
Expand All @@ -335,6 +339,13 @@ void print_auto_scroll(string str, size_t len, size_t start)
else
start = 0;

if (str.size() >= len){
str.resize(len - 1);
str[str.size() - 1] = '.';
str[str.size() - 2] = '.';
str[str.size() - 3] = '.';
}

string s = str.substr(start, len);
s.resize(len, ' ');
cout << s;
Expand Down Expand Up @@ -591,6 +602,10 @@ void print_oneline(string str)
if (w <= 3)
return;

if (g_max_process_width == 0){
g_max_process_width = min(str.size(), w);
}

if (str.size() >= w)
{
str.resize(w - 1);
Expand All @@ -600,7 +615,7 @@ void print_oneline(string str)
}
else
{
str.resize(w, ' ');
str.resize(g_max_process_width, ' ');
}
cout << str << endl;

Expand Down Expand Up @@ -654,7 +669,7 @@ int progress(uuu_notify nt, void *p)
else
{
string_ex str;
str.format("\rSuccess %d Failure %d ", g_overall_okay, g_overall_failure);
str.format("\rSuccess %d Failure %d ", g_overall_okay, g_overall_failure);

if (g_map_path_nt.empty())
str += "Wait for Known USB Device Appear...";
Expand Down

0 comments on commit 6c2141e

Please sign in to comment.