t9002: work around shells that are unable to set COLUMNS to 1
[git/git.git] / column.c
CommitLineData
7e29b825
NTND
1#include "cache.h"
2#include "column.h"
3#include "string-list.h"
4#include "parse-options.h"
077539d7
NTND
5#include "utf8.h"
6
7#define XY2LINEAR(d, x, y) (COL_LAYOUT((d)->colopts) == COL_COLUMN ? \
8 (x) * (d)->rows + (y) : \
9 (y) * (d)->cols + (x))
10
11struct column_data {
12 const struct string_list *list;
13 unsigned int colopts;
14 struct column_options opts;
15
16 int rows, cols;
17 int *len; /* cell length */
18};
19
20/* return length of 's' in letters, ANSI escapes stripped */
21static int item_length(unsigned int colopts, const char *s)
22{
23 int len, i = 0;
24 struct strbuf str = STRBUF_INIT;
25
26 strbuf_addstr(&str, s);
27 while ((s = strstr(str.buf + i, "\033[")) != NULL) {
28 int len = strspn(s + 2, "0123456789;");
29 i = s - str.buf;
30 strbuf_remove(&str, i, len + 3); /* \033[<len><func char> */
31 }
32 len = utf8_strwidth(str.buf);
33 strbuf_release(&str);
34 return len;
35}
36
37/*
38 * Calculate cell width, rows and cols for a table of equal cells, given
39 * table width and how many spaces between cells.
40 */
41static void layout(struct column_data *data, int *width)
42{
43 int i;
44
45 *width = 0;
46 for (i = 0; i < data->list->nr; i++)
47 if (*width < data->len[i])
48 *width = data->len[i];
49
50 *width += data->opts.padding;
51
52 data->cols = (data->opts.width - strlen(data->opts.indent)) / *width;
53 if (data->cols == 0)
54 data->cols = 1;
55
56 data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
57}
7e29b825
NTND
58
59/* Display without layout when not enabled */
60static void display_plain(const struct string_list *list,
61 const char *indent, const char *nl)
62{
63 int i;
64
65 for (i = 0; i < list->nr; i++)
66 printf("%s%s%s", indent, list->items[i].string, nl);
67}
68
077539d7
NTND
69/* Print a cell to stdout with all necessary leading/traling space */
70static int display_cell(struct column_data *data, int initial_width,
71 const char *empty_cell, int x, int y)
72{
73 int i, len, newline;
74
75 i = XY2LINEAR(data, x, y);
76 if (i >= data->list->nr)
77 return -1;
78 len = data->len[i];
79 if (COL_LAYOUT(data->colopts) == COL_COLUMN)
80 newline = i + data->rows >= data->list->nr;
81 else
82 newline = x == data->cols - 1 || i == data->list->nr - 1;
83
84 printf("%s%s%s",
85 x == 0 ? data->opts.indent : "",
86 data->list->items[i].string,
87 newline ? data->opts.nl : empty_cell + len);
88 return 0;
89}
90
91/* Display COL_COLUMN or COL_ROW */
92static void display_table(const struct string_list *list,
93 unsigned int colopts,
94 const struct column_options *opts)
95{
96 struct column_data data;
97 int x, y, i, initial_width;
98 char *empty_cell;
99
100 memset(&data, 0, sizeof(data));
101 data.list = list;
102 data.colopts = colopts;
103 data.opts = *opts;
104
105 data.len = xmalloc(sizeof(*data.len) * list->nr);
106 for (i = 0; i < list->nr; i++)
107 data.len[i] = item_length(colopts, list->items[i].string);
108
109 layout(&data, &initial_width);
110
111 empty_cell = xmalloc(initial_width + 1);
112 memset(empty_cell, ' ', initial_width);
113 empty_cell[initial_width] = '\0';
114 for (y = 0; y < data.rows; y++) {
115 for (x = 0; x < data.cols; x++)
116 if (display_cell(&data, initial_width, empty_cell, x, y))
117 break;
118 }
119
120 free(data.len);
121 free(empty_cell);
122}
123
7e29b825
NTND
124void print_columns(const struct string_list *list, unsigned int colopts,
125 const struct column_options *opts)
126{
127 struct column_options nopts;
128
129 if (!list->nr)
130 return;
131 assert((colopts & COL_ENABLE_MASK) != COL_AUTO);
132
133 memset(&nopts, 0, sizeof(nopts));
134 nopts.indent = opts && opts->indent ? opts->indent : "";
135 nopts.nl = opts && opts->nl ? opts->nl : "\n";
136 nopts.padding = opts ? opts->padding : 1;
137 nopts.width = opts && opts->width ? opts->width : term_columns() - 1;
138 if (!column_active(colopts)) {
139 display_plain(list, "", "\n");
140 return;
141 }
142 switch (COL_LAYOUT(colopts)) {
143 case COL_PLAIN:
144 display_plain(list, nopts.indent, nopts.nl);
145 break;
077539d7
NTND
146 case COL_ROW:
147 case COL_COLUMN:
148 display_table(list, colopts, &nopts);
149 break;
7e29b825
NTND
150 default:
151 die("BUG: invalid layout mode %d", COL_LAYOUT(colopts));
152 }
153}
154
155int finalize_colopts(unsigned int *colopts, int stdout_is_tty)
156{
157 if ((*colopts & COL_ENABLE_MASK) == COL_AUTO) {
158 if (stdout_is_tty < 0)
159 stdout_is_tty = isatty(1);
160 *colopts &= ~COL_ENABLE_MASK;
161 if (stdout_is_tty)
162 *colopts |= COL_ENABLED;
163 }
164 return 0;
165}
166
167struct colopt {
168 const char *name;
169 unsigned int value;
170 unsigned int mask;
171};
172
173#define LAYOUT_SET 1
174#define ENABLE_SET 2
175
176static int parse_option(const char *arg, int len, unsigned int *colopts,
177 int *group_set)
178{
179 struct colopt opts[] = {
180 { "always", COL_ENABLED, COL_ENABLE_MASK },
181 { "never", COL_DISABLED, COL_ENABLE_MASK },
182 { "auto", COL_AUTO, COL_ENABLE_MASK },
183 { "plain", COL_PLAIN, COL_LAYOUT_MASK },
077539d7
NTND
184 { "column", COL_COLUMN, COL_LAYOUT_MASK },
185 { "row", COL_ROW, COL_LAYOUT_MASK },
7e29b825
NTND
186 };
187 int i;
188
189 for (i = 0; i < ARRAY_SIZE(opts); i++) {
190 int arg_len = len, name_len;
191 const char *arg_str = arg;
192
193 name_len = strlen(opts[i].name);
194 if (arg_len != name_len ||
195 strncmp(arg_str, opts[i].name, name_len))
196 continue;
197
198 switch (opts[i].mask) {
199 case COL_ENABLE_MASK:
200 *group_set |= ENABLE_SET;
201 break;
202 case COL_LAYOUT_MASK:
203 *group_set |= LAYOUT_SET;
204 break;
205 }
206
207 if (opts[i].mask)
208 *colopts = (*colopts & ~opts[i].mask) | opts[i].value;
209 return 0;
210 }
211
212 return error("unsupported option '%s'", arg);
213}
214
215static int parse_config(unsigned int *colopts, const char *value)
216{
217 const char *sep = " ,";
218 int group_set = 0;
219
220 while (*value) {
221 int len = strcspn(value, sep);
222 if (len) {
223 if (parse_option(value, len, colopts, &group_set))
224 return -1;
225
226 value += len;
227 }
228 value += strspn(value, sep);
229 }
230 /*
231 * Setting layout implies "always" if neither always, never
232 * nor auto is specified.
233 *
234 * Current value in COL_ENABLE_MASK is disregarded. This means if
235 * you set column.ui = auto and pass --column=row, then "auto"
236 * will become "always".
237 */
238 if ((group_set & LAYOUT_SET) && !(group_set & ENABLE_SET))
239 *colopts = (*colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
240 return 0;
241}
242
243static int column_config(const char *var, const char *value,
244 const char *key, unsigned int *colopts)
245{
246 if (!value)
247 return config_error_nonbool(var);
248 if (parse_config(colopts, value))
249 return error("invalid column.%s mode %s", key, value);
250 return 0;
251}
252
253int git_column_config(const char *var, const char *value,
254 const char *command, unsigned int *colopts)
255{
256 const char *it = skip_prefix(var, "column.");
257 if (!it)
258 return 0;
259
260 if (!strcmp(it, "ui"))
261 return column_config(var, value, "ui", colopts);
262
263 if (command && !strcmp(it, command))
264 return column_config(var, value, it, colopts);
265
266 return 0;
267}
268
269int parseopt_column_callback(const struct option *opt,
270 const char *arg, int unset)
271{
272 unsigned int *colopts = opt->value;
273 *colopts |= COL_PARSEOPT;
274 *colopts &= ~COL_ENABLE_MASK;
275 if (unset) /* --no-column == never */
276 return 0;
277 /* --column == always unless "arg" states otherwise */
278 *colopts |= COL_ENABLED;
279 if (arg)
280 return parse_config(colopts, arg);
281
282 return 0;
283}