2 #include "run-command.h"
6 #define DEFAULT_PAGER "less"
16 * This is split up from the rest of git so that we can do
17 * something different on Windows.
20 static const char *pager_argv
[] = { NULL
, NULL
};
21 static struct child_process pager_process
;
23 static void wait_for_pager(void)
27 /* signal EOF to pager */
30 finish_command(&pager_process
);
33 static void wait_for_pager_signal(int signo
)
40 const char *git_pager(int stdout_is_tty
)
47 pager
= getenv("GIT_PAGER");
50 git_config(git_default_config
, NULL
);
51 pager
= pager_program
;
54 pager
= getenv("PAGER");
56 pager
= DEFAULT_PAGER
;
57 if (!*pager
|| !strcmp(pager
, "cat"))
63 void setup_pager(void)
65 const char *pager
= git_pager(isatty(1));
71 * force computing the width of the terminal before we redirect
72 * the standard output to the pager.
74 (void) term_columns();
76 setenv("GIT_PAGER_IN_USE", "true", 1);
79 pager_argv
[0] = pager
;
80 pager_process
.use_shell
= 1;
81 pager_process
.argv
= pager_argv
;
82 pager_process
.in
= -1;
83 if (!getenv("LESS") || !getenv("LV")) {
84 static const char *env
[3];
88 env
[i
++] = "LESS=FRSX";
92 pager_process
.env
= env
;
94 if (start_command(&pager_process
))
97 /* original process continues, but writes to the pipe */
98 dup2(pager_process
.in
, 1);
100 dup2(pager_process
.in
, 2);
101 close(pager_process
.in
);
103 /* this makes sure that the parent terminates after the pager */
104 sigchain_push_common(wait_for_pager_signal
);
105 atexit(wait_for_pager
);
108 int pager_in_use(void)
111 env
= getenv("GIT_PAGER_IN_USE");
112 return env
? git_config_bool("GIT_PAGER_IN_USE", env
) : 0;
116 * Return cached value (if set) or $COLUMNS environment variable (if
117 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
118 * and default to 80 if all else fails.
120 int term_columns(void)
122 static int term_columns_at_startup
;
127 if (term_columns_at_startup
)
128 return term_columns_at_startup
;
130 term_columns_at_startup
= 80;
132 col_string
= getenv("COLUMNS");
133 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
134 term_columns_at_startup
= n_cols
;
138 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
)
139 term_columns_at_startup
= ws
.ws_col
;
143 return term_columns_at_startup
;
147 * How many columns do we need to show this number in decimal?
149 int decimal_width(int number
)
153 for (width
= 1, i
= 10; i
<= number
; width
++)
158 static int pager_command_config(const char *var
, const char *value
, void *data
)
160 struct pager_config
*c
= data
;
161 if (starts_with(var
, "pager.") && !strcmp(var
+ 6, c
->cmd
)) {
162 int b
= git_config_maybe_bool(var
, value
);
167 c
->value
= xstrdup(value
);
173 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
174 int check_pager_config(const char *cmd
)
176 struct pager_config c
;
180 git_config(pager_command_config
, &c
);
182 pager_program
= c
.value
;