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 const char *pager_argv[] = { NULL, NULL };
15 static struct child_process pager_process = CHILD_PROCESS_INIT;
17 static void wait_for_pager(void)
21 /* signal EOF to pager */
24 finish_command(&pager_process);
27 static void wait_for_pager_signal(int signo)
34 const char *git_pager(int stdout_is_tty)
41 pager = getenv("GIT_PAGER");
44 git_config(git_default_config, NULL);
45 pager = pager_program;
48 pager = getenv("PAGER");
50 pager = DEFAULT_PAGER;
51 if (!*pager || !strcmp(pager, "cat"))
57 void setup_pager(void)
59 const char *pager = git_pager(isatty(1));
65 * force computing the width of the terminal before we redirect
66 * the standard output to the pager.
68 (void) term_columns();
70 setenv("GIT_PAGER_IN_USE", "true", 1);
73 pager_argv[0] = pager;
74 pager_process.use_shell = 1;
75 pager_process.argv = pager_argv;
76 pager_process.in = -1;
77 if (!getenv("LESS") || !getenv("LV")) {
78 static const char *env[3];
82 env[i++] = "LESS=FRX";
86 pager_process.env = env;
88 if (start_command(&pager_process))
91 /* original process continues, but writes to the pipe */
92 dup2(pager_process.in, 1);
94 dup2(pager_process.in, 2);
95 close(pager_process.in);
97 /* this makes sure that the parent terminates after the pager */
98 sigchain_push_common(wait_for_pager_signal);
99 atexit(wait_for_pager);
102 int pager_in_use(void)
105 env = getenv("GIT_PAGER_IN_USE");
106 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
110 * Return cached value (if set) or $COLUMNS environment variable (if
111 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
112 * and default to 80 if all else fails.
114 int term_columns(void)
116 static int term_columns_at_startup;
121 if (term_columns_at_startup)
122 return term_columns_at_startup;
124 term_columns_at_startup = 80;
126 col_string = getenv("COLUMNS");
127 if (col_string && (n_cols = atoi(col_string)) > 0)
128 term_columns_at_startup = n_cols;
132 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
133 term_columns_at_startup = ws.ws_col;
137 return term_columns_at_startup;
141 * How many columns do we need to show this number in decimal?
143 int decimal_width(int number)
147 for (width = 1, i = 10; i <= number; width++)
152 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
153 int check_pager_config(const char *cmd)
156 struct strbuf key = STRBUF_INIT;
157 const char *value = NULL;
158 strbuf_addf(&key, "pager.%s", cmd);
159 if (!git_config_get_value(key.buf, &value)) {
160 int b = git_config_maybe_bool(key.buf, value);
165 pager_program = xstrdup(value);
168 strbuf_release(&key);