3 #include "run-command.h"
8 #define DEFAULT_PAGER "less"
11 static struct child_process pager_process
;
12 static const char *pager_program
;
14 /* Is the value coming back from term_columns() just a guess? */
15 static int term_columns_guessed
;
18 static void close_pager_fds(void)
20 /* signal EOF to pager */
25 static void wait_for_pager_atexit(void)
30 finish_command(&pager_process
);
33 static void wait_for_pager_signal(int signo
)
36 finish_command_in_signal(&pager_process
);
41 static int core_pager_config(const char *var
, const char *value
,
44 if (!strcmp(var
, "core.pager"))
45 return git_config_string(&pager_program
, var
, value
);
49 const char *git_pager(int stdout_is_tty
)
56 pager
= getenv("GIT_PAGER");
59 read_early_config(core_pager_config
, NULL
);
60 pager
= pager_program
;
63 pager
= getenv("PAGER");
65 pager
= DEFAULT_PAGER
;
66 if (!*pager
|| !strcmp(pager
, "cat"))
72 static void setup_pager_env(struct strvec
*env
)
76 char *pager_env
= xstrdup(PAGER_ENV
);
77 int n
= split_cmdline(pager_env
, &argv
);
80 die("malformed build-time PAGER_ENV: %s",
81 split_cmdline_strerror(n
));
83 for (i
= 0; i
< n
; i
++) {
84 char *cp
= strchr(argv
[i
], '=');
87 die("malformed build-time PAGER_ENV");
90 if (!getenv(argv
[i
])) {
92 strvec_push(env
, argv
[i
]);
99 void prepare_pager_args(struct child_process
*pager_process
, const char *pager
)
101 strvec_push(&pager_process
->args
, pager
);
102 pager_process
->use_shell
= 1;
103 setup_pager_env(&pager_process
->env
);
104 pager_process
->trace2_child_class
= "pager";
107 void setup_pager(void)
109 const char *pager
= git_pager(isatty(1));
115 * After we redirect standard output, we won't be able to use an ioctl
116 * to get the terminal size. Let's grab it now, and then set $COLUMNS
117 * to communicate it to any sub-processes.
121 xsnprintf(buf
, sizeof(buf
), "%d", term_columns());
122 if (!term_columns_guessed
)
123 setenv("COLUMNS", buf
, 0);
126 setenv("GIT_PAGER_IN_USE", "true", 1);
128 child_process_init(&pager_process
);
130 /* spawn the pager */
131 prepare_pager_args(&pager_process
, pager
);
132 pager_process
.in
= -1;
133 strvec_push(&pager_process
.env
, "GIT_PAGER_IN_USE");
134 if (start_command(&pager_process
))
137 /* original process continues, but writes to the pipe */
138 dup2(pager_process
.in
, 1);
140 dup2(pager_process
.in
, 2);
141 close(pager_process
.in
);
143 /* this makes sure that the parent terminates after the pager */
144 sigchain_push_common(wait_for_pager_signal
);
145 atexit(wait_for_pager_atexit
);
148 int pager_in_use(void)
150 return git_env_bool("GIT_PAGER_IN_USE", 0);
154 * Return cached value (if set) or $COLUMNS environment variable (if
155 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
156 * and default to 80 if all else fails.
158 int term_columns(void)
160 static int term_columns_at_startup
;
165 if (term_columns_at_startup
)
166 return term_columns_at_startup
;
168 term_columns_at_startup
= 80;
169 term_columns_guessed
= 1;
171 col_string
= getenv("COLUMNS");
172 if (col_string
&& (n_cols
= atoi(col_string
)) > 0) {
173 term_columns_at_startup
= n_cols
;
174 term_columns_guessed
= 0;
179 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
) {
180 term_columns_at_startup
= ws
.ws_col
;
181 term_columns_guessed
= 0;
186 return term_columns_at_startup
;
190 * Clear the entire line, leave cursor in first column.
192 void term_clear_line(void)
194 if (is_terminal_dumb())
196 * Fall back to print a terminal width worth of space
197 * characters (hoping that the terminal is still as wide
198 * as it was upon the first call to term_columns()).
200 fprintf(stderr
, "\r%*s\r", term_columns(), "");
203 * On non-dumb terminals use an escape sequence to clear
204 * the whole line, no matter how wide the terminal.
206 fputs("\r\033[K", stderr
);
210 * How many columns do we need to show this number in decimal?
212 int decimal_width(uintmax_t number
)
216 for (width
= 1; number
>= 10; width
++)
221 struct pager_command_config_data
{
227 static int pager_command_config(const char *var
, const char *value
, void *vdata
)
229 struct pager_command_config_data
*data
= vdata
;
232 if (skip_prefix(var
, "pager.", &cmd
) && !strcmp(cmd
, data
->cmd
)) {
233 int b
= git_parse_maybe_bool(value
);
238 data
->value
= xstrdup(value
);
245 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
246 int check_pager_config(const char *cmd
)
248 struct pager_command_config_data data
;
254 read_early_config(pager_command_config
, &data
);
257 pager_program
= data
.value
;