Skip to content

Commit c17ff2a

Browse files
committed
Merge branch 'zj/term-columns' into maint
* zj/term-columns: pager: find out the terminal width before spawning the pager
2 parents 1e2545c + ad6c373 commit c17ff2a

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
@@ -1176,6 +1176,7 @@ extern void setup_pager(void);
11761176
extern const char *pager_program;
11771177
extern int pager_in_use(void);
11781178
extern int pager_use_color;
1179+
extern int term_columns(void);
11791180

11801181
extern const char *editor_program;
11811182
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)