]> git.ipfire.org Git - thirdparty/git.git/blame - pager.c
Add column layout skeleton and git-column
[thirdparty/git.git] / pager.c
CommitLineData
f67b45f8 1#include "cache.h"
ea27a18c 2#include "run-command.h"
a3da8821 3#include "sigchain.h"
f67b45f8 4
a3d023d0
JH
5#ifndef DEFAULT_PAGER
6#define DEFAULT_PAGER "less"
7#endif
8
f67b45f8 9/*
bfdd9ffd
JS
10 * This is split up from the rest of git so that we can do
11 * something different on Windows.
f67b45f8
LT
12 */
13
71064e3f 14#ifndef WIN32
ea27a18c 15static void pager_preexec(void)
f67b45f8 16{
35ce8622
LT
17 /*
18 * Work around bug in "less" by not starting it until we
19 * have real input
20 */
21 fd_set in;
22
23 FD_ZERO(&in);
24 FD_SET(0, &in);
25 select(1, &in, NULL, &in, NULL);
f67b45f8 26}
ea27a18c 27#endif
bfdd9ffd 28
ac0ba18d 29static const char *pager_argv[] = { NULL, NULL };
ea27a18c
JK
30static struct child_process pager_process;
31
bfdd9ffd
JS
32static void wait_for_pager(void)
33{
34 fflush(stdout);
35 fflush(stderr);
36 /* signal EOF to pager */
37 close(1);
38 close(2);
39 finish_command(&pager_process);
40}
f67b45f8 41
a3da8821
JK
42static void wait_for_pager_signal(int signo)
43{
44 wait_for_pager();
45 sigchain_pop(signo);
46 raise(signo);
47}
48
64778d24 49const char *git_pager(int stdout_is_tty)
f67b45f8 50{
63618245 51 const char *pager;
f67b45f8 52
64778d24 53 if (!stdout_is_tty)
63618245
JN
54 return NULL;
55
56 pager = getenv("GIT_PAGER");
cad3a205
JH
57 if (!pager) {
58 if (!pager_program)
ef90d6d4 59 git_config(git_default_config, NULL);
54adf370 60 pager = pager_program;
cad3a205 61 }
c27d205a
ML
62 if (!pager)
63 pager = getenv("PAGER");
402461aa 64 if (!pager)
a3d023d0 65 pager = DEFAULT_PAGER;
caef71a5 66 else if (!*pager || !strcmp(pager, "cat"))
63618245
JN
67 pager = NULL;
68
69 return pager;
70}
71
72void setup_pager(void)
73{
64778d24 74 const char *pager = git_pager(isatty(1));
63618245
JN
75
76 if (!pager)
402461aa
JS
77 return;
78
ad6c3739
ZJS
79 /*
80 * force computing the width of the terminal before we redirect
81 * the standard output to the pager.
82 */
83 (void) term_columns();
84
2e6c012e 85 setenv("GIT_PAGER_IN_USE", "true", 1);
85fb65ed 86
bfdd9ffd 87 /* spawn the pager */
ac0ba18d
JK
88 pager_argv[0] = pager;
89 pager_process.use_shell = 1;
ea27a18c
JK
90 pager_process.argv = pager_argv;
91 pager_process.in = -1;
25fc1786
JS
92 if (!getenv("LESS")) {
93 static const char *env[] = { "LESS=FRSX", NULL };
94 pager_process.env = env;
95 }
71064e3f 96#ifndef WIN32
ea27a18c
JK
97 pager_process.preexec_cb = pager_preexec;
98#endif
bfdd9ffd
JS
99 if (start_command(&pager_process))
100 return;
101
102 /* original process continues, but writes to the pipe */
103 dup2(pager_process.in, 1);
a8335024
JH
104 if (isatty(2))
105 dup2(pager_process.in, 2);
bfdd9ffd
JS
106 close(pager_process.in);
107
108 /* this makes sure that the parent terminates after the pager */
a3da8821 109 sigchain_push_common(wait_for_pager_signal);
bfdd9ffd 110 atexit(wait_for_pager);
f67b45f8 111}
6e9af863
JK
112
113int pager_in_use(void)
114{
115 const char *env;
6e9af863
JK
116 env = getenv("GIT_PAGER_IN_USE");
117 return env ? git_config_bool("GIT_PAGER_IN_USE", env) : 0;
118}
ad6c3739
ZJS
119
120/*
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.
124 */
125int term_columns(void)
126{
127 static int term_columns_at_startup;
128
129 char *col_string;
130 int n_cols;
131
132 if (term_columns_at_startup)
133 return term_columns_at_startup;
134
135 term_columns_at_startup = 80;
136
137 col_string = getenv("COLUMNS");
138 if (col_string && (n_cols = atoi(col_string)) > 0)
139 term_columns_at_startup = n_cols;
140#ifdef TIOCGWINSZ
141 else {
142 struct winsize ws;
143 if (!ioctl(1, TIOCGWINSZ, &ws) && ws.ws_col)
144 term_columns_at_startup = ws.ws_col;
145 }
146#endif
147
148 return term_columns_at_startup;
149}
4d9e079e 150
ec7ff5ba
ZJS
151/*
152 * How many columns do we need to show this number in decimal?
153 */
154int decimal_width(int number)
155{
156 int i, width;
157
158 for (width = 1, i = 10; i <= number; width++)
159 i *= 10;
160 return width;
161}