| 1 |
/* |
| 2 |
* Copyright (C) 2016 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/time.h> |
| 13 |
|
| 14 |
#include "c.h" |
| 15 |
#include "nls.h" |
| 16 |
#include "strutils.h" |
| 17 |
#include "timeutils.h" |
| 18 |
#include "xalloc.h" |
| 19 |
|
| 20 |
#include "libsmartcols.h" |
| 21 |
|
| 22 |
#define TIME_PERIOD 3.0 /* seconds */ |
| 23 |
|
| 24 |
enum { COL_NUM, COL_DATA, COL_TIME }; |
| 25 |
|
| 26 |
/* add columns to the @tb */ |
| 27 |
static void setup_columns(struct libscols_table *tb) |
| 28 |
{ |
| 29 |
scols_table_enable_maxout(tb, 1); |
| 30 |
if (!scols_table_new_column(tb, "#NUM", 0.1, SCOLS_FL_RIGHT)) |
| 31 |
goto fail; |
| 32 |
if (!scols_table_new_column(tb, "DATA", 0.7, 0)) |
| 33 |
goto fail; |
| 34 |
if (!scols_table_new_column(tb, "TIME", 0.2, 0)) |
| 35 |
goto fail; |
| 36 |
return; |
| 37 |
fail: |
| 38 |
scols_unref_table(tb); |
| 39 |
err(EXIT_FAILURE, "failed to create output columns"); |
| 40 |
} |
| 41 |
|
| 42 |
static struct libscols_line *add_line(struct libscols_table *tb, size_t i) |
| 43 |
{ |
| 44 |
struct libscols_line *ln = scols_table_new_line(tb, NULL); |
| 45 |
|
| 46 |
if (!ln) |
| 47 |
err(EXIT_FAILURE, "failed to create output line"); |
| 48 |
|
| 49 |
if (scols_line_sprintf(ln, COL_NUM, "%zu", i)) |
| 50 |
goto fail; |
| 51 |
|
| 52 |
if (scols_line_sprintf(ln, COL_DATA, "data-%02zu-%02zu-%02zu-end", |
| 53 |
i + 1, i + 2, i + 3)) |
| 54 |
goto fail; |
| 55 |
|
| 56 |
return ln; |
| 57 |
fail: |
| 58 |
scols_unref_table(tb); |
| 59 |
err(EXIT_FAILURE, "failed to create output line"); |
| 60 |
} |
| 61 |
|
| 62 |
int main(void) |
| 63 |
{ |
| 64 |
struct libscols_table *tb; |
| 65 |
size_t i; |
| 66 |
const size_t timecellsz = 500; |
| 67 |
struct timeval last; |
| 68 |
|
| 69 |
scols_init_debug(0); |
| 70 |
|
| 71 |
tb = scols_new_table(); |
| 72 |
if (!tb) |
| 73 |
err(EXIT_FAILURE, "failed to create output table"); |
| 74 |
|
| 75 |
setup_columns(tb); |
| 76 |
gettimeofday(&last, NULL); |
| 77 |
|
| 78 |
for (i = 0; i < 10; i++) { |
| 79 |
struct libscols_line *line; |
| 80 |
struct timeval now; |
| 81 |
int done = 0; |
| 82 |
char *timecell = xcalloc(1, timecellsz ); |
| 83 |
|
| 84 |
line = add_line(tb, i); |
| 85 |
|
| 86 |
/* Make a reference from cell data to the buffer, then we can |
| 87 |
* update cell data without any interaction with libsmartcols |
| 88 |
*/ |
| 89 |
if (scols_line_refer_data(line, COL_TIME, timecell) != 0) |
| 90 |
err(EXIT_FAILURE, "failed to add data to table"); |
| 91 |
|
| 92 |
do { |
| 93 |
double diff; |
| 94 |
|
| 95 |
gettimeofday(&now, NULL); |
| 96 |
diff = time_diff(&now, &last); |
| 97 |
|
| 98 |
if (now.tv_sec == last.tv_sec + (long) TIME_PERIOD) |
| 99 |
done = 1; |
| 100 |
else |
| 101 |
xusleep(100000); |
| 102 |
|
| 103 |
/* update "TIME" cell data */ |
| 104 |
snprintf(timecell, timecellsz, "%f [%3d%%]", diff, |
| 105 |
done ? 100 : (int)(diff / (TIME_PERIOD / 100.0))); |
| 106 |
|
| 107 |
/* Note that libsmartcols don't print \n for last line |
| 108 |
* in the table, but if you print a line somewhere in |
| 109 |
* the middle of the table you need |
| 110 |
* |
| 111 |
* scols_table_enable_nolinesep(tb, !done); |
| 112 |
* |
| 113 |
* to disable line breaks. In this example it's |
| 114 |
* unnecessary as we print the latest line only. |
| 115 |
*/ |
| 116 |
|
| 117 |
/* print the line */ |
| 118 |
scols_table_print_range(tb, line, NULL); |
| 119 |
|
| 120 |
if (!done) { |
| 121 |
/* terminal is waiting for \n, fflush() to force output */ |
| 122 |
fflush(scols_table_get_stream(tb)); |
| 123 |
/* move to the begin of the line */ |
| 124 |
fputc('\r', scols_table_get_stream(tb)); |
| 125 |
} else |
| 126 |
fputc('\n', scols_table_get_stream(tb)); |
| 127 |
} while (!done); |
| 128 |
|
| 129 |
last = now; |
| 130 |
} |
| 131 |
|
| 132 |
scols_unref_table(tb); |
| 133 |
return EXIT_SUCCESS; |
| 134 |
} |