Since term_columns() will usually fail, when a pager is installed, the cache is primed before the pager is installed. If a pager is not installed, then the cache will be set on first use. Conforms to The Single UNIX Specification, Version 2 (http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html#tag_002_003). Signed-off-by: Zbigniew Jędrzejewski-Szmek <zbyszek@xxxxxxxxx> --- help.c | 31 +++++++++++++++++++++++-------- help.h | 2 ++ pager.c | 5 +++++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/help.c b/help.c index bc15066..75b8d4b 100644 --- a/help.c +++ b/help.c @@ -5,26 +5,41 @@ #include "help.h" #include "common-cmds.h" -/* most GUI terminals set COLUMNS (although some don't export it) */ -static int term_columns(void) +/* cache for term_columns() value. Set on first use or when + * installing a pager and replacing stdout. + */ +static int term_columns_cache; + +/* Return cached value (iff set) or $COLUMNS (iff set and positive) or + * ioctl(1, TIOCGWINSZ).ws_col (if positive) or 80. + * + * $COLUMNS even if set, is usually not exported, so + * the variable can be used to override autodection. + */ +int term_columns(void) { - char *col_string = getenv("COLUMNS"); - int n_cols; + if (term_columns_cache) + return term_columns_cache; - if (col_string && (n_cols = atoi(col_string)) > 0) - return n_cols; + { + char *col_string = getenv("COLUMNS"); + int n_cols; + + if (col_string && (n_cols = atoi(col_string)) > 0) + return (term_columns_cache = n_cols); + } #ifdef TIOCGWINSZ { struct winsize ws; if (!ioctl(1, TIOCGWINSZ, &ws)) { if (ws.ws_col) - return ws.ws_col; + return (term_columns_cache = ws.ws_col); } } #endif - return 80; + return (term_columns_cache = 80); } void add_cmdname(struct cmdnames *cmds, const char *name, int len) diff --git a/help.h b/help.h index b6b12d5..880a4b4 100644 --- a/help.h +++ b/help.h @@ -29,4 +29,6 @@ extern void list_commands(const char *title, struct cmdnames *main_cmds, struct cmdnames *other_cmds); +extern int term_columns(void); + #endif /* HELP_H */ diff --git a/pager.c b/pager.c index 975955b..e7032de 100644 --- a/pager.c +++ b/pager.c @@ -1,6 +1,7 @@ #include "cache.h" #include "run-command.h" #include "sigchain.h" +#include "help.h" #ifndef DEFAULT_PAGER #define DEFAULT_PAGER "less" @@ -76,6 +77,10 @@ void setup_pager(void) if (!pager) return; + /* prime the term_columns() cache before it is too + * late and stdout is replaced */ + (void) term_columns(); + setenv("GIT_PAGER_IN_USE", "true", 1); /* spawn the pager */ -- 1.7.9.rc2.127.gcb239 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html