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 <zbyszek@in.waw.pl> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Zbigniew Jędrzejewski-Szmek committed
Feb 12, 2012 at 15:12 UTC
ad6c3739a33586ba15a8c5c245dcd59e8a31cef1
3 files changed
+38
-22
cache.h
+1
@@ -1172,6 +1172,7 @@ extern void setup_pager(void);
1172
extern const char *pager_program;
1173
extern int pager_in_use(void);
1174
extern int pager_use_color;
1175
+extern int term_columns(void);
1176
1177
extern const char *editor_program;
1178
extern const char *askpass_program;
help.c
-22
@@ -5,28 +5,6 @@
5
#include "help.h"
6
#include "common-cmds.h"
7
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
-
8
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
9
{
10
struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
pager.c
+37
@@ -76,6 +76,12 @@ void setup_pager(void)
76
if (!pager)
77
return;
78
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
+
85
setenv("GIT_PAGER_IN_USE", "true", 1);
86
87
/* spawn the pager */
@@ -110,3 +116,34 @@ int pager_in_use(void)
116
env = getenv("GIT_PAGER_IN_USE");
117
return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
118
}
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
+}