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
, void *data
)
43 if (!strcmp(var
, "core.pager"))
44 return git_config_string(&pager_program
, var
, value
);
48 const char *git_pager(int stdout_is_tty
)
55 pager
= getenv("GIT_PAGER");
58 read_early_config(core_pager_config
, NULL
);
59 pager
= pager_program
;
62 pager
= getenv("PAGER");
64 pager
= DEFAULT_PAGER
;
65 if (!*pager
|| !strcmp(pager
, "cat"))
71 static void setup_pager_env(struct strvec
*env
)
75 char *pager_env
= xstrdup(PAGER_ENV
);
76 int n
= split_cmdline(pager_env
, &argv
);
79 die("malformed build-time PAGER_ENV: %s",
80 split_cmdline_strerror(n
));
82 for (i
= 0; i
< n
; i
++) {
83 char *cp
= strchr(argv
[i
], '=');
86 die("malformed build-time PAGER_ENV");
89 if (!getenv(argv
[i
])) {
91 strvec_push(env
, argv
[i
]);
98 void prepare_pager_args(struct child_process
*pager_process
, const char *pager
)
100 strvec_push(&pager_process
->args
, pager
);
101 pager_process
->use_shell
= 1;
102 setup_pager_env(&pager_process
->env
);
103 pager_process
->trace2_child_class
= "pager";
106 void setup_pager(void)
108 const char *pager
= git_pager(isatty(1));
114 * After we redirect standard output, we won't be able to use an ioctl
115 * to get the terminal size. Let's grab it now, and then set $COLUMNS
116 * to communicate it to any sub-processes.
120 xsnprintf(buf
, sizeof(buf
), "%d", term_columns());
121 if (!term_columns_guessed
)
122 setenv("COLUMNS", buf
, 0);
125 setenv("GIT_PAGER_IN_USE", "true", 1);
127 child_process_init(&pager_process
);
129 /* spawn the pager */
130 prepare_pager_args(&pager_process
, pager
);
131 pager_process
.in
= -1;
132 strvec_push(&pager_process
.env
, "GIT_PAGER_IN_USE");
133 if (start_command(&pager_process
))
136 /* original process continues, but writes to the pipe */
137 dup2(pager_process
.in
, 1);
139 dup2(pager_process
.in
, 2);
140 close(pager_process
.in
);
142 /* this makes sure that the parent terminates after the pager */
143 sigchain_push_common(wait_for_pager_signal
);
144 atexit(wait_for_pager_atexit
);
147 int pager_in_use(void)
149 return git_env_bool("GIT_PAGER_IN_USE", 0);
153 * Return cached value (if set) or $COLUMNS environment variable (if
154 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
155 * and default to 80 if all else fails.
157 int term_columns(void)
159 static int term_columns_at_startup
;
164 if (term_columns_at_startup
)
165 return term_columns_at_startup
;
167 term_columns_at_startup
= 80;
168 term_columns_guessed
= 1;
170 col_string
= getenv("COLUMNS");
171 if (col_string
&& (n_cols
= atoi(col_string
)) > 0) {
172 term_columns_at_startup
= n_cols
;
173 term_columns_guessed
= 0;
178 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
) {
179 term_columns_at_startup
= ws
.ws_col
;
180 term_columns_guessed
= 0;
185 return term_columns_at_startup
;
189 * Clear the entire line, leave cursor in first column.
191 void term_clear_line(void)
193 if (is_terminal_dumb())
195 * Fall back to print a terminal width worth of space
196 * characters (hoping that the terminal is still as wide
197 * as it was upon the first call to term_columns()).
199 fprintf(stderr
, "\r%*s\r", term_columns(), "");
202 * On non-dumb terminals use an escape sequence to clear
203 * the whole line, no matter how wide the terminal.
205 fputs("\r\033[K", stderr
);
209 * How many columns do we need to show this number in decimal?
211 int decimal_width(uintmax_t number
)
215 for (width
= 1; number
>= 10; width
++)
220 struct pager_command_config_data
{
226 static int pager_command_config(const char *var
, const char *value
, void *vdata
)
228 struct pager_command_config_data
*data
= vdata
;
231 if (skip_prefix(var
, "pager.", &cmd
) && !strcmp(cmd
, data
->cmd
)) {
232 int b
= git_parse_maybe_bool(value
);
237 data
->value
= xstrdup(value
);
244 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
245 int check_pager_config(const char *cmd
)
247 struct pager_command_config_data data
;
253 read_early_config(pager_command_config
, &data
);
256 pager_program
= data
.value
;