4 #include "run-command.h"
9 #define DEFAULT_PAGER "less"
12 static struct child_process pager_process
;
13 static const char *pager_program
;
15 /* Is the value coming back from term_columns() just a guess? */
16 static int term_columns_guessed
;
19 static void close_pager_fds(void)
21 /* signal EOF to pager */
26 static void wait_for_pager_atexit(void)
31 finish_command(&pager_process
);
34 static void wait_for_pager_signal(int signo
)
37 finish_command_in_signal(&pager_process
);
42 static int core_pager_config(const char *var
, const char *value
,
45 if (!strcmp(var
, "core.pager"))
46 return git_config_string(&pager_program
, var
, value
);
50 const char *git_pager(int stdout_is_tty
)
57 pager
= getenv("GIT_PAGER");
60 read_early_config(core_pager_config
, NULL
);
61 pager
= pager_program
;
64 pager
= getenv("PAGER");
66 pager
= DEFAULT_PAGER
;
67 if (!*pager
|| !strcmp(pager
, "cat"))
73 static void setup_pager_env(struct strvec
*env
)
77 char *pager_env
= xstrdup(PAGER_ENV
);
78 int n
= split_cmdline(pager_env
, &argv
);
81 die("malformed build-time PAGER_ENV: %s",
82 split_cmdline_strerror(n
));
84 for (i
= 0; i
< n
; i
++) {
85 char *cp
= strchr(argv
[i
], '=');
88 die("malformed build-time PAGER_ENV");
91 if (!getenv(argv
[i
])) {
93 strvec_push(env
, argv
[i
]);
100 void prepare_pager_args(struct child_process
*pager_process
, const char *pager
)
102 strvec_push(&pager_process
->args
, pager
);
103 pager_process
->use_shell
= 1;
104 setup_pager_env(&pager_process
->env
);
105 pager_process
->trace2_child_class
= "pager";
108 void setup_pager(void)
110 const char *pager
= git_pager(isatty(1));
116 * After we redirect standard output, we won't be able to use an ioctl
117 * to get the terminal size. Let's grab it now, and then set $COLUMNS
118 * to communicate it to any sub-processes.
122 xsnprintf(buf
, sizeof(buf
), "%d", term_columns());
123 if (!term_columns_guessed
)
124 setenv("COLUMNS", buf
, 0);
127 setenv("GIT_PAGER_IN_USE", "true", 1);
129 child_process_init(&pager_process
);
131 /* spawn the pager */
132 prepare_pager_args(&pager_process
, pager
);
133 pager_process
.in
= -1;
134 strvec_push(&pager_process
.env
, "GIT_PAGER_IN_USE");
135 if (start_command(&pager_process
))
138 /* original process continues, but writes to the pipe */
139 dup2(pager_process
.in
, 1);
141 dup2(pager_process
.in
, 2);
142 close(pager_process
.in
);
144 /* this makes sure that the parent terminates after the pager */
145 sigchain_push_common(wait_for_pager_signal
);
146 atexit(wait_for_pager_atexit
);
149 int pager_in_use(void)
151 return git_env_bool("GIT_PAGER_IN_USE", 0);
155 * Return cached value (if set) or $COLUMNS environment variable (if
156 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
157 * and default to 80 if all else fails.
159 int term_columns(void)
161 static int term_columns_at_startup
;
166 if (term_columns_at_startup
)
167 return term_columns_at_startup
;
169 term_columns_at_startup
= 80;
170 term_columns_guessed
= 1;
172 col_string
= getenv("COLUMNS");
173 if (col_string
&& (n_cols
= atoi(col_string
)) > 0) {
174 term_columns_at_startup
= n_cols
;
175 term_columns_guessed
= 0;
180 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
) {
181 term_columns_at_startup
= ws
.ws_col
;
182 term_columns_guessed
= 0;
187 return term_columns_at_startup
;
191 * Clear the entire line, leave cursor in first column.
193 void term_clear_line(void)
195 if (is_terminal_dumb())
197 * Fall back to print a terminal width worth of space
198 * characters (hoping that the terminal is still as wide
199 * as it was upon the first call to term_columns()).
201 fprintf(stderr
, "\r%*s\r", term_columns(), "");
204 * On non-dumb terminals use an escape sequence to clear
205 * the whole line, no matter how wide the terminal.
207 fputs("\r\033[K", stderr
);
211 * How many columns do we need to show this number in decimal?
213 int decimal_width(uintmax_t number
)
217 for (width
= 1; number
>= 10; width
++)
222 struct pager_command_config_data
{
228 static int pager_command_config(const char *var
, const char *value
, void *vdata
)
230 struct pager_command_config_data
*data
= vdata
;
233 if (skip_prefix(var
, "pager.", &cmd
) && !strcmp(cmd
, data
->cmd
)) {
234 int b
= git_parse_maybe_bool(value
);
239 data
->value
= xstrdup(value
);
246 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
247 int check_pager_config(const char *cmd
)
249 struct pager_command_config_data data
;
255 read_early_config(pager_command_config
, &data
);
258 pager_program
= data
.value
;