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 const 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
,
48 if (!strcmp(var
, "core.pager"))
49 return git_config_string(&pager_program
, var
, value
);
53 const char *git_pager(int stdout_is_tty
)
60 pager
= getenv("GIT_PAGER");
63 read_early_config(core_pager_config
, NULL
);
64 pager
= pager_program
;
67 pager
= getenv("PAGER");
69 pager
= DEFAULT_PAGER
;
70 if (!*pager
|| !strcmp(pager
, "cat"))
76 static void setup_pager_env(struct strvec
*env
)
80 char *pager_env
= xstrdup(PAGER_ENV
);
81 int n
= split_cmdline(pager_env
, &argv
);
84 die("malformed build-time PAGER_ENV: %s",
85 split_cmdline_strerror(n
));
87 for (i
= 0; i
< n
; i
++) {
88 char *cp
= strchr(argv
[i
], '=');
91 die("malformed build-time PAGER_ENV");
94 if (!getenv(argv
[i
])) {
96 strvec_push(env
, argv
[i
]);
103 void prepare_pager_args(struct child_process
*pager_process
, const char *pager
)
105 strvec_push(&pager_process
->args
, pager
);
106 pager_process
->use_shell
= 1;
107 setup_pager_env(&pager_process
->env
);
108 pager_process
->trace2_child_class
= "pager";
111 void setup_pager(void)
113 const char *pager
= git_pager(isatty(1));
119 * After we redirect standard output, we won't be able to use an ioctl
120 * to get the terminal size. Let's grab it now, and then set $COLUMNS
121 * to communicate it to any sub-processes.
125 xsnprintf(buf
, sizeof(buf
), "%d", term_columns());
126 if (!term_columns_guessed
)
127 setenv("COLUMNS", buf
, 0);
130 setenv("GIT_PAGER_IN_USE", "true", 1);
132 child_process_init(&pager_process
);
134 /* spawn the pager */
135 prepare_pager_args(&pager_process
, pager
);
136 pager_process
.in
= -1;
137 strvec_push(&pager_process
.env
, "GIT_PAGER_IN_USE");
138 if (start_command(&pager_process
))
141 /* original process continues, but writes to the pipe */
142 dup2(pager_process
.in
, 1);
144 dup2(pager_process
.in
, 2);
145 close(pager_process
.in
);
147 /* this makes sure that the parent terminates after the pager */
148 sigchain_push_common(wait_for_pager_signal
);
149 atexit(wait_for_pager_atexit
);
152 int pager_in_use(void)
154 return git_env_bool("GIT_PAGER_IN_USE", 0);
158 * Return cached value (if set) or $COLUMNS environment variable (if
159 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
160 * and default to 80 if all else fails.
162 int term_columns(void)
164 static int term_columns_at_startup
;
169 if (term_columns_at_startup
)
170 return term_columns_at_startup
;
172 term_columns_at_startup
= 80;
173 term_columns_guessed
= 1;
175 col_string
= getenv("COLUMNS");
176 if (col_string
&& (n_cols
= atoi(col_string
)) > 0) {
177 term_columns_at_startup
= n_cols
;
178 term_columns_guessed
= 0;
183 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
) {
184 term_columns_at_startup
= ws
.ws_col
;
185 term_columns_guessed
= 0;
190 return term_columns_at_startup
;
194 * Clear the entire line, leave cursor in first column.
196 void term_clear_line(void)
198 if (is_terminal_dumb())
200 * Fall back to print a terminal width worth of space
201 * characters (hoping that the terminal is still as wide
202 * as it was upon the first call to term_columns()).
204 fprintf(stderr
, "\r%*s\r", term_columns(), "");
207 * On non-dumb terminals use an escape sequence to clear
208 * the whole line, no matter how wide the terminal.
210 fputs("\r\033[K", stderr
);
214 * How many columns do we need to show this number in decimal?
216 int decimal_width(uintmax_t number
)
220 for (width
= 1; number
>= 10; width
++)
225 struct pager_command_config_data
{
231 static int pager_command_config(const char *var
, const char *value
, void *vdata
)
233 struct pager_command_config_data
*data
= vdata
;
236 if (skip_prefix(var
, "pager.", &cmd
) && !strcmp(cmd
, data
->cmd
)) {
237 int b
= git_parse_maybe_bool(value
);
242 data
->value
= xstrdup(value
);
249 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
250 int check_pager_config(const char *cmd
)
252 struct pager_command_config_data data
;
258 read_early_config(pager_command_config
, &data
);
261 pager_program
= data
.value
;