2 #include "run-command.h"
6 #define DEFAULT_PAGER "less"
10 * This is split up from the rest of git so that we can do
11 * something different on Windows.
15 static void pager_preexec(void)
18 * Work around bug in "less" by not starting it until we
25 select(1, &in
, NULL
, &in
, NULL
);
29 static const char *pager_argv
[] = { NULL
, NULL
};
30 static struct child_process pager_process
;
32 static void wait_for_pager(void)
36 /* signal EOF to pager */
39 finish_command(&pager_process
);
42 static void wait_for_pager_signal(int signo
)
49 const char *git_pager(int stdout_is_tty
)
56 pager
= getenv("GIT_PAGER");
59 git_config(git_default_config
, NULL
);
60 pager
= pager_program
;
63 pager
= getenv("PAGER");
65 pager
= DEFAULT_PAGER
;
66 else if (!*pager
|| !strcmp(pager
, "cat"))
72 void setup_pager(void)
74 const char *pager
= git_pager(isatty(1));
76 if (!pager
|| pager_in_use())
80 * force computing the width of the terminal before we redirect
81 * the standard output to the pager.
83 (void) term_columns();
85 setenv("GIT_PAGER_IN_USE", "true", 1);
88 pager_argv
[0] = pager
;
89 pager_process
.use_shell
= 1;
90 pager_process
.argv
= pager_argv
;
91 pager_process
.in
= -1;
92 if (!getenv("LESS")) {
93 static const char *env
[] = { "LESS=FRSX", NULL
};
94 pager_process
.env
= env
;
97 pager_process
.preexec_cb
= pager_preexec
;
99 if (start_command(&pager_process
))
102 /* original process continues, but writes to the pipe */
103 dup2(pager_process
.in
, 1);
105 dup2(pager_process
.in
, 2);
106 close(pager_process
.in
);
108 /* this makes sure that the parent terminates after the pager */
109 sigchain_push_common(wait_for_pager_signal
);
110 atexit(wait_for_pager
);
113 int pager_in_use(void)
116 env
= getenv("GIT_PAGER_IN_USE");
117 return env
? git_config_bool("GIT_PAGER_IN_USE", env
) : 0;
121 * Return cached value (if set) or $COLUMNS environment variable (if
122 * set and positive) or ioctl(1, TIOCGWINSZ).ws_col (if positive),
123 * and default to 80 if all else fails.
125 int term_columns(void)
127 static int term_columns_at_startup
;
132 if (term_columns_at_startup
)
133 return term_columns_at_startup
;
135 term_columns_at_startup
= 80;
137 col_string
= getenv("COLUMNS");
138 if (col_string
&& (n_cols
= atoi(col_string
)) > 0)
139 term_columns_at_startup
= n_cols
;
143 if (!ioctl(1, TIOCGWINSZ
, &ws
) && ws
.ws_col
)
144 term_columns_at_startup
= ws
.ws_col
;
148 return term_columns_at_startup
;
152 * How many columns do we need to show this number in decimal?
154 int decimal_width(int number
)
158 for (width
= 1, i
= 10; i
<= number
; width
++)