2 #include "run-command.h"
6 #define DEFAULT_PAGER "less"
9 static struct child_process pager_process = CHILD_PROCESS_INIT;
10 static const char *pager_program;
12 static void wait_for_pager(int in_signal)
18 /* signal EOF to pager */
22 finish_command_in_signal(&pager_process);
24 finish_command(&pager_process);
27 static void wait_for_pager_atexit(void)
32 static void wait_for_pager_signal(int signo)
39 static int core_pager_config(const char *var, const char *value, void *data)
41 if (!strcmp(var, "core.pager"))
42 return git_config_string(&pager_program, var, value);
46 const char *git_pager(int stdout_is_tty)
53 pager = getenv("GIT_PAGER");
56 git_config(core_pager_config, NULL);
57 pager = pager_program;
60 pager = getenv("PAGER");
62 pager = DEFAULT_PAGER;
63 if (!*pager || !strcmp(pager, "cat"))
69 void prepare_pager_args(struct child_process *pager_process, const char *pager)
71 argv_array_push(&pager_process->args, pager);
72 pager_process->use_shell = 1;
74 argv_array_push(&pager_process->env_array, "LESS=FRX");
76 argv_array_push(&pager_process->env_array, "LV=-c");
79 void setup_pager(void)
81 const char *pager = git_pager(isatty(1));
87 * force computing the width of the terminal before we redirect
88 * the standard output to the pager.
90 (void) term_columns();
92 setenv("GIT_PAGER_IN_USE", "true", 1);
95 prepare_pager_args(&pager_process, pager);
96 pager_process.in = -1;
97 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
98 if (start_command(&pager_process))
101 /* original process continues, but writes to the pipe */
102 dup2(pager_process.in, 1);
104 dup2(pager_process.in, 2);
105 close(pager_process.in);
107 /* this makes sure that the parent terminates after the pager */
108 sigchain_push_common(wait_for_pager_signal);
109 atexit(wait_for_pager_atexit);
112 int pager_in_use(void)
115 env = getenv("GIT_PAGER_IN_USE");
116 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
120 * Return cached value (if set) or $COLUMNS environment variable (if
121 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
122 * and default to 80 if all else fails.
124 int term_columns(void)
126 static int term_columns_at_startup;
131 if (term_columns_at_startup)
132 return term_columns_at_startup;
134 term_columns_at_startup = 80;
136 col_string = getenv("COLUMNS");
137 if (col_string && (n_cols = atoi(col_string)) > 0)
138 term_columns_at_startup = n_cols;
142 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
143 term_columns_at_startup = ws.ws_col;
147 return term_columns_at_startup;
151 * How many columns do we need to show this number in decimal?
153 int decimal_width(uintmax_t number)
157 for (width = 1; number >= 10; width++)
162 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
163 int check_pager_config(const char *cmd)
166 struct strbuf key = STRBUF_INIT;
167 const char *value = NULL;
168 strbuf_addf(&key, "pager.%s", cmd);
169 if (git_config_key_is_valid(key.buf) &&
170 !git_config_get_value(key.buf, &value)) {
171 int b = git_config_maybe_bool(key.buf, value);
176 pager_program = xstrdup(value);
179 strbuf_release(&key);