summaryrefslogtreecommitdiffstats
path: root/libsmartcols/src/print.c
blob: 88ab5a2f0f91414261b538c3e239fcfece806496 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
 /* print.c - functions to print table
 *
 * Copyright (C) 2010-2014 Karel Zak <kzak@redhat.com>
 * Copyright (C) 2016 Igor Gnatenko <i.gnatenko.brain@gmail.com>
 *
 * This file may be redistributed under the terms of the
 * GNU Lesser General Public License.
 */

/**
 * SECTION: table_print
 * @title: Table print
 * @short_description: output functions
 *
 * Table output API.
 */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <ctype.h>

#include "mbsalign.h"
#include "carefulputc.h"
#include "smartcolsP.h"

/* Fallback for symbols
 *
 * Note that by default library define all the symbols, but in case user does
 * not define all symbols or if we extended the symbols struct then we need
 * fallback to be more robust and backwardly compatible.
 */
#define titlepadding_symbol(tb)	((tb)->symbols->title_padding ? (tb)->symbols->title_padding : " ")
#define branch_symbol(tb)	((tb)->symbols->tree_branch ? (tb)->symbols->tree_branch : "|-")
#define vertical_symbol(tb)	((tb)->symbols->tree_vert ? (tb)->symbols->tree_vert : "| ")
#define right_symbol(tb)	((tb)->symbols->tree_right ? (tb)->symbols->tree_right : "`-")

#define grp_vertical_symbol(tb)	((tb)->symbols->group_vert ? (tb)->symbols->group_vert : "|")
#define grp_horizontal_symbol(tb) ((tb)->symbols->group_horz ? (tb)->symbols->group_horz : "-")
#define grp_m_first_symbol(tb)	((tb)->symbols->group_first_member ? (tb)->symbols->group_first_member : ",->")
#define grp_m_last_symbol(tb)	((tb)->symbols->group_last_member ? (tb)->symbols->group_last_member : "\\->")
#define grp_m_middle_symbol(tb)	((tb)->symbols->group_middle_member ? (tb)->symbols->group_middle_member : "|->")
#define grp_c_middle_symbol(tb)	((tb)->symbols->group_middle_child ? (tb)->symbols->group_middle_child : "|-")
#define grp_c_last_symbol(tb)	((tb)->symbols->group_last_child ? (tb)->symbols->group_last_child : "`-")

#define cellpadding_symbol(tb)  ((tb)->padding_debug ? "." : \
				 ((tb)->symbols->cell_padding ? (tb)->symbols->cell_padding: " "))

#define want_repeat_header(tb)	(!(tb)->header_repeat || (tb)->header_next <= (tb)->termlines_used)

static int is_next_columns_empty(
			struct libscols_table *tb,
			struct libscols_column *cl,
			struct libscols_line *ln)
{
	struct libscols_iter itr;

	if (!tb || !cl)
		return 0;
	if (is_last_column(cl))
		return 1;
	if (!ln)
		return 0;

	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
	scols_table_set_columns_iter(tb, &itr, cl);

	/* skip current column */
	scols_table_next_column(tb, &itr, &cl);

	while (scols_table_next_column(tb, &itr, &cl) == 0) {
		struct libscols_cell *ce;
		const char *data = NULL;

                if (scols_column_is_hidden(cl))
                        continue;
		if (scols_column_is_tree(cl))
			return 0;

		ce = scols_line_get_cell(ln, cl->seqnum);
		if (ce)
			data = scols_cell_get_data(ce);
		if (data && *data)
			return 0;
	}
	return 1;
}

/* returns pointer to the end of used data */
static int tree_ascii_art_to_buffer(struct libscols_table *tb,
				    struct libscols_line *ln,
				    struct ul_buffer *buf)
{
	const char *art;
	int rc;

	assert(ln);
	assert(buf);

	if (!ln->parent)
		return 0;

	rc = tree_ascii_art_to_buffer(tb, ln->parent, buf);
	if (rc)
		return rc;

	if (is_last_child(ln))
		art = "  ";
	else
		art = vertical_symbol(tb);

	return ul_buffer_append_string(buf, art);
}

static int grpset_is_empty(	struct libscols_table *tb,
				size_t idx,
				size_t *rest)
{
	size_t i;

	for (i = idx; i < tb->grpset_size; i++) {
		if (tb->grpset[i] == NULL) {
			if (rest)
				(*rest)++;
		} else
			return 0;
	}
	return 1;
}

static int groups_ascii_art_to_buffer(	struct libscols_table *tb,
				struct libscols_line *ln,
				struct ul_buffer *buf,
				int empty)
{
	int filled = 0;
	size_t i, rest = 0;
	const char *filler = cellpadding_symbol(tb);

	if (!has_groups(tb))
		return 0;

	DBG(LINE, ul_debugobj(ln, "printing groups chart"));

	if (tb->is_dummy_print)
		return 0;		/* allocate grpset[] only */

	for (i = 0; i < tb->grpset_size; i += SCOLS_GRPSET_CHUNKSIZ) {
		struct libscols_group *gr = tb->grpset[i];

		if (!gr) {
			ul_buffer_append_ntimes(buf, SCOLS_GRPSET_CHUNKSIZ, cellpadding_symbol(tb));
			continue;
		}

		/*
		 * Empty cells (multi-line entries, etc.), print vertical symbols only
		 * to show that the group continues.
		 */
		if (empty) {
			switch (gr->state) {
			case SCOLS_GSTATE_FIRST_MEMBER:
			case SCOLS_GSTATE_MIDDLE_MEMBER:
			case SCOLS_GSTATE_CONT_MEMBERS:
				ul_buffer_append_string(buf, grp_vertical_symbol(tb));
				ul_buffer_append_ntimes(buf, 2, filler);
				break;

			case SCOLS_GSTATE_LAST_MEMBER:
			case SCOLS_GSTATE_MIDDLE_CHILD:
			case SCOLS_GSTATE_CONT_CHILDREN:
				ul_buffer_append_string(buf, filler);
				ul_buffer_append_string(buf, grp_vertical_symbol(tb));
				ul_buffer_append_string(buf, filler);
				break;
			case SCOLS_GSTATE_LAST_CHILD:
				ul_buffer_append_ntimes(buf, 3, filler);
				break;
			}
			continue;
		}

		/*
		 * Regular cell
		 */
		switch (gr->state) {
		case SCOLS_GSTATE_FIRST_MEMBER:
			ul_buffer_append_string(buf, grp_m_first_symbol(tb));
			break;
		case SCOLS_GSTATE_MIDDLE_MEMBER:
			ul_buffer_append_string(buf, grp_m_middle_symbol(tb));
			break;
		case SCOLS_GSTATE_LAST_MEMBER:
			ul_buffer_append_string(buf, grp_m_last_symbol(tb));
			break;
		case SCOLS_GSTATE_CONT_MEMBERS:
			ul_buffer_append_string(buf, grp_vertical_symbol(tb));
			ul_buffer_append_ntimes(buf, 2, filler);
			break;
		case SCOLS_GSTATE_MIDDLE_CHILD:
			ul_buffer_append_string(buf, filler);
			ul_buffer_append_string(buf, grp_c_middle_symbol(tb));
			if (grpset_is_empty(tb, i + SCOLS_GRPSET_CHUNKSIZ, &rest)) {
				ul_buffer_append_ntimes(buf, rest+1, grp_horizontal_symbol(tb));
				filled = 1;
			}
			filler = grp_horizontal_symbol(tb);
			break;
		case SCOLS_GSTATE_LAST_CHILD:
			ul_buffer_append_string(buf, cellpadding_symbol(tb));
			ul_buffer_append_string(buf, grp_c_last_symbol(tb));
			if (grpset_is_empty(tb, i + SCOLS_GRPSET_CHUNKSIZ, &rest)) {
				ul_buffer_append_ntimes(buf, rest+1, grp_horizontal_symbol(tb));
				filled = 1;
			}
			filler = grp_horizontal_symbol(tb);
			break;
		case SCOLS_GSTATE_CONT_CHILDREN:
			ul_buffer_append_string(buf, filler);
			ul_buffer_append_string(buf, grp_vertical_symbol(tb));
			ul_buffer_append_string(buf, filler);
			break;
		}

		if (filled)
			break;
	}

	if (!filled)
		ul_buffer_append_string(buf, filler);
	return 0;
}

static void fputs_color_reset(struct libscols_table *tb)
{
	if (tb->cur_color) {
		fputs(UL_COLOR_RESET, tb->out);
		tb->cur_color = NULL;
	}
}

static void fputs_color(struct libscols_table *tb, const char *color)
{
	if (tb->cur_color)
		fputs_color_reset(tb);

	tb->cur_color = color;
	if (color)
		fputs(color, tb->out);
}

static const char *get_cell_color(struct libscols_table *tb,
				struct libscols_column *cl,
				struct libscols_line *ln,
				struct libscols_cell *ce)
{
	const char *color = NULL;

	if (!tb || !tb->colors_wanted || tb->format != SCOLS_FMT_HUMAN)
		return NULL;
	if (ce)
		color = ce->color;
	if (!color && (!ln || !ln->color) && cl)
		color = cl->color;
	return color;
}

/* switch from line color to cell/column color */
static void fputs_color_cell_open(struct libscols_table *tb,
				struct libscols_column *cl,
				struct libscols_line *ln,
				struct libscols_cell *ce)
{
	const char *color = get_cell_color(tb, cl, ln, ce);

	if (color)
		fputs_color(tb, color);
}

/* switch from cell/column color to line color or reset */
static void fputs_color_cell_close(struct libscols_table *tb,
				struct libscols_column *cl,
				struct libscols_line *ln,
				struct libscols_cell *ce)
{
	const char *color = get_cell_color(tb, cl, ln, ce);

	if (color)
		fputs_color(tb, ln ? ln->color : NULL);
}

/* switch to line color */
static void fputs_color_line_open(struct libscols_table *tb,
				struct libscols_line *ln)
{
	if (!tb || !tb->colors_wanted || tb->format != SCOLS_FMT_HUMAN)
		return;
	fputs_color(tb, ln ? ln->color : NULL);
}

/* switch off all colors */
static void fputs_color_line_close(struct libscols_table *tb)
{
	if (!tb || !tb->colors_wanted || tb->format != SCOLS_FMT_HUMAN)
		return;
	fputs_color_reset(tb);
}

/* print padding or ASCII-art instead of data of @cl */
static void print_empty_cell(struct libscols_table *tb,
			  struct libscols_column *cl,
			  struct libscols_line *ln,	/* optional */
			  struct libscols_cell *ce,
			  size_t bufsz)
{
	size_t len_pad = 0;		/* in screen cells as opposed to bytes */

	DBG(COL, ul_debugobj(cl, " printing empty cell"));

	fputs_color_cell_open(tb, cl, ln, ce);

	/* generate tree/group ASCII-art rather than padding
	 */
	if (ln && scols_column_is_tree(cl)) {
		struct ul_buffer art = UL_INIT_BUFFER;
		char *data;

		if (ul_buffer_alloc_data(&art, bufsz) != 0)
			goto done;

		if (cl->is_groups)
			groups_ascii_art_to_buffer(tb, ln, &art, 1);

		tree_ascii_art_to_buffer(tb, ln, &art);

		if (!list_empty(&ln->ln_branch))
			ul_buffer_append_string(&art, vertical_symbol(tb));

		if (scols_table_is_noencoding(tb))
			data = ul_buffer_get_data(&art, NULL, &len_pad);
		else
			data = ul_buffer_get_safe_data(&art, NULL, &len_pad, NULL);

		if (data && len_pad)
			fputs(data, tb->out);
		ul_buffer_free_data(&art);
	}

done:
	/* minout -- don't fill */
	if (scols_table_is_minout(tb) && is_next_columns_empty(tb, cl, ln)) {
		fputs_color_cell_close(tb, cl, ln, ce);
		return;
	}

	/* default -- fill except last column */
	if (!scols_table_is_maxout(tb) && is_last_column(cl)) {
		fputs_color_cell_close(tb, cl, ln, ce);
		return;
	}

	/* fill rest of cell with space */
	for(; len_pad < cl->width; ++len_pad)
		fputs(cellpadding_symbol(tb), tb->out);

	fputs_color_cell_close(tb, cl, ln, ce);

	if (!is_last_column(cl))
		fputs(colsep(tb), tb->out);
}



/* Fill the start of a line with padding (or with tree ascii-art).
 *
 * This is necessary after a long non-truncated column, as this requires the
 * next column to be printed on the next line. For example (see 'DDD'):
 *
 * aaa bbb ccc ddd eee
 * AAA BBB CCCCCCC
 *             DDD EEE
 * ^^^^^^^^^^^^
 *  new line padding
 */
static void print_newline_padding(struct libscols_table *tb,
				  struct libscols_column *cl,
				  struct libscols_line *ln,	/* optional */
				  struct libscols_cell *ce,
				  size_t bufsz)
{
	size_t i;

	assert(tb);
	assert(cl);

	DBG(LINE, ul_debugobj(ln, "printing newline padding"));

	fputs(linesep(tb), tb->out);		/* line break */
	tb->termlines_used++;

	fputs_color_line_open(tb, ln);

	/* fill cells after line break */
	for (i = 0; i <= (size_t) cl->seqnum; i++)
		print_empty_cell(tb, scols_table_get_column(tb, i), ln, ce, bufsz);

	fputs_color_line_close(tb);
}

static int print_pending_data(struct libscols_table *tb, struct ul_buffer *buf)
{
	struct libscols_line *ln;
	struct libscols_column *cl;
	struct libscols_cell *ce;
	char *data;
	size_t i, width = 0, len = 0, bytes = 0;

	scols_table_get_cursor(tb, &ln, &cl, &ce);

	width = cl->width;
	if (!width)
		return -EINVAL;

	DBG(COL, ul_debugobj(cl, "printing pending data"));

	if (scols_table_is_noencoding(tb))
		data = ul_buffer_get_data(buf, &bytes, &len);
	else
		data = ul_buffer_get_safe_data(buf, &bytes, &len, scols_column_get_safechars(cl));

	if (!data)
		return 0;

	/* standard multi-line cell */
	if (len > width && scols_column_is_wrap(cl)
	    && !scols_column_is_customwrap(cl)) {

		len = width;
		bytes = mbs_truncate(data, &len);

		if (bytes != (size_t) -1 && bytes > 0)
			scols_column_move_wrap(cl, mbs_safe_decode_size(data));
	}

	fputs_color_cell_open(tb, cl, ln, ce);
	fputs(data, tb->out);

	/* minout -- don't fill */
	if (scols_table_is_minout(tb) && is_next_columns_empty(tb, cl, ln)) {
		fputs_color_cell_close(tb, cl, ln, ce);
		return 0;
	}

	/* default -- fill except last column */
	if (!scols_table_is_maxout(tb) && is_last_column(cl)) {
		fputs_color_cell_close(tb, cl, ln, ce);
		return 0;
	}

	/* fill rest of cell with space */
	for(i = len; i < width; i++)
		fputs(cellpadding_symbol(tb), tb->out);

	fputs_color_cell_close(tb, cl, ln, ce);

	if (!is_last_column(cl))
		fputs(colsep(tb), tb->out);

	return 0;
}

static void print_json_data(struct libscols_table *tb,
			    struct libscols_column *cl,
			    const char *name,
			    char *data)
{
	switch (cl->json_type) {
	case SCOLS_JSON_STRING:
		/* name: "aaa" */
		ul_jsonwrt_value_s(&tb->json, name, data);
		break;
	case SCOLS_JSON_NUMBER:
	case SCOLS_JSON_FLOAT:
		/* name: 123 */
		ul_jsonwrt_value_raw(&tb->json, name, data);
		break;
	case SCOLS_JSON_BOOLEAN:
	case SCOLS_JSON_BOOLEAN_OPTIONAL:
		/* name: true|false|null */
		if (cl->json_type == SCOLS_JSON_BOOLEAN_OPTIONAL && (!*data || !strcmp(data, "-"))) {
			ul_jsonwrt_value_null(&tb->json, name);
		} else {
			ul_jsonwrt_value_boolean(&tb->json, name,
					!*data ? 0 :
					*data == '0' ? 0 :
					*data == 'N' || *data == 'n' ? 0 : 1);
		}
		break;
	case SCOLS_JSON_ARRAY_STRING:
	case SCOLS_JSON_ARRAY_NUMBER:
		/* name: [ "aaa", "bbb", "ccc" ] */
		ul_jsonwrt_array_open(&tb->json, name);

		if (!scols_column_is_customwrap(cl))
			ul_jsonwrt_value_s(&tb->json, NULL, data);
		else do {
			if (cl->json_type == SCOLS_JSON_ARRAY_STRING)
				ul_jsonwrt_value_s(&tb->json, NULL, data);
			else
				ul_jsonwrt_value_raw(&tb->json, NULL, data);
		} while (scols_column_next_wrap(cl, NULL, &data) == 0);

		ul_jsonwrt_array_close(&tb->json);
		break;
	}
}

static int print_data(struct libscols_table *tb, struct ul_buffer *buf)
{
	struct libscols_line *ln;	/* NULL for header line! */
	struct libscols_column *cl;
	struct libscols_cell *ce;
	size_t len = 0, i, width, bytes;
	char *data = NULL;
	const char *name = NULL;
	int is_last;

	assert(tb);

	scols_table_get_cursor(tb, &ln, &cl, &ce);
	assert(cl);

	if (tb->format != SCOLS_FMT_HUMAN) {
		name = scols_table_is_shellvar(tb) ?
				scols_column_get_name_as_shellvar(cl) :
				scols_column_get_name(cl);

		data = ul_buffer_get_data(buf, NULL, NULL);
		if (!data)
			data = "";
	}

	is_last = is_last_column(cl);

	if (ln && is_last && scols_table_is_json(tb) &&
	    scols_table_is_tree(tb) && has_children(ln))
		/* "children": [] is the real last value */
		is_last = 0;

	switch (tb->format) {
	case SCOLS_FMT_RAW:
		fputs_nonblank(data, tb->out);
		if (!is_last)
			fputs(colsep(tb), tb->out);
		return 0;

	case SCOLS_FMT_EXPORT:
		fputs(name ?: "", tb->out);
		fputc('=', tb->out);
		fputs_quoted(data, tb->out);
		if (!is_last)
			fputs(colsep(tb), tb->out);
		return 0;

	case SCOLS_FMT_JSON:
		print_json_data(tb, cl, name, data);
		return 0;

	case SCOLS_FMT_HUMAN:
		break;		/* continue below */
	}

	/* Encode. Note that 'len' and 'width' are number of glyphs not bytes.
	 */
	if (scols_table_is_noencoding(tb))
		data = ul_buffer_get_data(buf, &bytes, &len);
	else
		data = ul_buffer_get_safe_data(buf, &bytes, &len, scols_column_get_safechars(cl));

	if (!data)
		data = "";
	width = cl->width;

	if (is_last
	    && len < width
	    && !scols_table_is_maxout(tb)
	    && !scols_column_is_right(cl))
		width = len;

	/* truncate data */
	if (len > width && scols_column_is_trunc(cl)) {
		len = width;
		bytes = mbs_truncate(data, &len);	/* updates 'len' */
	}

	/* standard multi-line cell */
	if (len > width && scols_column_is_wrap(cl)
	    && !scols_column_is_customwrap(cl)) {

		len = width;
		bytes = mbs_truncate(data, &len);

		if (bytes != (size_t) -1 && bytes > 0)
			scols_column_move_wrap(cl, mbs_safe_decode_size(data));
	}

	if (bytes == (size_t) -1) {
		bytes = len = 0;
		data = NULL;
	}

	fputs_color_cell_open(tb, cl, ln, ce);

	if (data && *data) {
		if (scols_column_is_right(cl)) {
			for (i = len; i < width; i++)
				fputs(cellpadding_symbol(tb), tb->out);
			len = width;
		}
		fputs(data, tb->out);
	}

	/* minout -- don't fill */
	if (scols_table_is_minout(tb) && is_next_columns_empty(tb, cl, ln)) {
		fputs_color_cell_close(tb, cl, ln, ce);
		return 0;
	}

	/* default -- fill except last column */
	if (!scols_table_is_maxout(tb) && is_last) {
		fputs_color_cell_close(tb, cl, ln, ce);
		return 0;
	}

	/* fill rest of cell with space */
	for(i = len; i < width; i++)
		fputs(cellpadding_symbol(tb), tb->out);

	fputs_color_cell_close(tb, cl, ln, ce);

	if (len > width && !scols_column_is_trunc(cl)) {
		DBG(COL, ul_debugobj(cl, "*** data len=%zu > column width=%zu", len, width));
		print_newline_padding(tb, cl, ln, ce, ul_buffer_get_bufsiz(buf));	/* next column starts on next line */

	} else if (!is_last)
		fputs(colsep(tb), tb->out);		/* columns separator */

	return 0;
}

/*
 * Copy current cell data to buffer. The @cal means "calculation" phase.
 */
int __cursor_to_buffer(struct libscols_table *tb,
		     struct ul_buffer *buf,
		     int cal)
{
	const char *data = NULL;
	size_t datasiz = 0;
	struct libscols_cell *ce;
	struct libscols_line *ln;
	struct libscols_column *cl;
	int rc = 0;

	assert(tb);

	scols_table_get_cursor(tb, &ln, &cl, &ce);

	assert(ln);
	assert(cl);
	assert(buf);
	assert(cl->seqnum <= tb->ncols);

	ul_buffer_reset_data(buf);

	if (!scols_column_is_tree(cl))
		goto notree;

	/*
	 * Group stuff
	 */
	if (!scols_table_is_json(tb) && cl->is_groups)
		rc = groups_ascii_art_to_buffer(tb, ln, buf, 0);

	/*
	 * Tree stuff
	 */
	if (!rc && ln->parent && !scols_table_is_json(tb)) {
		rc = tree_ascii_art_to_buffer(tb, ln->parent, buf);

		if (!rc && is_last_child(ln))
			rc = ul_buffer_append_string(buf, right_symbol(tb));
		else if (!rc)
			rc = ul_buffer_append_string(buf, branch_symbol(tb));
	}

	if (!rc && (ln->parent || cl->is_groups) && !scols_table_is_json(tb))
		ul_buffer_save_pointer(buf, SCOLS_BUFPTR_TREEEND);
notree:
	if (!rc && ce) {
		int do_wrap = scols_column_is_wrap(cl);

		/* Disable multi-line cells for "raw" and "export" formats.
		 * JSON uses data wrapping to generate arrays */
		if (do_wrap && (tb->format == SCOLS_FMT_RAW ||
				tb->format == SCOLS_FMT_EXPORT))
			do_wrap = 0;

		/* Wrapping enabled; append the next chunk if cell data */
		if (do_wrap) {
			char *x = NULL;

			rc = cal ? scols_column_greatest_wrap(cl, ce, &x) :
				   scols_column_next_wrap(cl, ce, &x);
			/* rc: error: <0; nodata: 1; success: 0 */
			if (rc < 0)
				goto done;
			data = x;
			rc = 0;
			if (data && *data)
				datasiz = strlen(data);
			if (data && datasiz)
				rc = ul_buffer_append_data(buf, data, datasiz);

		/* Wrapping disabled, but data maintained by custom wrapping
		 * callback. Try to use data as a string, if not possible,
		 * append all chunks separated by \n (backward compatibility).
		 * */
		} else if (scols_column_is_customwrap(cl)) {
			size_t len;
			int i = 0;
			char *x = NULL;

			data = scols_cell_get_data(ce);
			datasiz = scols_cell_get_datasiz(ce);
			len = data ? strnlen(data, datasiz) : 0;

			if (len && len + 1 == datasiz)
				rc = ul_buffer_append_data(buf, data, datasiz);

			else while (scols_column_next_wrap(cl, ce, &x) == 0) {
				/* non-string data in cell, use a nextchunk callback */
				if (!x)
					continue;
				datasiz = strlen(x);
				if (i)
					rc = ul_buffer_append_data(buf, "\n", 1);
				if (!rc)
					rc = ul_buffer_append_data(buf, x, datasiz);
				i++;
			}

		/* Wrapping disabled; let's use data as a classic string. */
		} else {
			data = scols_cell_get_data(ce);
			datasiz = scols_cell_get_datasiz(ce);

			if (data && *data && !datasiz)
				datasiz = strlen(data);		/* cell content may be updated */

			if (data && datasiz)
				rc = ul_buffer_append_data(buf, data, datasiz);
		}
	}

	/* reset wrapping after greatest chunk calculation */
	if (cal && scols_column_is_wrap(cl))
		scols_column_reset_wrap(cl);

done:
	DBG(COL, ul_debugobj(cl, "__cursor_to_buffer rc=%d", rc));
	return rc;
}

/*
 * Prints data. Data can be printed in more formats (raw, NAME=xxx pairs), and
 * control and non-printable characters can be encoded in the \x?? encoding.
 */
static int print_line(struct libscols_table *tb,
		      struct libscols_line *ln,
		      struct ul_buffer *buf)
{
	int rc = 0, pending = 0;
	struct libscols_column *cl;
	struct libscols_iter itr;

	assert(ln);

	DBG(LINE, ul_debugobj(ln, "     printing line"));

	fputs_color_line_open(tb, ln);

	/* regular line */
	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);

	while (rc == 0 && scols_table_next_column(tb, &itr, &cl) == 0) {
		if (scols_column_is_hidden(cl))
			continue;

		scols_table_set_cursor(tb, ln, cl, scols_line_get_cell(ln, cl->seqnum));
		rc = __cursor_to_buffer(tb, buf, 0);
		if (!rc)
			rc = print_data(tb, buf);
		if (!rc && scols_column_has_pending_wrap(cl))
			pending = 1;
		scols_table_reset_cursor(tb);
	}
	fputs_color_line_close(tb);

	/* extra lines of the multi-line cells */
	while (rc == 0 && pending) {
		DBG(LINE, ul_debugobj(ln, "printing pending data"));
		pending = 0;
		fputs(linesep(tb), tb->out);
		fputs_color_line_open(tb, ln);
		tb->termlines_used++;

		scols_reset_iter(&itr, SCOLS_ITER_FORWARD);

		while (rc == 0 && scols_table_next_column(tb, &itr, &cl) == 0) {
			if (scols_column_is_hidden(cl))
				continue;

			scols_table_set_cursor(tb, ln, cl, scols_line_get_cell(ln, cl->seqnum));
			if (scols_column_has_pending_wrap(cl)) {
				rc = __cursor_to_buffer(tb, buf, 0);
				if (!rc)
					rc = print_pending_data(tb, buf);
				if (!rc && scols_column_has_pending_wrap(cl))
					pending = 1;
				if (!rc && !pending)
					scols_column_reset_wrap(cl);
			} else
				print_empty_cell(tb, cl, ln, NULL, ul_buffer_get_bufsiz(buf));
			scols_table_reset_cursor(tb);
		}
		fputs_color_line_close(tb);
	}

	return 0;
}

int __scols_print_title(struct libscols_table *tb)
{
	int rc;
	mbs_align_t align;
	size_t width, len = 0, bufsz, titlesz;
	char *title = NULL, *buf = NULL;

	assert(tb);

	if (!tb->title.data)
		return 0;

	DBG(TAB, ul_debugobj(tb, "printing title"));

	/* encode data */
	if (tb->no_encode) {
		len = bufsz = strlen(tb->title.data) + 1;
		buf = strdup(tb->title.data);
		if (!buf) {
			rc = -ENOMEM;
			goto done;
		}
	} else {
		bufsz = mbs_safe_encode_size(strlen(tb->title.data)) + 1;
		if (bufsz == 1) {
			DBG(TAB, ul_debugobj(tb, "title is empty string -- ignore"));
			return 0;
		}
		buf = malloc(bufsz);
		if (!buf) {
			rc = -ENOMEM;
			goto done;
		}

		if (!mbs_safe_encode_to_buffer(tb->title.data, &len, buf, NULL) ||
		    !len || len == (size_t) -1) {
			rc = -EINVAL;
			goto done;
		}
	}

	/* truncate and align */
	width = tb->is_term ? tb->termwidth : 80;
	titlesz = width + bufsz;

	title = malloc(titlesz);
	if (!title) {
		rc = -EINVAL;
		goto done;
	}

	switch (scols_cell_get_alignment(&tb->title)) {
	case SCOLS_CELL_FL_RIGHT:
		align = MBS_ALIGN_RIGHT;
		break;
	case SCOLS_CELL_FL_CENTER:
		align = MBS_ALIGN_CENTER;
		break;
	case SCOLS_CELL_FL_LEFT:
	default:
		align = MBS_ALIGN_LEFT;
		/*
		 * Don't print extra blank chars after the title if on left
		 * (that's same as we use for the last column in the table).
		 */
		if (len < width
		    && !scols_table_is_maxout(tb)
		    && isblank(*titlepadding_symbol(tb)))
			width = len;
		break;

	}

	/* copy from buf to title and align to width with title_padding */
	rc = mbsalign_with_padding(buf, title, titlesz,
			&width, align,
			0, (int) *titlepadding_symbol(tb));

	if (rc == -1) {
		rc = -EINVAL;
		goto done;
	}


	if (tb->colors_wanted)
		fputs_color(tb, tb->title.color);

	fputs(title, tb->out);

	if (tb->colors_wanted)
		fputs_color_reset(tb);

	fputc('\n', tb->out);
	rc = 0;
done:
	free(buf);
	free(title);
	DBG(TAB, ul_debugobj(tb, "printing title done [rc=%d]", rc));
	return rc;
}

int __scols_print_header(struct libscols_table *tb, struct ul_buffer *buf)
{
	int rc = 0;
	struct libscols_column *cl;
	struct libscols_iter itr;

	assert(tb);

	if ((tb->header_printed == 1 && tb->header_repeat == 0) ||
	    scols_table_is_noheadings(tb) ||
	    scols_table_is_export(tb) ||
	    scols_table_is_json(tb) ||
	    list_empty(&tb->tb_lines))
		return 0;

	DBG(TAB, ul_debugobj(tb, "printing header"));

	/* set the width according to the size of the data */
	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
	while (rc == 0 && scols_table_next_column(tb, &itr, &cl) == 0) {
		if (scols_column_is_hidden(cl))
			continue;

		ul_buffer_reset_data(buf);
		scols_table_set_cursor(tb, NULL, cl, &cl->header);

		if (cl->is_groups
		    && scols_table_is_tree(tb) && scols_column_is_tree(cl)) {
			size_t i;
			for (i = 0; i < tb->grpset_size + 1; i++) {
				rc = ul_buffer_append_data(buf, " ", 1);
				if (rc)
					break;
			}
		}
		if (!rc)
			rc = ul_buffer_append_string(buf,
					scols_table_is_shellvar(tb) ?
						scols_column_get_name_as_shellvar(cl) :
						scols_column_get_name(cl));
		if (!rc)
			rc = print_data(tb, buf);
		scols_table_reset_cursor(tb);
	}

	if (rc == 0) {
		fputs(linesep(tb), tb->out);
		tb->termlines_used++;
	}

	tb->header_printed = 1;
	tb->header_next = tb->termlines_used + tb->termheight;
	if (tb->header_repeat)
		DBG(TAB, ul_debugobj(tb, "\tnext header: %zu [current=%zu, rc=%d]",
					tb->header_next, tb->termlines_used, rc));
	return rc;
}


int __scols_print_range(struct libscols_table *tb,
			struct ul_buffer *buf,
			struct libscols_iter *itr,
			struct libscols_line *end)
{
	int rc = 0;
	struct libscols_line *ln;

	assert(tb);
	DBG(TAB, ul_debugobj(tb, "printing range"));

	while (rc == 0 && scols_table_next_line(tb, itr, &ln) == 0) {

		int last = scols_iter_is_last(itr);

		if (scols_table_is_json(tb))
			ul_jsonwrt_object_open(&tb->json, NULL);

		rc = print_line(tb, ln, buf);

		if (scols_table_is_json(tb))
			ul_jsonwrt_object_close(&tb->json);
		else if (last == 0 && tb->no_linesep == 0) {
			fputs(linesep(tb), tb->out);
			tb->termlines_used++;
		}

		if (end && ln == end)
			break;

		if (!last && want_repeat_header(tb))
			__scols_print_header(tb, buf);
	}

	return rc;

}

int __scols_print_table(struct libscols_table *tb, struct ul_buffer *buf)
{
	struct libscols_iter itr;

	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
	return __scols_print_range(tb, buf, &itr, NULL);
}

/* scols_walk_tree() callback to print tree line */
static int print_tree_line(struct libscols_table *tb,
			   struct libscols_line *ln,
			   struct libscols_column *cl __attribute__((__unused__)),
			   void *data)
{
	struct ul_buffer *buf = (struct ul_buffer *) data;
	int rc;

	DBG(LINE, ul_debugobj(ln, "   printing tree line"));

	if (scols_table_is_json(tb))
		ul_jsonwrt_object_open(&tb->json, NULL);

	rc = print_line(tb, ln, buf);
	if (rc)
		return rc;

	if (has_children(ln)) {
		if (scols_table_is_json(tb))
			ul_jsonwrt_array_open(&tb->json, "children");
		else {
			/* between parent and child is separator */
			fputs(linesep(tb), tb->out);
			tb->termlines_used++;
		}
	} else {
		int last;

		/* terminate all open last children for JSON */
		if (scols_table_is_json(tb)) {
			do {
				last = (is_child(ln) && is_last_child(ln)) ||
				       (is_tree_root(ln) && is_last_tree_root(tb, ln));

				ul_jsonwrt_object_close(&tb->json);
				if (last && is_child(ln))
					ul_jsonwrt_array_close(&tb->json);
				ln = ln->parent;
			} while(ln && last);

		} else if (tb->no_linesep == 0) {
			int last_in_tree = scols_walk_is_last(tb, ln);

			if (last_in_tree == 0) {
				/* standard output */
				fputs(linesep(tb), tb->out);
				tb->termlines_used++;
			}
		}
	}

	return 0;
}

int __scols_print_tree(struct libscols_table *tb, struct ul_buffer *buf)
{
	assert(tb);
	DBG(TAB, ul_debugobj(tb, "----printing-tree-----"));

	return scols_walk_tree(tb, NULL, print_tree_line, (void *) buf);
}

static size_t strlen_line(struct libscols_line *ln)
{
	size_t i, sz = 0;

	assert(ln);

	for (i = 0; i < ln->ncells; i++) {
		struct libscols_cell *ce = scols_line_get_cell(ln, i);
		const char *data = ce ? scols_cell_get_data(ce) : NULL;

		sz += data ? strlen(data) : 0;
	}

	return sz;
}

void __scols_cleanup_printing(struct libscols_table *tb, struct ul_buffer *buf)
{
	if (!tb)
		return;

	ul_buffer_free_data(buf);

	if (tb->priv_symbols) {
		scols_table_set_symbols(tb, NULL);
		tb->priv_symbols = 0;
	}
}

int __scols_initialize_printing(struct libscols_table *tb, struct ul_buffer *buf)
{
	size_t bufsz, extra_bufsz = 0;
	struct libscols_line *ln;
	struct libscols_iter itr;
	int rc;

	DBG(TAB, ul_debugobj(tb, "initialize printing"));

	if (!tb->symbols) {
		rc = scols_table_set_default_symbols(tb);
		if (rc)
			goto err;
		tb->priv_symbols = 1;
	} else
		tb->priv_symbols = 0;

	if (tb->format == SCOLS_FMT_HUMAN)
		tb->is_term = tb->termforce == SCOLS_TERMFORCE_NEVER  ? 0 :
			      tb->termforce == SCOLS_TERMFORCE_ALWAYS ? 1 :
			      isatty(STDOUT_FILENO);

	if (tb->is_term) {
		size_t width = (size_t) scols_table_get_termwidth(tb);

		if (tb->termreduce > 0 && tb->termreduce < width) {
			width -= tb->termreduce;
			scols_table_set_termwidth(tb, width);
		}
		bufsz = width;
	} else
		bufsz = BUFSIZ;

	if (!tb->is_term || tb->format != SCOLS_FMT_HUMAN || scols_table_is_tree(tb))
		tb->header_repeat = 0;

	/*
	 * Estimate extra space necessary for tree, JSON or another output
	 * decoration.
	 */
	if (scols_table_is_tree(tb))
		extra_bufsz += tb->nlines * strlen(vertical_symbol(tb));

	switch (tb->format) {
	case SCOLS_FMT_RAW:
		extra_bufsz += tb->ncols;			/* separator between columns */
		break;
	case SCOLS_FMT_JSON:
		ul_jsonwrt_init(&tb->json, tb->out, 0);
		extra_bufsz += tb->nlines * 3;		/* indentation */
		/* fallthrough */
	case SCOLS_FMT_EXPORT:
	{
		struct libscols_column *cl;

		scols_reset_iter(&itr, SCOLS_ITER_FORWARD);

		while (scols_table_next_column(tb, &itr, &cl) == 0) {
			if (scols_column_is_hidden(cl))
				continue;

			if (scols_column_get_name(cl))
				extra_bufsz += strlen(scols_column_get_name(cl));	/* data */
			extra_bufsz += 2;						/* separators */
		}
		break;
	}
	case SCOLS_FMT_HUMAN:
		break;
	}

	/*
	 * Enlarge buffer if necessary, the buffer should be large enough to
	 * store line data and tree ascii art (or another decoration).
	 */
	scols_reset_iter(&itr, SCOLS_ITER_FORWARD);
	while (scols_table_next_line(tb, &itr, &ln) == 0) {
		size_t sz;

		sz = strlen_line(ln) + extra_bufsz;
		if (sz > bufsz)
			bufsz = sz;
	}

	/* pre-allocate space for data */
	rc = ul_buffer_alloc_data(buf, bufsz + 1);	/* data + space for \0 */
	if (rc)
		goto err;

	/*
	 * Make sure groups members are in the same orders as the tree
	 */
	if (has_groups(tb) && scols_table_is_tree(tb))
		scols_groups_fix_members_order(tb);

	if (tb->format == SCOLS_FMT_HUMAN) {
		rc = __scols_calculate(tb, buf);
		if (rc != 0)
			goto err;
	}

	return 0;
err:
	__scols_cleanup_printing(tb, buf);
	return rc;
}