Skip to content

Commit ad6c373

Browse files
keszybzgitster
authored andcommitted
pager: find out the terminal width before spawning the pager
term_columns() checks for terminal width via ioctl(2) on the standard output, but we spawn the pager too early for this check to be useful. The effect of this buglet can be observed by opening a wide terminal and running "git -p help --all", which still shows 80-column output, while "git help --all" uses the full terminal width. Run the check before we spawn the pager to fix this. While at it, move term_columns() to pager.c and export it from cache.h so that callers other than the help subsystem can use it. Signed-off-by: Zbigniew Jędrzejewski-Szmek <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0482e8 commit ad6c373

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

cache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,6 +1172,7 @@ extern void setup_pager(void);
11721172
extern const char *pager_program;
11731173
extern int pager_in_use(void);
11741174
extern int pager_use_color;
1175+
extern int term_columns(void);
11751176

11761177
extern const char *editor_program;
11771178
extern const char *askpass_program;

help.c

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,6 @@
55
#include "help.h"
66
#include "common-cmds.h"
77

8-
/* most GUI terminals set COLUMNS (although some don't export it) */
9-
static int term_columns(void)
10-
{
11-
char *col_string = getenv("COLUMNS");
12-
int n_cols;
13-
14-
if (col_string && (n_cols = atoi(col_string)) > 0)
15-
return n_cols;
16-
17-
#ifdef TIOCGWINSZ
18-
{
19-
struct winsize ws;
20-
if (!ioctl(1, TIOCGWINSZ, &ws)) {
21-
if (ws.ws_col)
22-
return ws.ws_col;
23-
}
24-
}
25-
#endif
26-
27-
return 80;
28-
}
29-
308
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
319
{
3210
struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);

pager.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,12 @@ void setup_pager(void)
7676
if (!pager)
7777
return;
7878

79+
/*
80+
* force computing the width of the terminal before we redirect
81+
* the standard output to the pager.
82+
*/
83+
(void) term_columns();
84+
7985
setenv("GIT_PAGER_IN_USE", "true", 1);
8086

8187
/* spawn the pager */
@@ -110,3 +116,34 @@ int pager_in_use(void)
110116
env = getenv("GIT_PAGER_IN_USE");
111117
return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
112118
}
119+
120+
/*
121+
* Return cached value (if set) or $COLUMNS environment variable (if
122+
* set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
123+
* and default to 80 if all else fails.
124+
*/
125+
int term_columns(void)
126+
{
127+
static int term_columns_at_startup;
128+
129+
char *col_string;
130+
int n_cols;
131+
132+
if (term_columns_at_startup)
133+
return term_columns_at_startup;
134+
135+
term_columns_at_startup = 80;
136+
137+
col_string = getenv("COLUMNS");
138+
if (col_string && (n_cols = atoi(col_string)) > 0)
139+
term_columns_at_startup = n_cols;
140+
#ifdef TIOCGWINSZ
141+
else {
142+
struct winsize ws;
143+
if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
144+
term_columns_at_startup = ws.ws_col;
145+
}
146+
#endif
147+
148+
return term_columns_at_startup;
149+
}

0 commit comments

Comments
 (0)