X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=pager.c;h=05584dead6728ceff818630fbccaa91bb6c6b686;hb=926b1ec63ee045503f609e88ca445b94c06bd5d7;hp=975955ba82a0dbb128d6733090cd74c2b509ea81;hpb=030a360849e9cb2b3c408cbf9d8c980f3aae5868;p=git.git diff --git a/pager.c b/pager.c index 975955ba8..05584dead 100644 --- a/pager.c +++ b/pager.c @@ -76,6 +76,12 @@ void setup_pager(void) if (!pager) return; + /* + * force computing the width of the terminal before we redirect + * the standard output to the pager. + */ + (void) term_columns(); + setenv("GIT_PAGER_IN_USE", "true", 1); /* spawn the pager */ @@ -110,3 +116,46 @@ int pager_in_use(void) env = getenv("GIT_PAGER_IN_USE"); return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0; } + +/* + * Return cached value (if set) or $COLUMNS environment variable (if + * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive), + * and default to 80 if all else fails. + */ +int term_columns(void) +{ + static int term_columns_at_startup; + + char *col_string; + int n_cols; + + if (term_columns_at_startup) + return term_columns_at_startup; + + term_columns_at_startup = 80; + + col_string = getenv("COLUMNS"); + if (col_string && (n_cols = atoi(col_string)) > 0) + term_columns_at_startup = n_cols; +#ifdef TIOCGWINSZ + else { + struct winsize ws; + if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col) + term_columns_at_startup = ws.ws_col; + } +#endif + + return term_columns_at_startup; +} + +/* + * How many columns do we need to show this number in decimal? + */ +int decimal_width(int number) +{ + int i, width; + + for (width = 1, i = 10; i <= number; width++) + i *= 10; + return width; +}