]>
Commit | Line | Data |
---|---|---|
77f091ed | 1 | #include "git-compat-util.h" |
b2141fc1 | 2 | #include "config.h" |
7e29b825 NTND |
3 | #include "column.h" |
4 | #include "string-list.h" | |
ca4eed70 | 5 | #include "pager.h" |
7e29b825 | 6 | #include "parse-options.h" |
b27004eb | 7 | #include "run-command.h" |
077539d7 NTND |
8 | #include "utf8.h" |
9 | ||
10 | #define XY2LINEAR(d, x, y) (COL_LAYOUT((d)->colopts) == COL_COLUMN ? \ | |
11 | (x) * (d)->rows + (y) : \ | |
12 | (y) * (d)->cols + (x)) | |
13 | ||
14 | struct column_data { | |
15 | const struct string_list *list; | |
16 | unsigned int colopts; | |
17 | struct column_options opts; | |
18 | ||
19 | int rows, cols; | |
20 | int *len; /* cell length */ | |
3f8eccbe | 21 | int *width; /* index to the longest row in column */ |
077539d7 NTND |
22 | }; |
23 | ||
24 | /* return length of 's' in letters, ANSI escapes stripped */ | |
acbf33f8 | 25 | static int item_length(const char *s) |
077539d7 | 26 | { |
522cc87f | 27 | return utf8_strnwidth(s, strlen(s), 1); |
077539d7 NTND |
28 | } |
29 | ||
30 | /* | |
31 | * Calculate cell width, rows and cols for a table of equal cells, given | |
32 | * table width and how many spaces between cells. | |
33 | */ | |
34 | static void layout(struct column_data *data, int *width) | |
35 | { | |
36 | int i; | |
37 | ||
38 | *width = 0; | |
39 | for (i = 0; i < data->list->nr; i++) | |
40 | if (*width < data->len[i]) | |
41 | *width = data->len[i]; | |
42 | ||
43 | *width += data->opts.padding; | |
44 | ||
45 | data->cols = (data->opts.width - strlen(data->opts.indent)) / *width; | |
46 | if (data->cols == 0) | |
47 | data->cols = 1; | |
48 | ||
49 | data->rows = DIV_ROUND_UP(data->list->nr, data->cols); | |
50 | } | |
7e29b825 | 51 | |
3f8eccbe NTND |
52 | static void compute_column_width(struct column_data *data) |
53 | { | |
54 | int i, x, y; | |
55 | for (x = 0; x < data->cols; x++) { | |
56 | data->width[x] = XY2LINEAR(data, x, 0); | |
57 | for (y = 0; y < data->rows; y++) { | |
58 | i = XY2LINEAR(data, x, y); | |
59 | if (i < data->list->nr && | |
60 | data->len[data->width[x]] < data->len[i]) | |
61 | data->width[x] = i; | |
62 | } | |
63 | } | |
64 | } | |
65 | ||
66 | /* | |
67 | * Shrink all columns by shortening them one row each time (and adding | |
68 | * more columns along the way). Hopefully the longest cell will be | |
69 | * moved to the next column, column is shrunk so we have more space | |
70 | * for new columns. The process ends when the whole thing no longer | |
71 | * fits in data->total_width. | |
72 | */ | |
73 | static void shrink_columns(struct column_data *data) | |
74 | { | |
2756ca43 | 75 | REALLOC_ARRAY(data->width, data->cols); |
3f8eccbe NTND |
76 | while (data->rows > 1) { |
77 | int x, total_width, cols, rows; | |
78 | rows = data->rows; | |
79 | cols = data->cols; | |
80 | ||
81 | data->rows--; | |
82 | data->cols = DIV_ROUND_UP(data->list->nr, data->rows); | |
83 | if (data->cols != cols) | |
2756ca43 | 84 | REALLOC_ARRAY(data->width, data->cols); |
3f8eccbe NTND |
85 | compute_column_width(data); |
86 | ||
87 | total_width = strlen(data->opts.indent); | |
88 | for (x = 0; x < data->cols; x++) { | |
89 | total_width += data->len[data->width[x]]; | |
90 | total_width += data->opts.padding; | |
91 | } | |
92 | if (total_width > data->opts.width) { | |
93 | data->rows = rows; | |
94 | data->cols = cols; | |
95 | break; | |
96 | } | |
97 | } | |
98 | compute_column_width(data); | |
99 | } | |
100 | ||
7e29b825 NTND |
101 | /* Display without layout when not enabled */ |
102 | static void display_plain(const struct string_list *list, | |
103 | const char *indent, const char *nl) | |
104 | { | |
105 | int i; | |
106 | ||
107 | for (i = 0; i < list->nr; i++) | |
108 | printf("%s%s%s", indent, list->items[i].string, nl); | |
109 | } | |
110 | ||
84544f2e | 111 | /* Print a cell to stdout with all necessary leading/trailing space */ |
077539d7 NTND |
112 | static int display_cell(struct column_data *data, int initial_width, |
113 | const char *empty_cell, int x, int y) | |
114 | { | |
115 | int i, len, newline; | |
116 | ||
117 | i = XY2LINEAR(data, x, y); | |
118 | if (i >= data->list->nr) | |
119 | return -1; | |
3f8eccbe | 120 | |
077539d7 | 121 | len = data->len[i]; |
3f8eccbe NTND |
122 | if (data->width && data->len[data->width[x]] < initial_width) { |
123 | /* | |
124 | * empty_cell has initial_width chars, if real column | |
125 | * is narrower, increase len a bit so we fill less | |
126 | * space. | |
127 | */ | |
128 | len += initial_width - data->len[data->width[x]]; | |
129 | len -= data->opts.padding; | |
130 | } | |
131 | ||
077539d7 NTND |
132 | if (COL_LAYOUT(data->colopts) == COL_COLUMN) |
133 | newline = i + data->rows >= data->list->nr; | |
134 | else | |
135 | newline = x == data->cols - 1 || i == data->list->nr - 1; | |
136 | ||
137 | printf("%s%s%s", | |
138 | x == 0 ? data->opts.indent : "", | |
139 | data->list->items[i].string, | |
140 | newline ? data->opts.nl : empty_cell + len); | |
141 | return 0; | |
142 | } | |
143 | ||
144 | /* Display COL_COLUMN or COL_ROW */ | |
145 | static void display_table(const struct string_list *list, | |
146 | unsigned int colopts, | |
147 | const struct column_options *opts) | |
148 | { | |
149 | struct column_data data; | |
150 | int x, y, i, initial_width; | |
151 | char *empty_cell; | |
152 | ||
153 | memset(&data, 0, sizeof(data)); | |
154 | data.list = list; | |
155 | data.colopts = colopts; | |
156 | data.opts = *opts; | |
157 | ||
b32fa95f | 158 | ALLOC_ARRAY(data.len, list->nr); |
077539d7 | 159 | for (i = 0; i < list->nr; i++) |
acbf33f8 | 160 | data.len[i] = item_length(list->items[i].string); |
077539d7 NTND |
161 | |
162 | layout(&data, &initial_width); | |
163 | ||
3f8eccbe NTND |
164 | if (colopts & COL_DENSE) |
165 | shrink_columns(&data); | |
166 | ||
3733e694 | 167 | empty_cell = xmallocz(initial_width); |
077539d7 | 168 | memset(empty_cell, ' ', initial_width); |
077539d7 NTND |
169 | for (y = 0; y < data.rows; y++) { |
170 | for (x = 0; x < data.cols; x++) | |
171 | if (display_cell(&data, initial_width, empty_cell, x, y)) | |
172 | break; | |
173 | } | |
174 | ||
175 | free(data.len); | |
3f8eccbe | 176 | free(data.width); |
077539d7 NTND |
177 | free(empty_cell); |
178 | } | |
179 | ||
7e29b825 NTND |
180 | void print_columns(const struct string_list *list, unsigned int colopts, |
181 | const struct column_options *opts) | |
182 | { | |
183 | struct column_options nopts; | |
184 | ||
185 | if (!list->nr) | |
186 | return; | |
187 | assert((colopts & COL_ENABLE_MASK) != COL_AUTO); | |
188 | ||
189 | memset(&nopts, 0, sizeof(nopts)); | |
190 | nopts.indent = opts && opts->indent ? opts->indent : ""; | |
191 | nopts.nl = opts && opts->nl ? opts->nl : "\n"; | |
192 | nopts.padding = opts ? opts->padding : 1; | |
193 | nopts.width = opts && opts->width ? opts->width : term_columns() - 1; | |
194 | if (!column_active(colopts)) { | |
195 | display_plain(list, "", "\n"); | |
196 | return; | |
197 | } | |
198 | switch (COL_LAYOUT(colopts)) { | |
199 | case COL_PLAIN: | |
200 | display_plain(list, nopts.indent, nopts.nl); | |
201 | break; | |
077539d7 NTND |
202 | case COL_ROW: |
203 | case COL_COLUMN: | |
204 | display_table(list, colopts, &nopts); | |
205 | break; | |
7e29b825 | 206 | default: |
033abf97 | 207 | BUG("invalid layout mode %d", COL_LAYOUT(colopts)); |
7e29b825 NTND |
208 | } |
209 | } | |
210 | ||
211 | int finalize_colopts(unsigned int *colopts, int stdout_is_tty) | |
212 | { | |
213 | if ((*colopts & COL_ENABLE_MASK) == COL_AUTO) { | |
214 | if (stdout_is_tty < 0) | |
215 | stdout_is_tty = isatty(1); | |
216 | *colopts &= ~COL_ENABLE_MASK; | |
b2d3fd28 | 217 | if (stdout_is_tty || pager_in_use()) |
7e29b825 NTND |
218 | *colopts |= COL_ENABLED; |
219 | } | |
220 | return 0; | |
221 | } | |
222 | ||
223 | struct colopt { | |
224 | const char *name; | |
225 | unsigned int value; | |
226 | unsigned int mask; | |
227 | }; | |
228 | ||
229 | #define LAYOUT_SET 1 | |
230 | #define ENABLE_SET 2 | |
231 | ||
232 | static int parse_option(const char *arg, int len, unsigned int *colopts, | |
233 | int *group_set) | |
234 | { | |
235 | struct colopt opts[] = { | |
236 | { "always", COL_ENABLED, COL_ENABLE_MASK }, | |
237 | { "never", COL_DISABLED, COL_ENABLE_MASK }, | |
238 | { "auto", COL_AUTO, COL_ENABLE_MASK }, | |
239 | { "plain", COL_PLAIN, COL_LAYOUT_MASK }, | |
077539d7 NTND |
240 | { "column", COL_COLUMN, COL_LAYOUT_MASK }, |
241 | { "row", COL_ROW, COL_LAYOUT_MASK }, | |
3f8eccbe | 242 | { "dense", COL_DENSE, 0 }, |
7e29b825 NTND |
243 | }; |
244 | int i; | |
245 | ||
246 | for (i = 0; i < ARRAY_SIZE(opts); i++) { | |
3f8eccbe | 247 | int set = 1, arg_len = len, name_len; |
7e29b825 NTND |
248 | const char *arg_str = arg; |
249 | ||
3f8eccbe NTND |
250 | if (!opts[i].mask) { |
251 | if (arg_len > 2 && !strncmp(arg_str, "no", 2)) { | |
252 | arg_str += 2; | |
253 | arg_len -= 2; | |
254 | set = 0; | |
255 | } | |
256 | } | |
257 | ||
7e29b825 NTND |
258 | name_len = strlen(opts[i].name); |
259 | if (arg_len != name_len || | |
260 | strncmp(arg_str, opts[i].name, name_len)) | |
261 | continue; | |
262 | ||
263 | switch (opts[i].mask) { | |
264 | case COL_ENABLE_MASK: | |
265 | *group_set |= ENABLE_SET; | |
266 | break; | |
267 | case COL_LAYOUT_MASK: | |
268 | *group_set |= LAYOUT_SET; | |
269 | break; | |
270 | } | |
271 | ||
272 | if (opts[i].mask) | |
273 | *colopts = (*colopts & ~opts[i].mask) | opts[i].value; | |
3f8eccbe NTND |
274 | else { |
275 | if (set) | |
276 | *colopts |= opts[i].value; | |
277 | else | |
278 | *colopts &= ~opts[i].value; | |
279 | } | |
7e29b825 NTND |
280 | return 0; |
281 | } | |
282 | ||
283 | return error("unsupported option '%s'", arg); | |
284 | } | |
285 | ||
286 | static int parse_config(unsigned int *colopts, const char *value) | |
287 | { | |
288 | const char *sep = " ,"; | |
289 | int group_set = 0; | |
290 | ||
291 | while (*value) { | |
292 | int len = strcspn(value, sep); | |
293 | if (len) { | |
294 | if (parse_option(value, len, colopts, &group_set)) | |
295 | return -1; | |
296 | ||
297 | value += len; | |
298 | } | |
299 | value += strspn(value, sep); | |
300 | } | |
301 | /* | |
01689909 JL |
302 | * If none of "always", "never", and "auto" is specified, then setting |
303 | * layout implies "always". | |
7e29b825 NTND |
304 | * |
305 | * Current value in COL_ENABLE_MASK is disregarded. This means if | |
306 | * you set column.ui = auto and pass --column=row, then "auto" | |
307 | * will become "always". | |
308 | */ | |
309 | if ((group_set & LAYOUT_SET) && !(group_set & ENABLE_SET)) | |
310 | *colopts = (*colopts & ~COL_ENABLE_MASK) | COL_ENABLED; | |
311 | return 0; | |
312 | } | |
313 | ||
314 | static int column_config(const char *var, const char *value, | |
315 | const char *key, unsigned int *colopts) | |
316 | { | |
317 | if (!value) | |
318 | return config_error_nonbool(var); | |
319 | if (parse_config(colopts, value)) | |
320 | return error("invalid column.%s mode %s", key, value); | |
321 | return 0; | |
322 | } | |
323 | ||
324 | int git_column_config(const char *var, const char *value, | |
325 | const char *command, unsigned int *colopts) | |
326 | { | |
cf4fff57 JK |
327 | const char *it; |
328 | ||
329 | if (!skip_prefix(var, "column.", &it)) | |
7e29b825 NTND |
330 | return 0; |
331 | ||
332 | if (!strcmp(it, "ui")) | |
333 | return column_config(var, value, "ui", colopts); | |
334 | ||
335 | if (command && !strcmp(it, command)) | |
336 | return column_config(var, value, it, colopts); | |
337 | ||
338 | return 0; | |
339 | } | |
340 | ||
341 | int parseopt_column_callback(const struct option *opt, | |
342 | const char *arg, int unset) | |
343 | { | |
344 | unsigned int *colopts = opt->value; | |
345 | *colopts |= COL_PARSEOPT; | |
346 | *colopts &= ~COL_ENABLE_MASK; | |
347 | if (unset) /* --no-column == never */ | |
348 | return 0; | |
349 | /* --column == always unless "arg" states otherwise */ | |
350 | *colopts |= COL_ENABLED; | |
351 | if (arg) | |
352 | return parse_config(colopts, arg); | |
353 | ||
354 | return 0; | |
355 | } | |
b27004eb NTND |
356 | |
357 | static int fd_out = -1; | |
d3180279 | 358 | static struct child_process column_process = CHILD_PROCESS_INIT; |
b27004eb NTND |
359 | |
360 | int run_column_filter(int colopts, const struct column_options *opts) | |
361 | { | |
ef8d7ac4 | 362 | struct strvec *argv; |
b27004eb NTND |
363 | |
364 | if (fd_out != -1) | |
365 | return -1; | |
366 | ||
8828f298 | 367 | child_process_init(&column_process); |
5eb7f7ea JK |
368 | argv = &column_process.args; |
369 | ||
ef8d7ac4 JK |
370 | strvec_push(argv, "column"); |
371 | strvec_pushf(argv, "--raw-mode=%d", colopts); | |
5eb7f7ea | 372 | if (opts && opts->width) |
ef8d7ac4 | 373 | strvec_pushf(argv, "--width=%d", opts->width); |
5eb7f7ea | 374 | if (opts && opts->indent) |
ef8d7ac4 | 375 | strvec_pushf(argv, "--indent=%s", opts->indent); |
5eb7f7ea | 376 | if (opts && opts->padding) |
ef8d7ac4 | 377 | strvec_pushf(argv, "--padding=%d", opts->padding); |
b27004eb NTND |
378 | |
379 | fflush(stdout); | |
b27004eb NTND |
380 | column_process.in = -1; |
381 | column_process.out = dup(1); | |
382 | column_process.git_cmd = 1; | |
b27004eb | 383 | |
5eb7f7ea | 384 | if (start_command(&column_process)) |
b27004eb NTND |
385 | return -2; |
386 | ||
387 | fd_out = dup(1); | |
388 | close(1); | |
389 | dup2(column_process.in, 1); | |
390 | close(column_process.in); | |
391 | return 0; | |
392 | } | |
393 | ||
394 | int stop_column_filter(void) | |
395 | { | |
396 | if (fd_out == -1) | |
397 | return -1; | |
398 | ||
399 | fflush(stdout); | |
400 | close(1); | |
401 | finish_command(&column_process); | |
402 | dup2(fd_out, 1); | |
403 | close(fd_out); | |
404 | fd_out = -1; | |
405 | return 0; | |
406 | } |