]>
git.ipfire.org Git - thirdparty/git.git/blob - column.c
d6c115c1bfff99bd54b4a3413a22da2dd524b956
3 #include "string-list.h"
4 #include "parse-options.h"
7 #define XY2LINEAR(d, x, y) (COL_LAYOUT((d)->colopts) == COL_COLUMN ? \
8 (x) * (d)->rows + (y) : \
12 const struct string_list
*list
;
14 struct column_options opts
;
17 int *len
; /* cell length */
18 int *width
; /* index to the longest row in column */
21 /* return length of 's' in letters, ANSI escapes stripped */
22 static int item_length(unsigned int colopts
, const char *s
)
25 struct strbuf str
= STRBUF_INIT
;
27 strbuf_addstr(&str
, s
);
28 while ((s
= strstr(str
.buf
+ i
, "\033[")) != NULL
) {
29 int len
= strspn(s
+ 2, "0123456789;");
31 strbuf_remove(&str
, i
, len
+ 3); /* \033[<len><func char> */
33 len
= utf8_strwidth(str
.buf
);
39 * Calculate cell width, rows and cols for a table of equal cells, given
40 * table width and how many spaces between cells.
42 static void layout(struct column_data
*data
, int *width
)
47 for (i
= 0; i
< data
->list
->nr
; i
++)
48 if (*width
< data
->len
[i
])
49 *width
= data
->len
[i
];
51 *width
+= data
->opts
.padding
;
53 data
->cols
= (data
->opts
.width
- strlen(data
->opts
.indent
)) / *width
;
57 data
->rows
= DIV_ROUND_UP(data
->list
->nr
, data
->cols
);
60 static void compute_column_width(struct column_data
*data
)
63 for (x
= 0; x
< data
->cols
; x
++) {
64 data
->width
[x
] = XY2LINEAR(data
, x
, 0);
65 for (y
= 0; y
< data
->rows
; y
++) {
66 i
= XY2LINEAR(data
, x
, y
);
67 if (i
< data
->list
->nr
&&
68 data
->len
[data
->width
[x
]] < data
->len
[i
])
75 * Shrink all columns by shortening them one row each time (and adding
76 * more columns along the way). Hopefully the longest cell will be
77 * moved to the next column, column is shrunk so we have more space
78 * for new columns. The process ends when the whole thing no longer
79 * fits in data->total_width.
81 static void shrink_columns(struct column_data
*data
)
83 data
->width
= xrealloc(data
->width
,
84 sizeof(*data
->width
) * data
->cols
);
85 while (data
->rows
> 1) {
86 int x
, total_width
, cols
, rows
;
91 data
->cols
= DIV_ROUND_UP(data
->list
->nr
, data
->rows
);
92 if (data
->cols
!= cols
)
93 data
->width
= xrealloc(data
->width
,
94 sizeof(*data
->width
) * data
->cols
);
95 compute_column_width(data
);
97 total_width
= strlen(data
->opts
.indent
);
98 for (x
= 0; x
< data
->cols
; x
++) {
99 total_width
+= data
->len
[data
->width
[x
]];
100 total_width
+= data
->opts
.padding
;
102 if (total_width
> data
->opts
.width
) {
108 compute_column_width(data
);
111 /* Display without layout when not enabled */
112 static void display_plain(const struct string_list
*list
,
113 const char *indent
, const char *nl
)
117 for (i
= 0; i
< list
->nr
; i
++)
118 printf("%s%s%s", indent
, list
->items
[i
].string
, nl
);
121 /* Print a cell to stdout with all necessary leading/traling space */
122 static int display_cell(struct column_data
*data
, int initial_width
,
123 const char *empty_cell
, int x
, int y
)
127 i
= XY2LINEAR(data
, x
, y
);
128 if (i
>= data
->list
->nr
)
132 if (data
->width
&& data
->len
[data
->width
[x
]] < initial_width
) {
134 * empty_cell has initial_width chars, if real column
135 * is narrower, increase len a bit so we fill less
138 len
+= initial_width
- data
->len
[data
->width
[x
]];
139 len
-= data
->opts
.padding
;
142 if (COL_LAYOUT(data
->colopts
) == COL_COLUMN
)
143 newline
= i
+ data
->rows
>= data
->list
->nr
;
145 newline
= x
== data
->cols
- 1 || i
== data
->list
->nr
- 1;
148 x
== 0 ? data
->opts
.indent
: "",
149 data
->list
->items
[i
].string
,
150 newline
? data
->opts
.nl
: empty_cell
+ len
);
154 /* Display COL_COLUMN or COL_ROW */
155 static void display_table(const struct string_list
*list
,
156 unsigned int colopts
,
157 const struct column_options
*opts
)
159 struct column_data data
;
160 int x
, y
, i
, initial_width
;
163 memset(&data
, 0, sizeof(data
));
165 data
.colopts
= colopts
;
168 data
.len
= xmalloc(sizeof(*data
.len
) * list
->nr
);
169 for (i
= 0; i
< list
->nr
; i
++)
170 data
.len
[i
] = item_length(colopts
, list
->items
[i
].string
);
172 layout(&data
, &initial_width
);
174 if (colopts
& COL_DENSE
)
175 shrink_columns(&data
);
177 empty_cell
= xmalloc(initial_width
+ 1);
178 memset(empty_cell
, ' ', initial_width
);
179 empty_cell
[initial_width
] = '\0';
180 for (y
= 0; y
< data
.rows
; y
++) {
181 for (x
= 0; x
< data
.cols
; x
++)
182 if (display_cell(&data
, initial_width
, empty_cell
, x
, y
))
191 void print_columns(const struct string_list
*list
, unsigned int colopts
,
192 const struct column_options
*opts
)
194 struct column_options nopts
;
198 assert((colopts
& COL_ENABLE_MASK
) != COL_AUTO
);
200 memset(&nopts
, 0, sizeof(nopts
));
201 nopts
.indent
= opts
&& opts
->indent
? opts
->indent
: "";
202 nopts
.nl
= opts
&& opts
->nl
? opts
->nl
: "\n";
203 nopts
.padding
= opts
? opts
->padding
: 1;
204 nopts
.width
= opts
&& opts
->width
? opts
->width
: term_columns() - 1;
205 if (!column_active(colopts
)) {
206 display_plain(list
, "", "\n");
209 switch (COL_LAYOUT(colopts
)) {
211 display_plain(list
, nopts
.indent
, nopts
.nl
);
215 display_table(list
, colopts
, &nopts
);
218 die("BUG: invalid layout mode %d", COL_LAYOUT(colopts
));
222 int finalize_colopts(unsigned int *colopts
, int stdout_is_tty
)
224 if ((*colopts
& COL_ENABLE_MASK
) == COL_AUTO
) {
225 if (stdout_is_tty
< 0)
226 stdout_is_tty
= isatty(1);
227 *colopts
&= ~COL_ENABLE_MASK
;
229 *colopts
|= COL_ENABLED
;
243 static int parse_option(const char *arg
, int len
, unsigned int *colopts
,
246 struct colopt opts
[] = {
247 { "always", COL_ENABLED
, COL_ENABLE_MASK
},
248 { "never", COL_DISABLED
, COL_ENABLE_MASK
},
249 { "auto", COL_AUTO
, COL_ENABLE_MASK
},
250 { "plain", COL_PLAIN
, COL_LAYOUT_MASK
},
251 { "column", COL_COLUMN
, COL_LAYOUT_MASK
},
252 { "row", COL_ROW
, COL_LAYOUT_MASK
},
253 { "dense", COL_DENSE
, 0 },
257 for (i
= 0; i
< ARRAY_SIZE(opts
); i
++) {
258 int set
= 1, arg_len
= len
, name_len
;
259 const char *arg_str
= arg
;
262 if (arg_len
> 2 && !strncmp(arg_str
, "no", 2)) {
269 name_len
= strlen(opts
[i
].name
);
270 if (arg_len
!= name_len
||
271 strncmp(arg_str
, opts
[i
].name
, name_len
))
274 switch (opts
[i
].mask
) {
275 case COL_ENABLE_MASK
:
276 *group_set
|= ENABLE_SET
;
278 case COL_LAYOUT_MASK
:
279 *group_set
|= LAYOUT_SET
;
284 *colopts
= (*colopts
& ~opts
[i
].mask
) | opts
[i
].value
;
287 *colopts
|= opts
[i
].value
;
289 *colopts
&= ~opts
[i
].value
;
294 return error("unsupported option '%s'", arg
);
297 static int parse_config(unsigned int *colopts
, const char *value
)
299 const char *sep
= " ,";
303 int len
= strcspn(value
, sep
);
305 if (parse_option(value
, len
, colopts
, &group_set
))
310 value
+= strspn(value
, sep
);
313 * Setting layout implies "always" if neither always, never
314 * nor auto is specified.
316 * Current value in COL_ENABLE_MASK is disregarded. This means if
317 * you set column.ui = auto and pass --column=row, then "auto"
318 * will become "always".
320 if ((group_set
& LAYOUT_SET
) && !(group_set
& ENABLE_SET
))
321 *colopts
= (*colopts
& ~COL_ENABLE_MASK
) | COL_ENABLED
;
325 static int column_config(const char *var
, const char *value
,
326 const char *key
, unsigned int *colopts
)
329 return config_error_nonbool(var
);
330 if (parse_config(colopts
, value
))
331 return error("invalid column.%s mode %s", key
, value
);
335 int git_column_config(const char *var
, const char *value
,
336 const char *command
, unsigned int *colopts
)
338 const char *it
= skip_prefix(var
, "column.");
342 if (!strcmp(it
, "ui"))
343 return column_config(var
, value
, "ui", colopts
);
345 if (command
&& !strcmp(it
, command
))
346 return column_config(var
, value
, it
, colopts
);
351 int parseopt_column_callback(const struct option
*opt
,
352 const char *arg
, int unset
)
354 unsigned int *colopts
= opt
->value
;
355 *colopts
|= COL_PARSEOPT
;
356 *colopts
&= ~COL_ENABLE_MASK
;
357 if (unset
) /* --no-column == never */
359 /* --column == always unless "arg" states otherwise */
360 *colopts
|= COL_ENABLED
;
362 return parse_config(colopts
, arg
);