1 #include "git-compat-util.h"
5 #include "run-command.h"
9 int pager_use_color
= 1;
12 #define DEFAULT_PAGER "less"
15 static struct child_process pager_process
;
16 static char *pager_program
;
18 /* Is the value coming back from term_columns() just a guess? */
19 static int term_columns_guessed
;
22 static void close_pager_fds(void)
24 /* signal EOF to pager */
29 static void wait_for_pager_atexit(void)
34 finish_command(&pager_process
);
37 static void wait_for_pager_signal(int signo
)
40 finish_command_in_signal(&pager_process
);
45 static int core_pager_config(const char *var
, const char *value
,
46 const struct config_context
*ctx UNUSED
,
49 if (!strcmp(var
, "core.pager"))
50 return git_config_string(&pager_program
, var
, value
);
54 const char *git_pager(int stdout_is_tty
)
61 pager
= getenv("GIT_PAGER");
64 read_early_config(core_pager_config
, NULL
);
65 pager
= pager_program
;
68 pager
= getenv("PAGER");
70 pager
= DEFAULT_PAGER
;
71 if (!*pager
|| !strcmp(pager
, "cat"))
77 static void setup_pager_env(struct strvec
*env
)
81 char *pager_env
= xstrdup(PAGER_ENV
);
82 int n
= split_cmdline(pager_env
, &argv
);
85 die("malformed build-time PAGER_ENV: %s",
86 split_cmdline_strerror(n
));
88 for (i
= 0; i
< n
; i
++) {
89 char *cp
= strchr(argv
[i
], '=');
92 die("malformed build-time PAGER_ENV");
95 if (!getenv(argv
[i
])) {
97 strvec_push(env
, argv
[i
]);
104 void prepare_pager_args(struct child_process
*pager_process
, const char *pager
)
106 strvec_push(&pager_process
->args
, pager
);
107 pager_process
->use_shell
= 1;
108 setup_pager_env(&pager_process
->env
);
109 pager_process
->trace2_child_class
= "pager";
112 void setup_pager(void)
114 const char *pager
= git_pager(isatty(1));
120 * After we redirect standard output, we won't be able to use an ioctl
121 * to get the terminal size. Let's grab it now, and then set $COLUMNS
122 * to communicate it to any sub-processes.
126 xsnprintf(buf
, sizeof(buf
), "%d", term_columns());
127 if (!term_columns_guessed
)
128 setenv("COLUMNS", buf
, 0);
131 setenv("GIT_PAGER_IN_USE", "true", 1);
133 child_process_init(&pager_process
);
135 /* spawn the pager */
136 prepare_pager_args(&pager_process
, pager
);
137 pager_process
.in
= -1;
138 strvec_push(&pager_process
.env
, "GIT_PAGER_IN_USE");
139 if (start_command(&pager_process
))
140 die("unable to execute pager '%s'", pager
);
142 /* original process continues, but writes to the pipe */
143 dup2(pager_process
.in
, 1);
145 dup2(pager_process
.in
, 2);
146 close(pager_process
.in
);
148 /* this makes sure that the parent terminates after the pager */
149 sigchain_push_common(wait_for_pager_signal
);
150 atexit(wait_for_pager_atexit
);
153 int pager_in_use(void)
155 return git_env_bool("GIT_PAGER_IN_USE", 0);
159 * Return cached value (if set) or $COLUMNS environment variable (if
160 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
161 * and default to 80 if all else fails.
163 int term_columns(void)
165 static int term_columns_at_startup
;
170 if (term_columns_at_startup
)
171 return term_columns_at_startup
;
173 term_columns_at_startup
= 80;
174 term_columns_guessed
= 1;
176 col_string
= getenv("COLUMNS");
177 if (col_string
&& (n_cols
= atoi(col_string
)) > 0) {
178 term_columns_at_startup
= n_cols
;
179 term_columns_guessed
= 0;
184 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
) {
185 term_columns_at_startup
= ws
.ws_col
;
186 term_columns_guessed
= 0;
191 return term_columns_at_startup
;
195 * Clear the entire line, leave cursor in first column.
197 void term_clear_line(void)
199 if (is_terminal_dumb())
201 * Fall back to print a terminal width worth of space
202 * characters (hoping that the terminal is still as wide
203 * as it was upon the first call to term_columns()).
205 fprintf(stderr
, "\r%*s\r", term_columns(), "");
208 * On non-dumb terminals use an escape sequence to clear
209 * the whole line, no matter how wide the terminal.
211 fputs("\r\033[K", stderr
);
215 * How many columns do we need to show this number in decimal?
217 int decimal_width(uintmax_t number
)
221 for (width
= 1; number
>= 10; width
++)
226 struct pager_command_config_data
{
232 static int pager_command_config(const char *var
, const char *value
,
233 const struct config_context
*ctx UNUSED
,
236 struct pager_command_config_data
*data
= vdata
;
239 if (skip_prefix(var
, "pager.", &cmd
) && !strcmp(cmd
, data
->cmd
)) {
240 int b
= git_parse_maybe_bool(value
);
245 data
->value
= xstrdup(value
);
252 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
253 int check_pager_config(const char *cmd
)
255 struct pager_command_config_data data
;
261 read_early_config(pager_command_config
, &data
);
264 pager_program
= data
.value
;