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>
This commit is contained in:
parent
d0482e88a7
commit
ad6c3739a3
1
cache.h
1
cache.h
@ -1172,6 +1172,7 @@ extern void setup_pager(void);
|
|||||||
extern const char *pager_program;
|
extern const char *pager_program;
|
||||||
extern int pager_in_use(void);
|
extern int pager_in_use(void);
|
||||||
extern int pager_use_color;
|
extern int pager_use_color;
|
||||||
|
extern int term_columns(void);
|
||||||
|
|
||||||
extern const char *editor_program;
|
extern const char *editor_program;
|
||||||
extern const char *askpass_program;
|
extern const char *askpass_program;
|
||||||
|
22
help.c
22
help.c
@ -5,28 +5,6 @@
|
|||||||
#include "help.h"
|
#include "help.h"
|
||||||
#include "common-cmds.h"
|
#include "common-cmds.h"
|
||||||
|
|
||||||
/* most GUI terminals set COLUMNS (although some don't export it) */
|
|
||||||
static int term_columns(void)
|
|
||||||
{
|
|
||||||
char *col_string = getenv("COLUMNS");
|
|
||||||
int n_cols;
|
|
||||||
|
|
||||||
if (col_string && (n_cols = atoi(col_string)) > 0)
|
|
||||||
return n_cols;
|
|
||||||
|
|
||||||
#ifdef TIOCGWINSZ
|
|
||||||
{
|
|
||||||
struct winsize ws;
|
|
||||||
if (!ioctl(1, TIOCGWINSZ, &ws)) {
|
|
||||||
if (ws.ws_col)
|
|
||||||
return ws.ws_col;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
return 80;
|
|
||||||
}
|
|
||||||
|
|
||||||
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
|
void add_cmdname(struct cmdnames *cmds, const char *name, int len)
|
||||||
{
|
{
|
||||||
struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
|
struct cmdname *ent = xmalloc(sizeof(*ent) + len + 1);
|
||||||
|
37
pager.c
37
pager.c
@ -76,6 +76,12 @@ void setup_pager(void)
|
|||||||
if (!pager)
|
if (!pager)
|
||||||
return;
|
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);
|
setenv("GIT_PAGER_IN_USE", "true", 1);
|
||||||
|
|
||||||
/* spawn the pager */
|
/* spawn the pager */
|
||||||
@ -110,3 +116,34 @@ int pager_in_use(void)
|
|||||||
env = getenv("GIT_PAGER_IN_USE");
|
env = getenv("GIT_PAGER_IN_USE");
|
||||||
return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
|
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;
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user