(root)/
util-linux-2.39/
libsmartcols/
samples/
grouping-overlay.c
       1  /*
       2   * Copyright (C) 2018 Karel Zak <kzak@redhat.com>
       3   *
       4   * This file may be redistributed under the terms of the
       5   * GNU Lesser General Public License.
       6   */
       7  #include <stdlib.h>
       8  #include <unistd.h>
       9  #include <string.h>
      10  #include <errno.h>
      11  #include <sys/types.h>
      12  #include <sys/stat.h>
      13  #include <dirent.h>
      14  #include <getopt.h>
      15  
      16  #include "c.h"
      17  #include "nls.h"
      18  #include "strutils.h"
      19  #include "xalloc.h"
      20  
      21  #include "libsmartcols.h"
      22  
      23  
      24  enum { COL_NAME, COL_DATA };
      25  
      26  /* add columns to the @tb */
      27  static void setup_columns(struct libscols_table *tb)
      28  {
      29  	struct libscols_column *cl;
      30  
      31  	if (!scols_table_new_column(tb, "NAME", 0, SCOLS_FL_TREE))
      32  		goto fail;
      33  	cl = scols_table_new_column(tb, "DATA", 0, SCOLS_FL_WRAP);
      34  	if (!cl)
      35  		goto fail;
      36  	scols_column_set_wrapfunc(cl, scols_wrapnl_chunksize,
      37  			scols_wrapnl_nextchunk, NULL);
      38  	scols_column_set_safechars(cl, "\n");
      39  	return;
      40  fail:
      41  	scols_unref_table(tb);
      42  	err(EXIT_FAILURE, "failed to create output columns");
      43  }
      44  
      45  static struct libscols_line *add_line(struct libscols_table *tb, struct libscols_line *parent, const char *name, const char *data)
      46  {
      47  	struct libscols_line *ln = scols_table_new_line(tb, parent);
      48  	if (!ln)
      49  		err(EXIT_FAILURE, "failed to create output line");
      50  
      51  	if (scols_line_set_data(ln, COL_NAME, name))
      52  		goto fail;
      53  	if (scols_line_set_data(ln, COL_DATA, data))
      54  		goto fail;
      55  	return ln;
      56  fail:
      57  	scols_unref_table(tb);
      58  	err(EXIT_FAILURE, "failed to create output line");
      59  }
      60  
      61  int main(int argc, char *argv[])
      62  {
      63  	struct libscols_table *tb;
      64  	struct libscols_line *ln;		/* any line */
      65  	struct libscols_line *g1, *g2, *g3;	/* groups */
      66  	struct libscols_line *p1;		/* parents */
      67  	int c;
      68  
      69  	static const struct option longopts[] = {
      70  		{ "maxout", 0, NULL, 'm' },
      71  		{ "width",  1, NULL, 'w' },
      72  		{ "help",   1, NULL, 'h' },
      73  
      74  		{ NULL, 0, NULL, 0 },
      75  	};
      76  
      77  	setlocale(LC_ALL, "");	/* just to have enable UTF8 chars */
      78  
      79  	scols_init_debug(0);
      80  
      81  	tb = scols_new_table();
      82  	if (!tb)
      83  		err(EXIT_FAILURE, "failed to create output table");
      84  
      85  	while((c = getopt_long(argc, argv, "hmw:", longopts, NULL)) != -1) {
      86  		switch(c) {
      87  		case 'h':
      88  			printf("%s [--help | --maxout | --width <num>]\n", program_invocation_short_name);
      89  			break;
      90  		case 'm':
      91  			scols_table_enable_maxout(tb, TRUE);
      92  			break;
      93  		case 'w':
      94  			scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS);
      95  			scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width"));
      96  			break;
      97  		}
      98  	}
      99  
     100  	scols_table_enable_colors(tb, isatty(STDOUT_FILENO));
     101  	setup_columns(tb);
     102  
     103  	add_line(tb, NULL, "Alone", "bla bla bla");
     104  
     105  	p1 = add_line(tb, NULL, "A", "bla bla\nbla");
     106  	     add_line(tb, p1,   "A:B", "bla bla\nbla");
     107  	     add_line(tb, p1,   "A:C", "bla bla\nbla");
     108  
     109  	g1 = add_line(tb, NULL, "B", "bla bla\nbla");
     110  
     111  	g2 = add_line(tb, NULL, "C", "bla bla\nbla");
     112  	ln = add_line(tb, NULL, "D", "bla bla\nbla");
     113  	scols_table_group_lines(tb, g2, ln, 0);
     114  
     115  	ln = add_line(tb, NULL, "G2:A", "alb alb\nalb");
     116  	scols_line_link_group(ln, g2, 0);
     117  
     118  	ln = add_line(tb, NULL, "E", "bla bla\nbla");
     119  	scols_table_group_lines(tb, g1, ln, 0);
     120  
     121  
     122  	ln = add_line(tb, NULL, "G1:A", "alb alb alb");
     123  	scols_line_link_group(ln, g1, 0);
     124  
     125  	add_line(tb, NULL, "G", "bla bla bla");
     126  
     127  	g3 = ln = add_line(tb, NULL, "G1:B", "alb alb\nalb");
     128  	scols_line_link_group(ln, g1, 0);
     129  
     130  	ln = add_line(tb, NULL, "F", "bla bla bla");
     131  	scols_table_group_lines(tb, g3, ln, 0);
     132  
     133  	ln = add_line(tb, NULL, "G3:A", "alb alb alb");
     134  	scols_line_link_group(ln, g3, 0);
     135  
     136  	add_line(tb, NULL, "foo", "bla bla bla");
     137  	add_line(tb, NULL, "bar", "bla bla bla");
     138  
     139  	scols_print_table(tb);
     140  
     141  	scols_unref_table(tb);
     142  	return EXIT_SUCCESS;
     143  }