Pass a file descriptor to getcols and flush_term_input

This makes these methods a bit more flexible.

Signed-off-by: Dan McGee <dan@archlinux.org>
This commit is contained in:
Dan McGee 2012-03-16 17:05:06 -05:00
parent e6f72c61a0
commit 0972b7acfd
3 changed files with 19 additions and 18 deletions

View File

@ -384,7 +384,7 @@ void cb_progress(alpm_progress_t event, const char *pkgname, int percent,
int len, wclen, wcwid, padwid; int len, wclen, wcwid, padwid;
wchar_t *wcstr; wchar_t *wcstr;
const unsigned short cols = getcols(); const unsigned short cols = getcols(fileno(stdout));
if(config->noprogressbar || cols == 0) { if(config->noprogressbar || cols == 0) {
return; return;
@ -544,7 +544,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 unsigned short cols = getcols(); const unsigned short cols = getcols(fileno(stdout));
if(config->noprogressbar || cols == 0 || file_total == -1) { if(config->noprogressbar || cols == 0 || file_total == -1) {
if(file_xfered == 0) { if(file_xfered == 0) {

View File

@ -130,10 +130,10 @@ int check_syncdbs(size_t need_repos, int check_valid)
} }
/* discard unhandled input on the terminal's input buffer */ /* discard unhandled input on the terminal's input buffer */
static int flush_term_input(void) { static int flush_term_input(int fd) {
#ifdef HAVE_TCFLUSH #ifdef HAVE_TCFLUSH
if(isatty(fileno(stdin))) { if(isatty(fd)) {
return tcflush(fileno(stdin), TCIFLUSH); return tcflush(fd, TCIFLUSH);
} }
#endif #endif
@ -142,24 +142,24 @@ static int flush_term_input(void) {
} }
/* gets the current screen column width */ /* gets the current screen column width */
unsigned short getcols(void) unsigned short getcols(int fd)
{ {
const unsigned short default_tty = 80; const unsigned short default_tty = 80;
const unsigned short default_notty = 0; const unsigned short default_notty = 0;
unsigned short termwidth = 0; unsigned short termwidth = 0;
if(!isatty(fileno(stdout))) { if(!isatty(fd)) {
return default_notty; return default_notty;
} }
#ifdef TIOCGSIZE #if defined(TIOCGSIZE)
struct ttysize win; struct ttysize win;
if(ioctl(1, TIOCGSIZE, &win) == 0) { if(ioctl(fd, TIOCGSIZE, &win) == 0) {
termwidth = 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(fd, TIOCGWINSZ, &win) == 0) {
termwidth = win.ws_col; termwidth = win.ws_col;
} }
#endif #endif
@ -259,7 +259,7 @@ void indentprint(const char *str, size_t indent)
wchar_t *wcstr; wchar_t *wcstr;
const wchar_t *p; const wchar_t *p;
int len, cidx; int len, cidx;
const unsigned short cols = getcols(); const unsigned short cols = getcols(fileno(stdout));
if(!str) { if(!str) {
return; return;
@ -622,7 +622,7 @@ int table_display(const char *title, const alpm_list_t *header,
totalwidth = table_calc_widths(header, rows, padding, totalcols, totalwidth = table_calc_widths(header, rows, padding, totalcols,
&widths, &has_data); &widths, &has_data);
/* return -1 if terminal is not wide enough */ /* return -1 if terminal is not wide enough */
if(totalwidth > getcols()) { if(totalwidth > getcols(fileno(stdout))) {
pm_printf(ALPM_LOG_WARNING, pm_printf(ALPM_LOG_WARNING,
_("insufficient columns available for table display\n")); _("insufficient columns available for table display\n"));
return -1; return -1;
@ -660,7 +660,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 unsigned short maxcols = getcols(); const unsigned short maxcols = getcols(fileno(stdout));
size_t cols = len; size_t cols = len;
const char *str = list->data; const char *str = list->data;
fputs(str, stdout); fputs(str, stdout);
@ -1377,7 +1377,7 @@ int multiselect_question(char *array, int count)
break; break;
} }
flush_term_input(); flush_term_input(fileno(stdin));
if(fgets(response, response_len, stdin)) { if(fgets(response, response_len, stdin)) {
const size_t response_incr = 64; const size_t response_incr = 64;
@ -1440,7 +1440,7 @@ int select_question(int count)
break; break;
} }
flush_term_input(); flush_term_input(fileno(stdin));
if(fgets(response, sizeof(response), stdin)) { if(fgets(response, sizeof(response), stdin)) {
size_t len = strtrim(response); size_t len = strtrim(response);
@ -1463,6 +1463,7 @@ static int question(short preset, char *fmt, va_list args)
{ {
char response[32]; char response[32];
FILE *stream; FILE *stream;
int fd_in = fileno(stdin);
if(config->noconfirm) { if(config->noconfirm) {
stream = stdout; stream = stdout;
@ -1489,7 +1490,7 @@ static int question(short preset, char *fmt, va_list args)
} }
fflush(stream); fflush(stream);
flush_term_input(); flush_term_input(fd_in);
if(fgets(response, sizeof(response), stdin)) { if(fgets(response, sizeof(response), stdin)) {
size_t len = strtrim(response); size_t len = strtrim(response);
@ -1499,7 +1500,7 @@ static int question(short preset, char *fmt, va_list args)
/* if stdin is piped, response does not get printed out, and as a result /* if stdin is piped, response does not get printed out, and as a result
* a \n is missing, resulting in broken output (FS#27909) */ * a \n is missing, resulting in broken output (FS#27909) */
if(!isatty(fileno(stdin))) { if(!isatty(fd_in)) {
fprintf(stream, "%s\n", response); fprintf(stream, "%s\n", response);
} }

View File

@ -47,7 +47,7 @@ int trans_init(alpm_transflag_t flags, int check_valid);
int trans_release(void); int trans_release(void);
int needs_root(void); int needs_root(void);
int check_syncdbs(size_t need_repos, int check_valid); int check_syncdbs(size_t need_repos, int check_valid);
unsigned short getcols(void); unsigned short getcols(int fd);
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);