3 #include "string-list.h"
4 #include "parse-options.h"
5 #include "run-command.h"
8 #define XY2LINEAR(d, x, y) (COL_LAYOUT((d)->colopts) == COL_COLUMN ? \
9 (x) * (d)->rows + (y) : \
10 (y) * (d)->cols + (x))
13 const struct string_list
*list
;
15 struct column_options opts
;
18 int *len
; /* cell length */
19 int *width
; /* index to the longest row in column */
22 /* return length of 's' in letters, ANSI escapes stripped */
23 static int item_length(unsigned int colopts
, const char *s
)
26 struct strbuf str
= STRBUF_INIT
;
28 strbuf_addstr(&str
, s
);
29 while ((s
= strstr(str
.buf
+ i
, "\033[")) != NULL
) {
30 int len
= strspn(s
+ 2, "0123456789;");
32 strbuf_remove(&str
, i
, len
+ 3); /* \033[<len><func char> */
34 len
= utf8_strwidth(str
.buf
);
40 * Calculate cell width, rows and cols for a table of equal cells, given
41 * table width and how many spaces between cells.
43 static void layout(struct column_data
*data
, int *width
)
48 for (i
= 0; i
< data
->list
->nr
; i
++)
49 if (*width
< data
->len
[i
])
50 *width
= data
->len
[i
];
52 *width
+= data
->opts
.padding
;
54 data
->cols
= (data
->opts
.width
- strlen(data
->opts
.indent
)) / *width
;
58 data
->rows
= DIV_ROUND_UP(data
->list
->nr
, data
->cols
);
61 static void compute_column_width(struct column_data
*data
)
64 for (x
= 0; x
< data
->cols
; x
++) {
65 data
->width
[x
] = XY2LINEAR(data
, x
, 0);
66 for (y
= 0; y
< data
->rows
; y
++) {
67 i
= XY2LINEAR(data
, x
, y
);
68 if (i
< data
->list
->nr
&&
69 data
->len
[data
->width
[x
]] < data
->len
[i
])
76 * Shrink all columns by shortening them one row each time (and adding
77 * more columns along the way). Hopefully the longest cell will be
78 * moved to the next column, column is shrunk so we have more space
79 * for new columns. The process ends when the whole thing no longer
80 * fits in data->total_width.
82 static void shrink_columns(struct column_data
*data
)
84 data
->width
= xrealloc(data
->width
,
85 sizeof(*data
->width
) * data
->cols
);
86 while (data
->rows
> 1) {
87 int x
, total_width
, cols
, rows
;
92 data
->cols
= DIV_ROUND_UP(data
->list
->nr
, data
->rows
);
93 if (data
->cols
!= cols
)
94 data
->width
= xrealloc(data
->width
,
95 sizeof(*data
->width
) * data
->cols
);
96 compute_column_width(data
);
98 total_width
= strlen(data
->opts
.indent
);
99 for (x
= 0; x
< data
->cols
; x
++) {
100 total_width
+= data
->len
[data
->width
[x
]];
101 total_width
+= data
->opts
.padding
;
103 if (total_width
> data
->opts
.width
) {
109 compute_column_width(data
);
112 /* Display without layout when not enabled */
113 static void display_plain(const struct string_list
*list
,
114 const char *indent
, const char *nl
)
118 for (i
= 0; i
< list
->nr
; i
++)
119 printf("%s%s%s", indent
, list
->items
[i
].string
, nl
);
122 /* Print a cell to stdout with all necessary leading/traling space */
123 static int display_cell(struct column_data
*data
, int initial_width
,
124 const char *empty_cell
, int x
, int y
)
128 i
= XY2LINEAR(data
, x
, y
);
129 if (i
>= data
->list
->nr
)
133 if (data
->width
&& data
->len
[data
->width
[x
]] < initial_width
) {
135 * empty_cell has initial_width chars, if real column
136 * is narrower, increase len a bit so we fill less
139 len
+= initial_width
- data
->len
[data
->width
[x
]];
140 len
-= data
->opts
.padding
;
143 if (COL_LAYOUT(data
->colopts
) == COL_COLUMN
)
144 newline
= i
+ data
->rows
>= data
->list
->nr
;
146 newline
= x
== data
->cols
- 1 || i
== data
->list
->nr
- 1;
149 x
== 0 ? data
->opts
.indent
: "",
150 data
->list
->items
[i
].string
,
151 newline
? data
->opts
.nl
: empty_cell
+ len
);
155 /* Display COL_COLUMN or COL_ROW */
156 static void display_table(const struct string_list
*list
,
157 unsigned int colopts
,
158 const struct column_options
*opts
)
160 struct column_data data
;
161 int x
, y
, i
, initial_width
;
164 memset(&data
, 0, sizeof(data
));
166 data
.colopts
= colopts
;
169 data
.len
= xmalloc(sizeof(*data
.len
) * list
->nr
);
170 for (i
= 0; i
< list
->nr
; i
++)
171 data
.len
[i
] = item_length(colopts
, list
->items
[i
].string
);
173 layout(&data
, &initial_width
);
175 if (colopts
& COL_DENSE
)
176 shrink_columns(&data
);
178 empty_cell
= xmalloc(initial_width
+ 1);
179 memset(empty_cell
, ' ', initial_width
);
180 empty_cell
[initial_width
] = '\0';
181 for (y
= 0; y
< data
.rows
; y
++) {
182 for (x
= 0; x
< data
.cols
; x
++)
183 if (display_cell(&data
, initial_width
, empty_cell
, x
, y
))
192 void print_columns(const struct string_list
*list
, unsigned int colopts
,
193 const struct column_options
*opts
)
195 struct column_options nopts
;
199 assert((colopts
& COL_ENABLE_MASK
) != COL_AUTO
);
201 memset(&nopts
, 0, sizeof(nopts
));
202 nopts
.indent
= opts
&& opts
->indent
? opts
->indent
: "";
203 nopts
.nl
= opts
&& opts
->nl
? opts
->nl
: "\n";
204 nopts
.padding
= opts
? opts
->padding
: 1;
205 nopts
.width
= opts
&& opts
->width
? opts
->width
: term_columns() - 1;
206 if (!column_active(colopts
)) {
207 display_plain(list
, "", "\n");
210 switch (COL_LAYOUT(colopts
)) {
212 display_plain(list
, nopts
.indent
, nopts
.nl
);
216 display_table(list
, colopts
, &nopts
);
219 die("BUG: invalid layout mode %d", COL_LAYOUT(colopts
));
223 int finalize_colopts(unsigned int *colopts
, int stdout_is_tty
)
225 if ((*colopts
& COL_ENABLE_MASK
) == COL_AUTO
) {
226 if (stdout_is_tty
< 0)
227 stdout_is_tty
= isatty(1);
228 *colopts
&= ~COL_ENABLE_MASK
;
230 *colopts
|= COL_ENABLED
;
244 static int parse_option(const char *arg
, int len
, unsigned int *colopts
,
247 struct colopt opts
[] = {
248 { "always", COL_ENABLED
, COL_ENABLE_MASK
},
249 { "never", COL_DISABLED
, COL_ENABLE_MASK
},
250 { "auto", COL_AUTO
, COL_ENABLE_MASK
},
251 { "plain", COL_PLAIN
, COL_LAYOUT_MASK
},
252 { "column", COL_COLUMN
, COL_LAYOUT_MASK
},
253 { "row", COL_ROW
, COL_LAYOUT_MASK
},
254 { "dense", COL_DENSE
, 0 },
258 for (i
= 0; i
< ARRAY_SIZE(opts
); i
++) {
259 int set
= 1, arg_len
= len
, name_len
;
260 const char *arg_str
= arg
;
263 if (arg_len
> 2 && !strncmp(arg_str
, "no", 2)) {
270 name_len
= strlen(opts
[i
].name
);
271 if (arg_len
!= name_len
||
272 strncmp(arg_str
, opts
[i
].name
, name_len
))
275 switch (opts
[i
].mask
) {
276 case COL_ENABLE_MASK
:
277 *group_set
|= ENABLE_SET
;
279 case COL_LAYOUT_MASK
:
280 *group_set
|= LAYOUT_SET
;
285 *colopts
= (*colopts
& ~opts
[i
].mask
) | opts
[i
].value
;
288 *colopts
|= opts
[i
].value
;
290 *colopts
&= ~opts
[i
].value
;
295 return error("unsupported option '%s'", arg
);
298 static int parse_config(unsigned int *colopts
, const char *value
)
300 const char *sep
= " ,";
304 int len
= strcspn(value
, sep
);
306 if (parse_option(value
, len
, colopts
, &group_set
))
311 value
+= strspn(value
, sep
);
314 * If none of "always", "never", and "auto" is specified, then setting
315 * layout implies "always".
317 * Current value in COL_ENABLE_MASK is disregarded. This means if
318 * you set column.ui = auto and pass --column=row, then "auto"
319 * will become "always".
321 if ((group_set
& LAYOUT_SET
) && !(group_set
& ENABLE_SET
))
322 *colopts
= (*colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
326 static int column_config(const char *var
, const char *value
,
327 const char *key
, unsigned int *colopts
)
330 return config_error_nonbool(var
);
331 if (parse_config(colopts
, value
))
332 return error("invalid column.%s mode %s", key
, value
);
336 int git_column_config(const char *var
, const char *value
,
337 const char *command
, unsigned int *colopts
)
341 if (!skip_prefix(var
, "column.", &it
))
344 if (!strcmp(it
, "ui"))
345 return column_config(var
, value
, "ui", colopts
);
347 if (command
&& !strcmp(it
, command
))
348 return column_config(var
, value
, it
, colopts
);
353 int parseopt_column_callback(const struct option
*opt
,
354 const char *arg
, int unset
)
356 unsigned int *colopts
= opt
->value
;
357 *colopts
|= COL_PARSEOPT
;
358 *colopts
&= ~COL_ENABLE_MASK
;
359 if (unset
) /* --no-column == never */
361 /* --column == always unless "arg" states otherwise */
362 *colopts
|= COL_ENABLED
;
364 return parse_config(colopts
, arg
);
369 static int fd_out
= -1;
370 static struct child_process column_process
;
372 int run_column_filter(int colopts
, const struct column_options
*opts
)
374 struct argv_array
*argv
;
379 memset(&column_process
, 0, sizeof(column_process
));
380 argv
= &column_process
.args
;
382 argv_array_push(argv
, "column");
383 argv_array_pushf(argv
, "--raw-mode=%d", colopts
);
384 if (opts
&& opts
->width
)
385 argv_array_pushf(argv
, "--width=%d", opts
->width
);
386 if (opts
&& opts
->indent
)
387 argv_array_pushf(argv
, "--indent=%s", opts
->indent
);
388 if (opts
&& opts
->padding
)
389 argv_array_pushf(argv
, "--padding=%d", opts
->padding
);
392 column_process
.in
= -1;
393 column_process
.out
= dup(1);
394 column_process
.git_cmd
= 1;
396 if (start_command(&column_process
))
401 dup2(column_process
.in
, 1);
402 close(column_process
.in
);
406 int stop_column_filter(void)
413 finish_command(&column_process
);