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