| 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 |
if (!scols_table_new_column(tb, "NAME", 0, 0)) |
| 30 |
goto fail; |
| 31 |
if (!scols_table_new_column(tb, "DATA", 0, 0)) |
| 32 |
goto fail; |
| 33 |
return; |
| 34 |
fail: |
| 35 |
scols_unref_table(tb); |
| 36 |
err(EXIT_FAILURE, "failed to create output columns"); |
| 37 |
} |
| 38 |
|
| 39 |
static void add_line(struct libscols_table *tb, const char *name, const char *data) |
| 40 |
{ |
| 41 |
struct libscols_line *ln = scols_table_new_line(tb, NULL); |
| 42 |
if (!ln) |
| 43 |
err(EXIT_FAILURE, "failed to create output line"); |
| 44 |
|
| 45 |
if (scols_line_set_data(ln, COL_NAME, name)) |
| 46 |
goto fail; |
| 47 |
if (scols_line_set_data(ln, COL_DATA, data)) |
| 48 |
goto fail; |
| 49 |
return; |
| 50 |
fail: |
| 51 |
scols_unref_table(tb); |
| 52 |
err(EXIT_FAILURE, "failed to create output line"); |
| 53 |
} |
| 54 |
|
| 55 |
int main(int argc, char *argv[]) |
| 56 |
{ |
| 57 |
struct libscols_table *tb; |
| 58 |
struct libscols_symbols *sy; |
| 59 |
struct libscols_cell *title; |
| 60 |
int c; |
| 61 |
|
| 62 |
static const struct option longopts[] = { |
| 63 |
{ "maxout", 0, NULL, 'm' }, |
| 64 |
{ "width", 1, NULL, 'w' }, |
| 65 |
{ "help", 1, NULL, 'h' }, |
| 66 |
|
| 67 |
{ NULL, 0, NULL, 0 }, |
| 68 |
}; |
| 69 |
|
| 70 |
setlocale(LC_ALL, ""); /* just to have enable UTF8 chars */ |
| 71 |
|
| 72 |
scols_init_debug(0); |
| 73 |
|
| 74 |
tb = scols_new_table(); |
| 75 |
if (!tb) |
| 76 |
err(EXIT_FAILURE, "failed to create output table"); |
| 77 |
|
| 78 |
while((c = getopt_long(argc, argv, "hmw:", longopts, NULL)) != -1) { |
| 79 |
switch(c) { |
| 80 |
case 'h': |
| 81 |
printf("%s [--help | --maxout | --width <num>]\n", program_invocation_short_name); |
| 82 |
break; |
| 83 |
case 'm': |
| 84 |
scols_table_enable_maxout(tb, TRUE); |
| 85 |
break; |
| 86 |
case 'w': |
| 87 |
scols_table_set_termforce(tb, SCOLS_TERMFORCE_ALWAYS); |
| 88 |
scols_table_set_termwidth(tb, strtou32_or_err(optarg, "failed to parse terminal width")); |
| 89 |
break; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
scols_table_enable_colors(tb, isatty(STDOUT_FILENO)); |
| 94 |
setup_columns(tb); |
| 95 |
add_line(tb, "foo", "bla bla bla"); |
| 96 |
add_line(tb, "bar", "alb alb alb"); |
| 97 |
|
| 98 |
title = scols_table_get_title(tb); |
| 99 |
|
| 100 |
/* right */ |
| 101 |
scols_cell_set_data(title, "This is right title"); |
| 102 |
scols_cell_set_color(title, "red"); |
| 103 |
scols_cell_set_flags(title, SCOLS_CELL_FL_RIGHT); |
| 104 |
scols_print_table(tb); |
| 105 |
|
| 106 |
/* left without padding */ |
| 107 |
scols_cell_set_data(title, "This is left title (without padding)"); |
| 108 |
scols_cell_set_color(title, "yellow"); |
| 109 |
scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT); |
| 110 |
scols_print_table(tb); |
| 111 |
|
| 112 |
/* center */ |
| 113 |
sy = scols_new_symbols(); |
| 114 |
if (!sy) |
| 115 |
err_oom(); |
| 116 |
scols_table_set_symbols(tb, sy); |
| 117 |
scols_unref_symbols(sy); |
| 118 |
|
| 119 |
scols_symbols_set_title_padding(sy, "="); |
| 120 |
scols_cell_set_data(title, "This is center title (with padding)"); |
| 121 |
scols_cell_set_color(title, "green"); |
| 122 |
scols_cell_set_flags(title, SCOLS_CELL_FL_CENTER); |
| 123 |
scols_print_table(tb); |
| 124 |
|
| 125 |
/* left with padding */ |
| 126 |
scols_symbols_set_title_padding(sy, "-"); |
| 127 |
scols_cell_set_data(title, "This is left title (with padding)"); |
| 128 |
scols_cell_set_color(title, "blue"); |
| 129 |
scols_cell_set_flags(title, SCOLS_CELL_FL_LEFT); |
| 130 |
scols_print_table(tb); |
| 131 |
|
| 132 |
|
| 133 |
scols_unref_table(tb); |
| 134 |
return EXIT_SUCCESS; |
| 135 |
} |