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