2 #include "run-command.h"
6 #define DEFAULT_PAGER "less"
10 * This is split up from the rest of git so that we can do
11 * something different on Windows.
14 static struct child_process pager_process
= CHILD_PROCESS_INIT
;
16 static void wait_for_pager(void)
20 /* signal EOF to pager */
23 finish_command(&pager_process
);
26 static void wait_for_pager_signal(int signo
)
33 const char *git_pager(int stdout_is_tty
)
40 pager
= getenv("GIT_PAGER");
43 git_config(git_default_config
, NULL
);
44 pager
= pager_program
;
47 pager
= getenv("PAGER");
49 pager
= DEFAULT_PAGER
;
50 if (!*pager
|| !strcmp(pager
, "cat"))
56 void setup_pager(void)
58 const char *pager
= git_pager(isatty(1));
64 * force computing the width of the terminal before we redirect
65 * the standard output to the pager.
67 (void) term_columns();
69 setenv("GIT_PAGER_IN_USE", "true", 1);
72 argv_array_push(&pager_process
.args
, pager
);
73 pager_process
.use_shell
= 1;
74 pager_process
.in
= -1;
76 argv_array_push(&pager_process
.env_array
, "LESS=FRX");
78 argv_array_push(&pager_process
.env_array
, "LV=-c");
79 argv_array_push(&pager_process
.env_array
, "GIT_PAGER_IN_USE");
80 if (start_command(&pager_process
))
83 /* original process continues, but writes to the pipe */
84 dup2(pager_process
.in
, 1);
86 dup2(pager_process
.in
, 2);
87 close(pager_process
.in
);
89 /* this makes sure that the parent terminates after the pager */
90 sigchain_push_common(wait_for_pager_signal
);
91 atexit(wait_for_pager
);
94 int pager_in_use(void)
97 env
= getenv("GIT_PAGER_IN_USE");
98 return env
? git_config_bool("GIT_PAGER_IN_USE", env
) : 0;
102 * Return cached value (if set) or $COLUMNS environment variable (if
103 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
104 * and default to 80 if all else fails.
106 int term_columns(void)
108 static int term_columns_at_startup
;
113 if (term_columns_at_startup
)
114 return term_columns_at_startup
;
116 term_columns_at_startup
= 80;
118 col_string
= getenv("COLUMNS");
119 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
120 term_columns_at_startup
= n_cols
;
124 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
)
125 term_columns_at_startup
= ws
.ws_col
;
129 return term_columns_at_startup
;
133 * How many columns do we need to show this number in decimal?
135 int decimal_width(uintmax_t number
)
139 for (width
= 1; number
>= 10; width
++)
144 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
145 int check_pager_config(const char *cmd
)
148 struct strbuf key
= STRBUF_INIT
;
149 const char *value
= NULL
;
150 strbuf_addf(&key
, "pager.%s", cmd
);
151 if (!git_config_get_value(key
.buf
, &value
)) {
152 int b
= git_config_maybe_bool(key
.buf
, value
);
157 pager_program
= xstrdup(value
);
160 strbuf_release(&key
);