15
16
int rows, cols;
17
int *len; /* cell length */
18
+ int *width; /* index to the longest row in column */
19
};
20
21
/* return length of 's' in letters, ANSI escapes stripped */
57
data->rows = DIV_ROUND_UP(data->list->nr, data->cols);
58
}
59
60
+static void compute_column_width(struct column_data *data)
61
+{
62
+ int i, x, y;
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])
69
+ data->width[x] = i;
70
+ }
71
+ }
72
+}
73
+
74
+/*
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.
80
+ */
81
+static void shrink_columns(struct column_data *data)
82
+{
83
+ data->width = xrealloc(data->width,
84
+ sizeof(*data->width) * data->cols);
85
+ while (data->rows > 1) {
86
+ int x, total_width, cols, rows;
87
+ rows = data->rows;
88
+ cols = data->cols;
89
+
90
+ data->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);
96
+
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;
101
+ }
102
+ if (total_width > data->opts.width) {
103
+ data->rows = rows;
104
+ data->cols = cols;
105
+ break;
106
+ }
107
+ }
108
+ compute_column_width(data);
109
+}
110
+
111
/* Display without layout when not enabled */
112
static void display_plain(const struct string_list *list,
113
const char *indent, const char *nl)
127
i = XY2LINEAR(data, x, y);
128
if (i >= data->list->nr)
129
return -1;
130
+
131
len = data->len[i];
132
+ if (data->width && data->len[data->width[x]] < initial_width) {
133
+ /*
134
+ * empty_cell has initial_width chars, if real column
135
+ * is narrower, increase len a bit so we fill less
136
+ * space.
137
+ */
138
+ len += initial_width - data->len[data->width[x]];
139
+ len -= data->opts.padding;
140
+ }
141
+
142
if (COL_LAYOUT(data->colopts) == COL_COLUMN)
143
newline = i + data->rows >= data->list->nr;
144
else
171
172
layout(&data, &initial_width);
173
174
+ if (colopts & COL_DENSE)
175
+ shrink_columns(&data);
176
+
177
empty_cell = xmalloc(initial_width + 1);
178
memset(empty_cell, ' ', initial_width);
179
empty_cell[initial_width] = '\0';
184
}
185
186
free(data.len);
187
+ free(data.width);
188
free(empty_cell);
189
}
190
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 },
254
};
255
int i;
256
257
for (i = 0; i < ARRAY_SIZE(opts); i++) {
190
- int arg_len = len, name_len;
258
+ int set = 1, arg_len = len, name_len;
259
const char *arg_str = arg;
260
261
+ if (!opts[i].mask) {
262
+ if (arg_len > 2 && !strncmp(arg_str, "no", 2)) {
263
+ arg_str += 2;
264
+ arg_len -= 2;
265
+ set = 0;
266
+ }
267
+ }
268
+
269
name_len = strlen(opts[i].name);
270
if (arg_len != name_len ||
271
strncmp(arg_str, opts[i].name, name_len))
282
283
if (opts[i].mask)
284
*colopts = (*colopts & ~opts[i].mask) | opts[i].value;
285
+ else {
286
+ if (set)
287
+ *colopts |= opts[i].value;
288
+ else
289
+ *colopts &= ~opts[i].value;
290
+ }
291
return 0;
292
}
293