2 #include "run-command.h"
6 #define DEFAULT_PAGER "less"
9 static struct child_process pager_process
= CHILD_PROCESS_INIT
;
11 static void wait_for_pager(int in_signal
)
17 /* signal EOF to pager */
21 finish_command_in_signal(&pager_process
);
23 finish_command(&pager_process
);
26 static void wait_for_pager_atexit(void)
31 static void wait_for_pager_signal(int signo
)
38 const char *git_pager(int stdout_is_tty
)
45 pager
= getenv("GIT_PAGER");
48 git_config(git_default_config
, NULL
);
49 pager
= pager_program
;
52 pager
= getenv("PAGER");
54 pager
= DEFAULT_PAGER
;
55 if (!*pager
|| !strcmp(pager
, "cat"))
61 void prepare_pager_args(struct child_process
*pager_process
, const char *pager
)
63 argv_array_push(&pager_process
->args
, pager
);
64 pager_process
->use_shell
= 1;
66 argv_array_push(&pager_process
->env_array
, "LESS=FRX");
68 argv_array_push(&pager_process
->env_array
, "LV=-c");
71 void setup_pager(void)
73 const char *pager
= git_pager(isatty(1));
79 * force computing the width of the terminal before we redirect
80 * the standard output to the pager.
82 (void) term_columns();
84 setenv("GIT_PAGER_IN_USE", "true", 1);
87 prepare_pager_args(&pager_process
, pager
);
88 pager_process
.in
= -1;
89 argv_array_push(&pager_process
.env_array
, "GIT_PAGER_IN_USE");
90 if (start_command(&pager_process
))
93 /* original process continues, but writes to the pipe */
94 dup2(pager_process
.in
, 1);
96 dup2(pager_process
.in
, 2);
97 close(pager_process
.in
);
99 /* this makes sure that the parent terminates after the pager */
100 sigchain_push_common(wait_for_pager_signal
);
101 atexit(wait_for_pager_atexit
);
104 int pager_in_use(void)
107 env
= getenv("GIT_PAGER_IN_USE");
108 return env
? git_config_bool("GIT_PAGER_IN_USE", env
) : 0;
112 * Return cached value (if set) or $COLUMNS environment variable (if
113 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
114 * and default to 80 if all else fails.
116 int term_columns(void)
118 static int term_columns_at_startup
;
123 if (term_columns_at_startup
)
124 return term_columns_at_startup
;
126 term_columns_at_startup
= 80;
128 col_string
= getenv("COLUMNS");
129 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
130 term_columns_at_startup
= n_cols
;
134 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
)
135 term_columns_at_startup
= ws
.ws_col
;
139 return term_columns_at_startup
;
143 * How many columns do we need to show this number in decimal?
145 int decimal_width(uintmax_t number
)
149 for (width
= 1; number
>= 10; width
++)
154 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
155 int check_pager_config(const char *cmd
)
158 struct strbuf key
= STRBUF_INIT
;
159 const char *value
= NULL
;
160 strbuf_addf(&key
, "pager.%s", cmd
);
161 if (git_config_key_is_valid(key
.buf
) &&
162 !git_config_get_value(key
.buf
, &value
)) {
163 int b
= git_config_maybe_bool(key
.buf
, value
);
168 pager_program
= xstrdup(value
);
171 strbuf_release(&key
);