mirror of
https://github.com/moparisthebest/pacman
synced 2025-01-09 13:07:58 -05:00
pacman/util.c: support terminals with unknown width
Add detection for stdout being attached to a tty device. When this check fails, return a default width of 0, which callers interpret to mean "don't wrap". Conversely, when our term ioctl suceeds but returns 0, we interpret this to mean a tty with an unknown width (e.g., a serial console), in which case we default to a sane value of 80. Signed-off-by: Dave Reisner <d@falconindy.com>
This commit is contained in:
parent
0f26e3aa5b
commit
620cddfc13
@ -355,7 +355,7 @@ void cb_trans_progress(pmtransprog_t event, const char *pkgname, int percent,
|
|||||||
int len, wclen, wcwid, padwid;
|
int len, wclen, wcwid, padwid;
|
||||||
wchar_t *wcstr;
|
wchar_t *wcstr;
|
||||||
|
|
||||||
const int cols = getcols(0);
|
const int cols = getcols();
|
||||||
|
|
||||||
if(config->noprogressbar || cols == 0) {
|
if(config->noprogressbar || cols == 0) {
|
||||||
return;
|
return;
|
||||||
@ -504,7 +504,7 @@ void cb_dl_progress(const char *filename, off_t file_xfered, off_t file_total)
|
|||||||
const char *rate_label, *xfered_label;
|
const char *rate_label, *xfered_label;
|
||||||
int file_percent = 0, total_percent = 0;
|
int file_percent = 0, total_percent = 0;
|
||||||
|
|
||||||
const int cols = getcols(0);
|
const int cols = getcols();
|
||||||
|
|
||||||
if(config->noprogressbar || cols == 0 || file_total == -1) {
|
if(config->noprogressbar || cols == 0 || file_total == -1) {
|
||||||
if(file_xfered == 0) {
|
if(file_xfered == 0) {
|
||||||
|
@ -117,20 +117,28 @@ static int flush_term_input(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* gets the current screen column width */
|
/* gets the current screen column width */
|
||||||
int getcols(int def)
|
int getcols()
|
||||||
{
|
{
|
||||||
|
int termwidth = -1;
|
||||||
|
const int default_tty = 80;
|
||||||
|
const int default_notty = 0;
|
||||||
|
|
||||||
|
if(!isatty(fileno(stdout))) {
|
||||||
|
return default_notty;
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef TIOCGSIZE
|
#ifdef TIOCGSIZE
|
||||||
struct ttysize win;
|
struct ttysize win;
|
||||||
if(ioctl(1, TIOCGSIZE, &win) == 0) {
|
if(ioctl(1, TIOCGSIZE, &win) == 0) {
|
||||||
return win.ts_cols;
|
termwidth = win.ts_cols;
|
||||||
}
|
}
|
||||||
#elif defined(TIOCGWINSZ)
|
#elif defined(TIOCGWINSZ)
|
||||||
struct winsize win;
|
struct winsize win;
|
||||||
if(ioctl(1, TIOCGWINSZ, &win) == 0) {
|
if(ioctl(1, TIOCGWINSZ, &win) == 0) {
|
||||||
return win.ws_col;
|
termwidth = win.ws_col;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return def;
|
return termwidth <= 0 ? default_tty : termwidth;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* does the same thing as 'rm -rf' */
|
/* does the same thing as 'rm -rf' */
|
||||||
@ -226,7 +234,7 @@ void indentprint(const char *str, int indent)
|
|||||||
wchar_t *wcstr;
|
wchar_t *wcstr;
|
||||||
const wchar_t *p;
|
const wchar_t *p;
|
||||||
int len, cidx;
|
int len, cidx;
|
||||||
const int cols = getcols(0);
|
const int cols = getcols();
|
||||||
|
|
||||||
if(!str) {
|
if(!str) {
|
||||||
return;
|
return;
|
||||||
@ -516,8 +524,8 @@ static alpm_list_t *table_create_format(const alpm_list_t *header,
|
|||||||
alpm_list_free(longest_strs);
|
alpm_list_free(longest_strs);
|
||||||
|
|
||||||
/* return NULL if terminal is not wide enough */
|
/* return NULL if terminal is not wide enough */
|
||||||
if(totalwidth > getcols(80)) {
|
if(totalwidth > getcols()) {
|
||||||
pm_fprintf(stderr, PM_LOG_WARNING, _("insufficient columns available for table display\n"));
|
fprintf(stderr, _("insufficient columns available for table display\n"));
|
||||||
FREELIST(formats);
|
FREELIST(formats);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@ -578,7 +586,7 @@ void list_display(const char *title, const alpm_list_t *list)
|
|||||||
if(!list) {
|
if(!list) {
|
||||||
printf("%s\n", _("None"));
|
printf("%s\n", _("None"));
|
||||||
} else {
|
} else {
|
||||||
const int maxcols = getcols(0);
|
const int maxcols = getcols();
|
||||||
int cols = len;
|
int cols = len;
|
||||||
const char *str = alpm_list_getdata(list);
|
const char *str = alpm_list_getdata(list);
|
||||||
printf("%s", str);
|
printf("%s", str);
|
||||||
|
@ -42,7 +42,7 @@
|
|||||||
int trans_init(pmtransflag_t flags);
|
int trans_init(pmtransflag_t flags);
|
||||||
int trans_release(void);
|
int trans_release(void);
|
||||||
int needs_root(void);
|
int needs_root(void);
|
||||||
int getcols(int def);
|
int getcols(void);
|
||||||
int rmrf(const char *path);
|
int rmrf(const char *path);
|
||||||
const char *mbasename(const char *path);
|
const char *mbasename(const char *path);
|
||||||
char *mdirname(const char *path);
|
char *mdirname(const char *path);
|
||||||
|
Loading…
Reference in New Issue
Block a user