am -i: fix "v"iew
[git.git] / pager.c
blobcb28207c48b2b9adafc5a874ea9f5afc9a55842c
1 #include "cache.h"
2 #include "run-command.h"
3 #include "sigchain.h"
5 #ifndef DEFAULT_PAGER
6 #define DEFAULT_PAGER "less"
7 #endif
9 /*
10 * This is split up from the rest of git so that we can do
11 * something different on Windows.
14 static struct child_process pager_process = CHILD_PROCESS_INIT;
16 static void wait_for_pager(void)
18 fflush(stdout);
19 fflush(stderr);
20 /* signal EOF to pager */
21 close(1);
22 close(2);
23 finish_command(&pager_process);
26 static void wait_for_pager_signal(int signo)
28 wait_for_pager();
29 sigchain_pop(signo);
30 raise(signo);
33 const char *git_pager(int stdout_is_tty)
35 const char *pager;
37 if (!stdout_is_tty)
38 return NULL;
40 pager = getenv("GIT_PAGER");
41 if (!pager) {
42 if (!pager_program)
43 git_config(git_default_config, NULL);
44 pager = pager_program;
46 if (!pager)
47 pager = getenv("PAGER");
48 if (!pager)
49 pager = DEFAULT_PAGER;
50 if (!*pager || !strcmp(pager, "cat"))
51 pager = NULL;
53 return pager;
56 void prepare_pager_args(struct child_process *pager_process, const char *pager)
58 argv_array_push(&pager_process->args, pager);
59 pager_process->use_shell = 1;
60 if (!getenv("LESS"))
61 argv_array_push(&pager_process->env_array, "LESS=FRX");
62 if (!getenv("LV"))
63 argv_array_push(&pager_process->env_array, "LV=-c");
66 void setup_pager(void)
68 const char *pager = git_pager(isatty(1));
70 if (!pager)
71 return;
74 * force computing the width of the terminal before we redirect
75 * the standard output to the pager.
77 (void) term_columns();
79 setenv("GIT_PAGER_IN_USE", "true", 1);
81 /* spawn the pager */
82 prepare_pager_args(&pager_process, pager);
83 pager_process.in = -1;
84 argv_array_push(&pager_process.env_array, "GIT_PAGER_IN_USE");
85 if (start_command(&pager_process))
86 return;
88 /* original process continues, but writes to the pipe */
89 dup2(pager_process.in, 1);
90 if (isatty(2))
91 dup2(pager_process.in, 2);
92 close(pager_process.in);
94 /* this makes sure that the parent terminates after the pager */
95 sigchain_push_common(wait_for_pager_signal);
96 atexit(wait_for_pager);
99 int pager_in_use(void)
101 const char *env;
102 env = getenv("GIT_PAGER_IN_USE");
103 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
107 * Return cached value (if set) or $COLUMNS environment variable (if
108 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
109 * and default to 80 if all else fails.
111 int term_columns(void)
113 static int term_columns_at_startup;
115 char *col_string;
116 int n_cols;
118 if (term_columns_at_startup)
119 return term_columns_at_startup;
121 term_columns_at_startup = 80;
123 col_string = getenv("COLUMNS");
124 if (col_string && (n_cols = atoi(col_string)) > 0)
125 term_columns_at_startup = n_cols;
126 #ifdef TIOCGWINSZ
127 else {
128 struct winsize ws;
129 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
130 term_columns_at_startup = ws.ws_col;
132 #endif
134 return term_columns_at_startup;
138 * How many columns do we need to show this number in decimal?
140 int decimal_width(uintmax_t number)
142 int width;
144 for (width = 1; number >= 10; width++)
145 number /= 10;
146 return width;
149 /* returns 0 for "no pager", 1 for "use pager", and -1 for "not specified" */
150 int check_pager_config(const char *cmd)
152 int want = -1;
153 struct strbuf key = STRBUF_INIT;
154 const char *value = NULL;
155 strbuf_addf(&key, "pager.%s", cmd);
156 if (!git_config_get_value(key.buf, &value)) {
157 int b = git_config_maybe_bool(key.buf, value);
158 if (b >= 0)
159 want = b;
160 else {
161 want = 1;
162 pager_program = xstrdup(value);
165 strbuf_release(&key);
166 return want;