Merge branch 'aj/stash-staged-fix' into maint-2.45
[alt-git.git] / column.c
blob50bbccc92ee86cf754a6b3250e52338a1870c990
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"
10 #define XY2LINEAR(d, x, y) (COL_LAYOUT((d)->colopts) == COL_COLUMN ? \
11 (x) * (d)->rows + (y) : \
12 (y) * (d)->cols + (x))
14 struct column_data {
15 const struct string_list *list;
16 unsigned int colopts;
17 struct column_options opts;
19 int rows, cols;
20 int *len; /* cell length */
21 int *width; /* index to the longest row in column */
24 /* return length of 's' in letters, ANSI escapes stripped */
25 static int item_length(const char *s)
27 return utf8_strnwidth(s, strlen(s), 1);
31 * Calculate cell width, rows and cols for a table of equal cells, given
32 * table width and how many spaces between cells.
34 static void layout(struct column_data *data, int *width)
36 int i;
38 *width = 0;
39 for (i = 0; i < data->list->nr; i++)
40 if (*width < data->len[i])
41 *width = data->len[i];
43 *width += data->opts.padding;
45 data->cols = (data->opts.width - strlen(data->opts.indent)) / *width;
46 if (data->cols == 0)
47 data->cols = 1;
49 data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
52 static void compute_column_width(struct column_data *data)
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;
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.
73 static void shrink_columns(struct column_data *data)
75 REALLOC_ARRAY(data->width, data->cols);
76 while (data->rows > 1) {
77 int x, total_width, cols, rows;
78 rows = data->rows;
79 cols = data->cols;
81 data->rows--;
82 data->cols = DIV_ROUND_UP(data->list->nr, data->rows);
83 if (data->cols != cols)
84 REALLOC_ARRAY(data->width, data->cols);
85 compute_column_width(data);
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;
92 if (total_width > data->opts.width) {
93 data->rows = rows;
94 data->cols = cols;
95 break;
98 compute_column_width(data);
101 /* Display without layout when not enabled */
102 static void display_plain(const struct string_list *list,
103 const char *indent, const char *nl)
105 int i;
107 for (i = 0; i < list->nr; i++)
108 printf("%s%s%s", indent, list->items[i].string, nl);
111 /* Print a cell to stdout with all necessary leading/trailing space */
112 static int display_cell(struct column_data *data, int initial_width,
113 const char *empty_cell, int x, int y)
115 int i, len, newline;
117 i = XY2LINEAR(data, x, y);
118 if (i >= data->list->nr)
119 return -1;
121 len = data->len[i];
122 if (data->width && data->len[data->width[x]] < initial_width) {
124 * empty_cell has initial_width chars, if real column
125 * is narrower, increase len a bit so we fill less
126 * space.
128 len += initial_width - data->len[data->width[x]];
129 len -= data->opts.padding;
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;
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;
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)
149 struct column_data data;
150 int x, y, i, initial_width;
151 char *empty_cell;
153 memset(&data, 0, sizeof(data));
154 data.list = list;
155 data.colopts = colopts;
156 data.opts = *opts;
158 ALLOC_ARRAY(data.len, list->nr);
159 for (i = 0; i < list->nr; i++)
160 data.len[i] = item_length(list->items[i].string);
162 layout(&data, &initial_width);
164 if (colopts & COL_DENSE)
165 shrink_columns(&data);
167 empty_cell = xmallocz(initial_width);
168 memset(empty_cell, ' ', initial_width);
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;
175 free(data.len);
176 free(data.width);
177 free(empty_cell);
180 void print_columns(const struct string_list *list, unsigned int colopts,
181 const struct column_options *opts)
183 struct column_options nopts;
185 if (opts && (0 > opts->padding))
186 BUG("padding must be non-negative");
187 if (!list->nr)
188 return;
189 assert((colopts & COL_ENABLE_MASK) != COL_AUTO);
191 memset(&nopts, 0, sizeof(nopts));
192 nopts.indent = opts && opts->indent ? opts->indent : "";
193 nopts.nl = opts && opts->nl ? opts->nl : "\n";
194 nopts.padding = opts ? opts->padding : 1;
195 nopts.width = opts && opts->width ? opts->width : term_columns() - 1;
196 if (!column_active(colopts)) {
197 display_plain(list, "", "\n");
198 return;
200 switch (COL_LAYOUT(colopts)) {
201 case COL_PLAIN:
202 display_plain(list, nopts.indent, nopts.nl);
203 break;
204 case COL_ROW:
205 case COL_COLUMN:
206 display_table(list, colopts, &nopts);
207 break;
208 default:
209 BUG("invalid layout mode %d", COL_LAYOUT(colopts));
213 int finalize_colopts(unsigned int *colopts, int stdout_is_tty)
215 if ((*colopts & COL_ENABLE_MASK) == COL_AUTO) {
216 if (stdout_is_tty < 0)
217 stdout_is_tty = isatty(1);
218 *colopts &= ~COL_ENABLE_MASK;
219 if (stdout_is_tty || pager_in_use())
220 *colopts |= COL_ENABLED;
222 return 0;
225 struct colopt {
226 const char *name;
227 unsigned int value;
228 unsigned int mask;
231 #define LAYOUT_SET 1
232 #define ENABLE_SET 2
234 static int parse_option(const char *arg, int len, unsigned int *colopts,
235 int *group_set)
237 struct 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 },
246 int i;
248 for (i = 0; i < ARRAY_SIZE(opts); i++) {
249 int set = 1, arg_len = len, name_len;
250 const char *arg_str = arg;
252 if (!opts[i].mask) {
253 if (arg_len > 2 && !strncmp(arg_str, "no", 2)) {
254 arg_str += 2;
255 arg_len -= 2;
256 set = 0;
260 name_len = strlen(opts[i].name);
261 if (arg_len != name_len ||
262 strncmp(arg_str, opts[i].name, name_len))
263 continue;
265 switch (opts[i].mask) {
266 case COL_ENABLE_MASK:
267 *group_set |= ENABLE_SET;
268 break;
269 case COL_LAYOUT_MASK:
270 *group_set |= LAYOUT_SET;
271 break;
274 if (opts[i].mask)
275 *colopts = (*colopts & ~opts[i].mask) | opts[i].value;
276 else {
277 if (set)
278 *colopts |= opts[i].value;
279 else
280 *colopts &= ~opts[i].value;
282 return 0;
285 return error("unsupported option '%s'", arg);
288 static int parse_config(unsigned int *colopts, const char *value)
290 const char *sep = " ,";
291 int group_set = 0;
293 while (*value) {
294 int len = strcspn(value, sep);
295 if (len) {
296 if (parse_option(value, len, colopts, &group_set))
297 return -1;
299 value += len;
301 value += strspn(value, sep);
304 * If none of "always", "never", and "auto" is specified, then setting
305 * layout implies "always".
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".
311 if ((group_set & LAYOUT_SET) && !(group_set & ENABLE_SET))
312 *colopts = (*colopts & ~COL_ENABLE_MASK) | COL_ENABLED;
313 return 0;
316 static int column_config(const char *var, const char *value,
317 const char *key, unsigned int *colopts)
319 if (!value)
320 return config_error_nonbool(var);
321 if (parse_config(colopts, value))
322 return error("invalid column.%s mode %s", key, value);
323 return 0;
326 int git_column_config(const char *var, const char *value,
327 const char *command, unsigned int *colopts)
329 const char *it;
331 if (!skip_prefix(var, "column.", &it))
332 return 0;
334 if (!strcmp(it, "ui"))
335 return column_config(var, value, "ui", colopts);
337 if (command && !strcmp(it, command))
338 return column_config(var, value, it, colopts);
340 return 0;
343 int parseopt_column_callback(const struct option *opt,
344 const char *arg, int unset)
346 unsigned int *colopts = opt->value;
347 *colopts |= COL_PARSEOPT;
348 *colopts &= ~COL_ENABLE_MASK;
349 if (unset) /* --no-column == never */
350 return 0;
351 /* --column == always unless "arg" states otherwise */
352 *colopts |= COL_ENABLED;
353 if (arg)
354 return parse_config(colopts, arg);
356 return 0;
359 static int fd_out = -1;
360 static struct child_process column_process = CHILD_PROCESS_INIT;
362 int run_column_filter(int colopts, const struct column_options *opts)
364 struct strvec *argv;
366 if (opts && (0 > opts->padding))
367 BUG("padding must be non-negative");
368 if (fd_out != -1)
369 return -1;
371 child_process_init(&column_process);
372 argv = &column_process.args;
374 strvec_push(argv, "column");
375 strvec_pushf(argv, "--raw-mode=%d", colopts);
376 if (opts && opts->width)
377 strvec_pushf(argv, "--width=%d", opts->width);
378 if (opts && opts->indent)
379 strvec_pushf(argv, "--indent=%s", opts->indent);
380 if (opts && opts->padding)
381 strvec_pushf(argv, "--padding=%d", opts->padding);
383 fflush(stdout);
384 column_process.in = -1;
385 column_process.out = dup(1);
386 column_process.git_cmd = 1;
388 if (start_command(&column_process))
389 return -2;
391 fd_out = dup(1);
392 close(1);
393 dup2(column_process.in, 1);
394 close(column_process.in);
395 return 0;
398 int stop_column_filter(void)
400 if (fd_out == -1)
401 return -1;
403 fflush(stdout);
404 close(1);
405 finish_command(&column_process);
406 dup2(fd_out, 1);
407 close(fd_out);
408 fd_out = -1;
409 return 0;