Debian Codegraph @

libsmartcols/src/calculate.c

raw
1 #include "smartcolsP.h"
2 #include "mbsalign.h"
3
4 static void dbg_column(struct libscols_table *tb, struct libscols_column *cl)
5 {
6 struct libscols_wstat *st;
7
8 if (scols_column_is_hidden(cl)) {
9 DBG(COL, ul_debugobj(cl, "%s (hidden) ignored", cl->header.data));
10 return;
11 }
12
13 st = &cl->wstat;
14
15 DBG(COL, ul_debugobj(cl, "#%zu %12s: width=%zd "
16 "hint=%d max=%zu min=%zu "
17 "0x04%x [%s%s%s]",
18
19 cl->seqnum, cl->header.data, cl->width,
20 cl->width_hint >= 1.0 ? (int) cl->width_hint :
21 (int) (cl->width_hint * tb->outwidth),
22 st->width_max,
23 st->width_min,
24 cl->flags,
25 cl->flags & SCOLS_FL_TRUNC ? "trunc " : "",
26 scols_column_is_right(cl) ? "right " : "",
27 scols_column_is_noextremes(cl) ? "noextrem " : ""));
28 }
29
30 static void dbg_columns(struct libscols_table *tb)
31 {
32 struct libscols_iter itr;
33 struct libscols_column *cl;
34
35 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
36 while (scols_table_next_column(tb, &itr, &cl) == 0)
37 dbg_column(tb, cl);
38 }
39
40 static int count_cell_width(struct libscols_table *tb,
41 struct libscols_line *ln,
42 struct libscols_column *cl,
43 struct ul_buffer *buf)
44 {
45 size_t len = 0;
46 int rc = 0;
47 struct libscols_cell *ce;
48 char *data;
49
50 ce = scols_line_get_cell(ln, cl->seqnum);
51 scols_table_set_cursor(tb, ln, cl, ce);
52
53 rc = __cursor_to_buffer(tb, buf, 1);
54 if (rc)
55 goto done;
56
57 data = ul_buffer_get_data(buf, NULL, NULL);
58 if (data) {
59 len = scols_table_is_noencoding(tb) ?
60 mbs_width(data) :
61 mbs_safe_width(data);
62
63 if (len == (size_t) -1) /* ignore broken multibyte strings */
64 len = 0;
65 }
66
67 if (scols_column_is_tree(cl)) {
68 size_t treewidth = ul_buffer_get_safe_pointer_width(buf, SCOLS_BUFPTR_TREEEND);
69 cl->width_treeart = max(cl->width_treeart, treewidth);
70 }
71
72 ce->width = len;
73 cl->wstat.width_max = max(len, cl->wstat.width_max);
74 done:
75 scols_table_reset_cursor(tb);
76 return rc;
77 }
78
79 static int walk_count_cell_width(struct libscols_table *tb,
80 struct libscols_line *ln,
81 struct libscols_column *cl,
82 void *data)
83 {
84 return count_cell_width(tb, ln, cl, (struct ul_buffer *) data);
85 }
86
87 static double sqrtroot(double num)
88 {
89 double tmp = 0, sq = num / 2;
90
91 while (sq != tmp){
92 tmp = sq;
93 sq = (num / tmp + tmp) / 2;
94 }
95 return sq;
96 }
97
98 static void count_column_deviation(struct libscols_table *tb, struct libscols_column *cl)
99 {
100 struct libscols_wstat *st;
101 struct libscols_iter itr;
102 struct libscols_line *ln;
103 struct libscols_cell *ce;
104 size_t sum = 0, n = 0, extra = 0;
105
106 st = &cl->wstat;
107
108 if (scols_column_is_tree(cl) && has_groups(tb))
109 extra = tb->grpset_size + 1;
110
111 /* count average */
112 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
113 while (scols_table_next_line(tb, &itr, &ln) == 0) {
114 ce = scols_line_get_cell(ln, cl->seqnum);
115
116 n++;
117 sum += ce->width + extra;
118 }
119
120 if (n)
121 st->width_avg = (double) sum / (double) n;
122
123 /* count deviation */
124 if (n > 1) {
125 double variance;
126
127 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
128 while (scols_table_next_line(tb, &itr, &ln) == 0) {
129 double diff;
130 ce = scols_line_get_cell(ln, cl->seqnum);
131
132 diff = (double) ce->width - st->width_avg;
133 st->width_sqr_sum += diff * diff; /* aka pow(x, 2) */
134 }
135
136 variance = st->width_sqr_sum / (double) (n - 1);
137 st->width_deviation = sqrtroot(variance);
138 }
139
140 DBG(COL, ul_debugobj(cl, "%15s avg=%g, deviation=%g",
141 cl->header.data,
142 st->width_avg,
143 st->width_deviation));
144 }
145
146 /*
147 * This function counts column width.
148 */
149 static int count_column_width(struct libscols_table *tb,
150 struct libscols_column *cl,
151 struct ul_buffer *buf)
152 {
153 int rc = 0, no_header = 0;
154 const char *data;
155 struct libscols_wstat *st;
156 struct libscols_iter itr;
157 struct libscols_line *ln;
158
159 assert(tb);
160 assert(cl);
161
162 st = &cl->wstat;
163
164 cl->width = 0;
165 memset(st, 0, sizeof(struct libscols_wstat));
166
167 /* set minimal width according to width_hint */
168 if (cl->width_hint < 1 && scols_table_is_maxout(tb) && tb->is_term) {
169 st->width_min = (size_t) (cl->width_hint * tb->outwidth);
170 if (st->width_min && !is_last_column(cl))
171 st->width_min--;
172 }
173
174 /* set minimal width according to header width */
175 if (!scols_table_is_noheadings(tb) &&
176 (data = scols_cell_get_data(&cl->header))) {
177
178 size_t len = scols_table_is_noencoding(tb) ?
179 mbs_width(data) : mbs_safe_width(data);
180
181 st->width_min = max(st->width_min, len);
182 } else
183 no_header = 1;
184
185 if (!st->width_min)
186 st->width_min = 1;
187
188 /* count width according to cells data */
189 if (scols_table_is_tree(tb)) {
190 /* Count width for tree */
191 rc = scols_walk_tree(tb, cl, walk_count_cell_width, (void *) buf);
192 if (rc)
193 goto done;
194 } else {
195 /* Count width for list */
196 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
197 while (scols_table_next_line(tb, &itr, &ln) == 0) {
198 rc = count_cell_width(tb, ln, cl, buf);
199 if (rc)
200 goto done;
201 }
202 }
203
204 if (scols_column_is_tree(cl) && has_groups(tb)) {
205 /* We don't fill buffer with groups tree ascii art during width
206 * calculation. The print function only enlarge grpset[] and we
207 * calculate final width from grpset_size.
208 */
209 size_t gprwidth = tb->grpset_size + 1;
210 st->width_treeart += gprwidth;
211 st->width_max += gprwidth;
212 }
213
214 /* this is default, may be later reduced */
215 cl->width = st->width_max;
216
217 /* enlarge to minimal width (usually header width) */
218 if (cl->width <= st->width_min && !scols_column_is_strict_width(cl))
219 cl->width = st->width_min;
220
221 /* use absolute width if specified (usually "strictwidth" columns) */
222 else if (cl->width_hint >= 1
223 && cl->width < (size_t) cl->width_hint
224 && st->width_min < (size_t) cl->width_hint)
225
226 cl->width = (size_t) cl->width_hint;
227
228
229 /* Column without header and data, set minimal size to zero (default is 1) */
230 if (st->width_max == 0 && no_header && st->width_min == 1 && cl->width <= 1)
231 cl->width = st->width_min = 0;
232
233 done:
234 ON_DBG(COL, dbg_column(tb, cl));
235 return rc;
236 }
237
238 static int cmp_deviation(struct list_head *a, struct list_head *b,
239 void *data __attribute__((__unused__)))
240 {
241 struct libscols_column *ca = list_entry(a, struct libscols_column, cl_columns);
242 struct libscols_column *cb = list_entry(b, struct libscols_column, cl_columns);
243
244 double xa = ca->wstat.width_avg + (3.0 * ca->wstat.width_deviation);
245 double xb = cb->wstat.width_avg + (3.0 * cb->wstat.width_deviation);
246
247 return cmp_numbers(xa, xb);
248 }
249
250 static int cmp_seqnum(struct list_head *a, struct list_head *b,
251 void *data __attribute__((__unused__)))
252 {
253 struct libscols_column *ca = list_entry(a, struct libscols_column, cl_columns);
254 struct libscols_column *cb = list_entry(b, struct libscols_column, cl_columns);
255
256 return cmp_numbers(ca->seqnum, cb->seqnum);
257 }
258
259 static inline void sort_columns(struct libscols_table *tb,
260 int (*cmp)(struct list_head *, struct list_head *, void *))
261 {
262 list_sort(&tb->tb_columns, cmp, NULL);
263 }
264
265
266 /* 68%–95%–99% rule (aka empirical rule) defines relation ship between
267 * mean (avg) and and standard deviation.
268 *
269 * avg + (n * deviation)
270 *
271 * n=1 : covers 68% of data
272 * n=2 : covers 95% of data
273 * n=3 : covers 99.7% of data
274 *
275 */
276 static void reduce_to_68(struct libscols_column *cl, size_t wanted)
277 {
278 struct libscols_wstat *st = &cl->wstat;
279 size_t new;
280
281 if (st->width_deviation < 1.0)
282 return;
283
284 new = (size_t) (st->width_avg + st->width_deviation);
285
286 if (new < st->width_min)
287 new = st->width_min;
288 else if (new > st->width_max)
289 new = st->width_max;
290
291 if (new >= cl->width)
292 return;
293
294 if (cl->width - new > wanted)
295 cl->width -= wanted;
296 else
297 cl->width = new;
298 }
299
300 static int reduce_column(struct libscols_table *tb,
301 struct libscols_column *cl,
302 size_t *width,
303 int stage,
304 int nth)
305 {
306 struct libscols_wstat *st = &cl->wstat;
307 size_t wanted, org_width, reduce = 1;
308 int is_trunc = 0;
309
310 if (stage > 6)
311 return -1;
312 if (tb->outwidth >= *width)
313 return 1;
314 /* ignore hidden columns */
315 if (scols_column_is_hidden(cl))
316 return 0;
317 /* never truncate if already minimal width */
318 if (cl->width == cl->wstat.width_min)
319 return 0;
320 /* nothing to truncate */
321 if (cl->width == 0)
322 return 0;
323 /* never truncate the tree */
324 if (scols_column_is_tree(cl) && *width <= cl->width_treeart)
325 return 0;
326
327 org_width = cl->width;
328 wanted = *width - tb->outwidth;
329
330 is_trunc = scols_column_is_trunc(cl)
331 || (scols_column_is_wrap(cl) && !scols_column_is_customwrap(cl));
332
333 switch (stage) {
334 case 0:
335 /* reduce 1st column if with trunc or extreme flag (the
336 * columns are sorted by deviation, so 1st is the worst) */
337 if (!is_trunc && !scols_column_is_noextremes(cl))
338 break;
339 if (nth != 0)
340 break;
341 reduce_to_68(cl, wanted);
342 break;
343
344 case 1:
345 /* reduce extreme columns with large width deviation */
346 if (st->width_deviation < st->width_avg / 2.0)
347 break;
348 FALLTHROUGH;
349 case 2:
350 /* reduce extreme columns */
351 if (!scols_column_is_noextremes(cl))
352 break;
353 reduce_to_68(cl, wanted);
354 break;
355
356 case 3:
357 /* reduce columns with trunc flag and relative whint and large width deviation */
358 if (st->width_deviation < st->width_avg / 2.0)
359 break;
360 FALLTHROUGH;
361 case 4:
362 /* reduce columns with trunc flag and relative whint */
363 if (!is_trunc)
364 break;
365 if (cl->width_hint <= 0 || cl->width_hint >= 1)
366 break;
367 if (cl->width < (size_t) (cl->width_hint * tb->outwidth))
368 break;
369 reduce_to_68(cl, wanted);
370 break;
371
372 case 5:
373 /* reduce all columns with trunc flag large width deviation */
374 if (st->width_deviation < st->width_avg / 2.2)
375 break;
376 FALLTHROUGH;
377 case 6:
378 /* reduce all columns with trunc flag */
379 if (!is_trunc && !scols_column_is_noextremes(cl))
380 break;
381 if (nth == 0)
382 /* columns are reduced in "bad first" way, be more
383 * aggressive for the the worst column */
384 reduce = wanted < 3 ? wanted : 3;
385
386 if (cl->width < reduce)
387 reduce = cl->width;
388
389 if (cl->width - reduce > st->width_min)
390 cl->width -= reduce;
391 else
392 cl->width = st->width_min;
393 break;
394 default:
395 return -1; /* no more stages */
396 }
397
398 /* hide zero width columns */
399 if (cl->width == 0)
400 cl->flags |= SCOLS_FL_HIDDEN;
401
402 if (cl->width != org_width)
403 DBG(COL, ul_debugobj(cl, " [%02zd] %s reduced %zu-->%zu",
404 cl->seqnum,
405 cl->header.data, org_width, cl->width));
406
407 *width -= org_width - cl->width;
408 return 0;
409 }
410
411 /*
412 * This is core of the scols_* voodoo...
413 */
414 int __scols_calculate(struct libscols_table *tb, struct ul_buffer *buf)
415 {
416 struct libscols_column *cl, *last_cl;
417 struct libscols_iter itr;
418 size_t width = 0, width_min = 0; /* output width */
419 int stage = 0, rc = 0;
420 int ignore_extremes = 0, group_ncolumns = 0;
421 size_t colsepsz;
422 int sorted = 0;
423
424 DBG(TAB, ul_debugobj(tb, "-----calculate-(termwidth=%zu, outwidth=%zu)-----", tb->termwidth, tb->outwidth));
425 tb->is_dummy_print = 1;
426 colsepsz = scols_table_is_noencoding(tb) ?
427 mbs_width(colsep(tb)) :
428 mbs_safe_width(colsep(tb));
429
430 if (has_groups(tb))
431 group_ncolumns = 1;
432
433 /* set basic columns width
434 */
435 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
436 while (scols_table_next_column(tb, &itr, &cl) == 0) {
437 int is_last;
438
439 memset(&cl->wstat, 0, sizeof(cl->wstat));
440 cl->width = 0;
441 cl->width_treeart = 0;
442 scols_column_reset_wrap(cl);
443
444 if (scols_column_is_hidden(cl))
445 continue;
446
447 /* we print groups chart only for the for the first tree column */
448 if (scols_column_is_tree(cl) && group_ncolumns == 1) {
449 cl->is_groups = 1;
450 group_ncolumns++;
451 }
452
453 rc = count_column_width(tb, cl, buf);
454 if (rc)
455 goto done;
456
457 is_last = is_last_column(cl);
458
459 width += cl->width + (is_last ? 0 : colsepsz); /* separator for non-last column */
460 width_min += cl->wstat.width_min + (is_last ? 0 : colsepsz);
461 }
462
463 if (!tb->is_term) {
464 DBG(TAB, ul_debugobj(tb, " non-terminal output"));
465 goto done;
466 }
467
468 /* be paranoid */
469 if (width_min > tb->outwidth && scols_table_is_maxout(tb)) {
470 DBG(TAB, ul_debugobj(tb, " min width larger than terminal! [width=%zu, term=%zu, avail=%zu]",
471 width_min, tb->termwidth, tb->outwidth));
472 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
473 while (width_min > tb->outwidth
474 && scols_table_next_column(tb, &itr, &cl) == 0) {
475
476 if (scols_column_is_hidden(cl))
477 continue;
478 width_min--;
479 cl->wstat.width_min--;
480 }
481 DBG(TAB, ul_debugobj(tb, " min width reduced to %zu", width_min));
482 }
483
484 /* calculate statistics */
485 scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
486 while (scols_table_next_column(tb, &itr, &cl) == 0) {
487
488 count_column_deviation(tb, cl);
489
490 if (scols_column_is_noextremes(cl))
491 ignore_extremes++;
492 }
493
494 /* remember last column before we sort columns */
495 last_cl = list_entry(tb->tb_columns.prev, struct libscols_column, cl_columns);
496
497 /* reduce columns width */
498 while (width > tb->outwidth) {
499 size_t org_width = width;
500 int xrc = 0, n = 0;
501
502 if (!sorted) {
503 DBG(TAB, ul_debugobj(tb, "sorting by deviation"));
504 sort_columns(tb, cmp_deviation);
505 ON_DBG(TAB, dbg_columns(tb));
506 sorted = 1;
507 }
508
509 DBG(TAB, ul_debugobj(tb, "#%d reduce stage (width=%zu, avail=%zu)",
510 stage, width, tb->outwidth));
511
512 scols_reset_iter(&itr, SCOLS_ITER_BACKWARD);
513
514 while (width > tb->outwidth
515 && xrc == 0
516 && scols_table_next_column(tb, &itr, &cl) == 0) {
517 xrc = reduce_column(tb, cl, &width, stage, n++);
518 }
519
520 if (xrc != 0)
521 break;
522 if (org_width == width)
523 stage++;
524 }
525
526 /* enlarge */
527 if (width < tb->outwidth) {
528 DBG(TAB, ul_debugobj(tb, " enlarge (extreme, available %zu)",
529 tb->outwidth - width));
530 if (ignore_extremes) {
531 if (!sorted) {
532 sort_columns(tb, cmp_deviation);
533 sorted = 1;
534 }
535
536 scols_reset_iter(&itr, SCOLS_ITER_BACKWARD);
537 while (scols_table_next_column(tb, &itr, &cl) == 0) {
538 size_t add;
539
540 if (!scols_column_is_noextremes(cl) || scols_column_is_hidden(cl))
541 continue;
542 if (cl->wstat.width_min == 0 && cl->width == 0)
543 continue;
544 if (cl->width >= cl->wstat.width_max)
545 continue;
546
547 add = tb->outwidth - width;
548 if (add && cl->wstat.width_max
549 && cl->width + add > cl->wstat.width_max)
550 add = cl->wstat.width_max - cl->width;
551
552 if (!add)
553 continue;
554 DBG(COL, ul_debugobj(cl, " add +%zd (%s)",
555 add, cl->header.data));
556 cl->width += add;
557 width += add;
558
559 if (width == tb->outwidth)
560 break;
561 }
562 }
563
564 if (width < tb->outwidth) {
565 DBG(TAB, ul_debugobj(tb, " enlarge (absolute, available %zu)",
566 tb->outwidth - width));
567
568 /* try enlarging all columns with absolute hint */
569 while (width < tb->outwidth) {
570 int changed = 0;
571
572 scols_reset_iter(&itr, SCOLS_ITER_BACKWARD);
573 while (scols_table_next_column(tb, &itr, &cl) == 0) {
574 if (scols_column_is_hidden(cl))
575 continue;
576 if (cl->width_hint < 1.0)
577 continue;
578 if (cl->width >= (size_t) cl->width_hint)
579 continue;
580 DBG(COL, ul_debugobj(cl, " add +1 (%s)",
581 cl->header.data));
582 cl->width++;
583 width++;
584 changed++;
585 if (width == tb->outwidth)
586 break;
587 }
588 if (!changed)
589 break;
590 }
591
592 }
593 if (width < tb->outwidth && scols_table_is_maxout(tb)) {
594 DBG(TAB, ul_debugobj(tb, " enlarge (max-out, available %zu)",
595 tb->outwidth - width));
596
597 /* try enlarging all columns */
598 while (width < tb->outwidth) {
599 scols_reset_iter(&itr, SCOLS_ITER_BACKWARD);
600 while (scols_table_next_column(tb, &itr, &cl) == 0) {
601 if (scols_column_is_hidden(cl))
602 continue;
603 DBG(COL, ul_debugobj(cl, " add +1 (%s)",
604 cl->header.data));
605 cl->width++;
606 width++;
607 if (width == tb->outwidth)
608 break;
609 }
610 }
611
612 } else if (width < tb->outwidth) {
613 /* enlarge the last column */
614 DBG(TAB, ul_debugobj(tb, " enlarge (last column, available %zu)",
615 tb->outwidth - width));
616
617 if (!scols_column_is_right(last_cl)) {
618 DBG(COL, ul_debugobj(last_cl, " add +%zu (%s)",
619 tb->outwidth - width,
620 last_cl->header.data));
621 last_cl->width += tb->outwidth - width;
622 width = tb->outwidth;
623 }
624 }
625 }
626
627
628 if (sorted) {
629 sort_columns(tb, cmp_seqnum);
630 sorted = 0;
631 }
632
633 /* ignore last column(s) or force last column to be truncated if
634 * nowrap mode enabled */
635 if (tb->no_wrap && width > tb->outwidth) {
636 scols_reset_iter(&itr, SCOLS_ITER_BACKWARD);
637 while (scols_table_next_column(tb, &itr, &cl) == 0) {
638
639 if (scols_column_is_hidden(cl))
640 continue;
641 if (width <= tb->outwidth)
642 break;
643 if (width - cl->width < tb->outwidth) {
644 size_t r = width - tb->outwidth;
645
646 cl->flags |= SCOLS_FL_TRUNC;
647 cl->width -= r;
648 width -= r;
649 } else {
650 cl->flags |= SCOLS_FL_HIDDEN;
651 width -= cl->width + colsepsz;
652 }
653 }
654 }
655 done:
656 if (sorted)
657 sort_columns(tb, cmp_seqnum);
658
659 tb->is_dummy_print = 0;
660 DBG(TAB, ul_debugobj(tb, "-----final width: %zu (rc=%d)-----", width, rc));
661 ON_DBG(TAB, dbg_columns(tb));
662
663 return rc;
664 }