diff options
author | Junio C Hamano <gitster@pobox.com> | 2012-02-21 15:00:15 -0800 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2012-02-21 15:00:15 -0800 |
commit | c17ff2a36160ae51a1cefa9fe8f067dbeb33e884 (patch) | |
tree | 1599544b005ae2df04dcbca7c43b1ab1988e52c4 /pager.c | |
parent | 1e2545c6878665f2092940eab1aaeb20703f7d14 (diff) | |
parent | ad6c3739a33586ba15a8c5c245dcd59e8a31cef1 (diff) | |
download | git-c17ff2a36160ae51a1cefa9fe8f067dbeb33e884.tar.gz git-c17ff2a36160ae51a1cefa9fe8f067dbeb33e884.tar.xz |
Merge branch 'zj/term-columns' into maint
* zj/term-columns:
pager: find out the terminal width before spawning the pager
Diffstat (limited to 'pager.c')
-rw-r--r-- | pager.c | 37 |
1 files changed, 37 insertions, 0 deletions
@@ -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,34 @@ 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; +} |