| 1 |
/* |
| 2 |
* Copyright (C) 2010-2014 Karel Zak <[email protected]> |
| 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 |
|
| 37 |
scols_column_set_wrapfunc(cl, scols_wrapnl_chunksize, |
| 38 |
scols_wrapnl_nextchunk, |
| 39 |
NULL); |
| 40 |
scols_column_set_safechars(cl, "\n"); |
| 41 |
|
| 42 |
return; |
| 43 |
fail: |
| 44 |
scols_unref_table(tb); |
| 45 |
err(EXIT_FAILURE, "failed to create output columns"); |
| 46 |
} |
| 47 |
|
| 48 |
static struct libscols_line *add_line(struct libscols_table *tb, struct libscols_line *parent, const char *name, const char *data) |
| 49 |
{ |
| 50 |
struct libscols_line *ln = scols_table_new_line(tb, parent); |
| 51 |
if (!ln) |
| 52 |
err(EXIT_FAILURE, "failed to create output line"); |
| 53 |
|
| 54 |
if (scols_line_set_data(ln, COL_NAME, name)) |
| 55 |
goto fail; |
| 56 |
if (scols_line_set_data(ln, COL_DATA, data)) |
| 57 |
goto fail; |
| 58 |
return ln; |
| 59 |
fail: |
| 60 |
scols_unref_table(tb); |
| 61 |
err(EXIT_FAILURE, "failed to create output line"); |
| 62 |
} |
| 63 |
|
| 64 |
int main(int argc, char *argv[]) |
| 65 |
{ |
| 66 |
struct libscols_table *tb; |
| 67 |
struct libscols_line *ln; /* any line */ |
| 68 |
struct libscols_line *g1; /* groups */ |
| 69 |
struct libscols_line *p1, *p2; /* parents */ |
| 70 |
int c; |
| 71 |
|
| 72 |
static const struct option longopts[] = { |
| 73 |
{ "maxout", 0, NULL, 'm' }, |
| 74 |
{ "width", 1, NULL, 'w' }, |
| 75 |
{ "help", 1, NULL, 'h' }, |
| 76 |
|
| 77 |
{ NULL, 0, NULL, 0 }, |
| 78 |
}; |
| 79 |
|
| 80 |
setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */ |
| 81 |
|
| 82 |
scols_init_debug(0); |
| 83 |
|
| 84 |
tb = scols_new_table(); |
| 85 |
if (!tb) |
| 86 |
err(EXIT_FAILURE, "failed to create output table"); |
| 87 |
|
| 88 |
while((c = getopt_long(argc, argv, "hmw:", longopts, NULL)) != -1) { |
| 89 |
switch(c) { |
| 90 |
case 'h': |
| 91 |
printf("%s [--help | --maxout | --width <num>]\n", program_invocation_short_name); |
| 92 |
break; |
| 93 |
case 'm': |
| 94 |
scols_table_enable_maxout(tb, TRUE); |
| 95 |
break; |
| 96 |
case 'w': |
| 97 |
scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS); |
| 98 |
scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width")); |
| 99 |
break; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
scols_table_enable_colors(tb, isatty(STDOUT_FILENO)); |
| 104 |
setup_columns(tb); |
| 105 |
|
| 106 |
add_line(tb, NULL, "Alone", "bla bla bla"); |
| 107 |
|
| 108 |
p1 = add_line(tb, NULL, "A", "bla bla bla"); |
| 109 |
add_line(tb, p1, "A:B", "bla bla bla"); |
| 110 |
add_line(tb, p1, "A:C", "bla bla bla"); |
| 111 |
|
| 112 |
g1 = add_line(tb, NULL, "B", "bla bla bla"); |
| 113 |
add_line(tb, NULL, "C", "bla\nfoo"); |
| 114 |
p1 = add_line(tb, NULL, "D", "bla bla\nbar"); |
| 115 |
|
| 116 |
p2 = add_line(tb, p1, "D:A", "bla bla bla"); |
| 117 |
|
| 118 |
ln = add_line(tb, p2, "D:A:A", "bla\nbla\nbla"); |
| 119 |
scols_table_group_lines(tb, g1, ln, 0); |
| 120 |
|
| 121 |
add_line(tb, p1, "D:B", "bla bla bla"); |
| 122 |
add_line(tb, p1, "D:C", "bla\nother bla"); |
| 123 |
add_line(tb, p1, "D:D", "bla bla bla"); |
| 124 |
|
| 125 |
ln = add_line(tb, NULL, "E", "bla bla bla"); |
| 126 |
scols_table_group_lines(tb, g1, ln, 0); |
| 127 |
|
| 128 |
p1 = ln; |
| 129 |
add_line(tb, p1, "E:A", "bla bla bla"); |
| 130 |
add_line(tb, p1, "E:B", "bla bla bla"); |
| 131 |
add_line(tb, p1, "E:C", "bla bla bla"); |
| 132 |
|
| 133 |
add_line(tb, NULL, "F", "bla bla bla"); |
| 134 |
|
| 135 |
ln = add_line(tb, NULL, "G1:A", "alb alb alb"); |
| 136 |
scols_line_link_group(ln, g1, 0); |
| 137 |
|
| 138 |
p1 = ln; |
| 139 |
add_line(tb, p1, "G1:A:A", "bla\nbla bla"); |
| 140 |
add_line(tb, p1, "G1:A:B", "bla bla bla"); |
| 141 |
add_line(tb, p1, "G1:A:C", "bla bla bla"); |
| 142 |
|
| 143 |
add_line(tb, NULL, "G", "bla bla bla"); |
| 144 |
|
| 145 |
ln = add_line(tb, NULL, "G1:B", "alb alb\nalb"); |
| 146 |
scols_line_link_group(ln, g1, 0); |
| 147 |
|
| 148 |
add_line(tb, NULL, "foo", "bla bla bla"); |
| 149 |
add_line(tb, NULL, "bar", "bla bla bla"); |
| 150 |
|
| 151 |
scols_print_table(tb); |
| 152 |
|
| 153 |
scols_unref_table(tb); |
| 154 |
return EXIT_SUCCESS; |
| 155 |
} |