curl: make --help adapt to the terminal width

Instead of assuming and working with 80 colums, try figuring out what
width is actually used.

Ref: #13141

Closes #13171
This commit is contained in:
Daniel Stenberg 2024-03-25 13:07:48 +01:00
parent 97c417ae28
commit 2efc111ea4
No known key found for this signature in database
GPG key ID: 5CC908FDB71E12C2
3 changed files with 49 additions and 37 deletions

View file

@ -229,28 +229,25 @@ int tool_progress_cb(void *clientp,
return 0;
}
void progressbarinit(struct ProgressData *bar,
struct OperationConfig *config)
/*
* get_terminal_columns() returns the number of columns in the current
* terminal. It will return 79 on failure. Also, the number can be very big.
*/
unsigned int get_terminal_columns(void)
{
char *colp;
memset(bar, 0, sizeof(struct ProgressData));
/* pass the resume from value through to the progress function so it can
* display progress towards total file not just the part that's left. */
if(config->use_resume)
bar->initial_size = config->resume_from;
colp = curlx_getenv("COLUMNS");
unsigned int width = 0;
char *colp = curlx_getenv("COLUMNS");
if(colp) {
char *endptr;
long num = strtol(colp, &endptr, 10);
if((endptr != colp) && (endptr == colp + strlen(colp)) && (num > 20) &&
(num < 10000))
bar->width = (int)num;
width = (unsigned int)num;
curl_free(colp);
}
if(!bar->width) {
if(!width) {
int cols = 0;
#ifdef TIOCGSIZE
@ -277,14 +274,30 @@ void progressbarinit(struct ProgressData *bar,
}
}
#endif /* TIOCGSIZE */
if(cols > 20)
bar->width = cols;
if(cols < 10000)
width = cols;
}
if(!width)
width = 79;
return width; /* 79 for unknown, might also be very small or very big */
}
if(!bar->width)
bar->width = 79;
else if(bar->width > MAX_BARLENGTH)
void progressbarinit(struct ProgressData *bar,
struct OperationConfig *config)
{
int cols;
memset(bar, 0, sizeof(struct ProgressData));
/* pass the resume from value through to the progress function so it can
* display progress towards total file not just the part that's left. */
if(config->use_resume)
bar->initial_size = config->resume_from;
cols = get_terminal_columns();
if(cols > MAX_BARLENGTH)
bar->width = MAX_BARLENGTH;
else if(cols > 20)
bar->width = cols;
bar->out = tool_stderr;
bar->tick = 150;