| 1 |
/* |
| 2 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
* |
| 4 |
* Copyright (C) 2016 Karel Zak <[email protected]> |
| 5 |
* Copyright (C) 2023 Thomas Weißschuh <[email protected]> |
| 6 |
*/ |
| 7 |
#include <stdlib.h> |
| 8 |
#include <unistd.h> |
| 9 |
#include <string.h> |
| 10 |
#include <errno.h> |
| 11 |
|
| 12 |
#include "c.h" |
| 13 |
#include "xalloc.h" |
| 14 |
|
| 15 |
#include "libsmartcols.h" |
| 16 |
|
| 17 |
/* add columns to the @tb */ |
| 18 |
static void setup_columns(struct libscols_table *tb) |
| 19 |
{ |
| 20 |
scols_table_enable_maxout(tb, 1); |
| 21 |
if (!scols_table_new_column(tb, "COUNT", 0.1, SCOLS_FL_RIGHT)) |
| 22 |
goto fail; |
| 23 |
if (!scols_table_new_column(tb, "TEXT", 0.9, 0)) |
| 24 |
goto fail; |
| 25 |
return; |
| 26 |
fail: |
| 27 |
scols_unref_table(tb); |
| 28 |
err(EXIT_FAILURE, "failed to create output columns"); |
| 29 |
} |
| 30 |
|
| 31 |
static struct libscols_line *add_line(struct libscols_table *tb, int i) |
| 32 |
{ |
| 33 |
struct libscols_line *ln = scols_table_new_line(tb, NULL); |
| 34 |
|
| 35 |
if (!ln) |
| 36 |
err(EXIT_FAILURE, "failed to create output line"); |
| 37 |
|
| 38 |
if (scols_line_sprintf(ln, 0, "%d", i)) |
| 39 |
goto fail; |
| 40 |
|
| 41 |
if (scols_line_sprintf(ln, 1, "text%d", i)) |
| 42 |
goto fail; |
| 43 |
|
| 44 |
return ln; |
| 45 |
fail: |
| 46 |
scols_unref_table(tb); |
| 47 |
err(EXIT_FAILURE, "failed to create output line"); |
| 48 |
} |
| 49 |
|
| 50 |
int main(void) |
| 51 |
{ |
| 52 |
struct libscols_table *tb; |
| 53 |
size_t i; |
| 54 |
|
| 55 |
scols_init_debug(0); |
| 56 |
|
| 57 |
tb = scols_new_table(); |
| 58 |
if (!tb) |
| 59 |
err(EXIT_FAILURE, "failed to create output table"); |
| 60 |
scols_table_enable_json(tb, 1); |
| 61 |
|
| 62 |
setup_columns(tb); |
| 63 |
|
| 64 |
for (i = 0; i < 10; i++) { |
| 65 |
struct libscols_line *line; |
| 66 |
|
| 67 |
line = add_line(tb, i); |
| 68 |
|
| 69 |
/* print the line */ |
| 70 |
scols_table_print_range(tb, line, NULL); |
| 71 |
|
| 72 |
fflush(scols_table_get_stream(tb)); |
| 73 |
} |
| 74 |
|
| 75 |
scols_unref_table(tb); |
| 76 |
return EXIT_SUCCESS; |
| 77 |
} |