libsmartcols: add sample-scols-continuous
Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
2981e0fd0e
commit
12ca9cb1f5
|
@ -2,7 +2,8 @@
|
|||
check_PROGRAMS += \
|
||||
sample-scols-tree \
|
||||
sample-scols-title \
|
||||
sample-scols-wrap
|
||||
sample-scols-wrap \
|
||||
sample-scols-continuous
|
||||
|
||||
sample_scols_tree_SOURCES = libsmartcols/samples/tree.c
|
||||
sample_scols_tree_LDADD = libsmartcols.la libcommon.la
|
||||
|
@ -15,3 +16,8 @@ sample_scols_title_CFLAGS = -I$(ul_libsmartcols_incdir)
|
|||
sample_scols_wrap_SOURCES = libsmartcols/samples/wrap.c
|
||||
sample_scols_wrap_LDADD = libsmartcols.la
|
||||
sample_scols_wrap_CFLAGS = -I$(ul_libsmartcols_incdir)
|
||||
|
||||
sample_scols_continuous_SOURCES = libsmartcols/samples/continuous.c
|
||||
sample_scols_continuous_LDADD = libsmartcols.la libcommon.la
|
||||
sample_scols_continuous_CFLAGS = -I$(ul_libsmartcols_incdir)
|
||||
|
||||
|
|
|
@ -0,0 +1,128 @@
|
|||
/*
|
||||
* Copyright (C) 2016 Karel Zak <kzak@redhat.com>
|
||||
*
|
||||
* This file may be redistributed under the terms of the
|
||||
* GNU Lesser General Public License.
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "c.h"
|
||||
#include "nls.h"
|
||||
#include "strutils.h"
|
||||
#include "xalloc.h"
|
||||
|
||||
#include "libsmartcols.h"
|
||||
|
||||
#define TIME_PERIOD 3.0 /* seconds */
|
||||
|
||||
enum { COL_NUM, COL_DATA, COL_TIME };
|
||||
|
||||
static double time_diff(struct timeval *a, struct timeval *b)
|
||||
{
|
||||
return (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec) / 1E6;
|
||||
}
|
||||
|
||||
/* add columns to the @tb */
|
||||
static void setup_columns(struct libscols_table *tb)
|
||||
{
|
||||
if (!scols_table_new_column(tb, "#NUM", 0, SCOLS_FL_RIGHT))
|
||||
goto fail;
|
||||
if (!scols_table_new_column(tb, "DATA", 0, 0))
|
||||
goto fail;
|
||||
if (!scols_table_new_column(tb, "TIME", 0, 0))
|
||||
goto fail;
|
||||
return;
|
||||
fail:
|
||||
scols_unref_table(tb);
|
||||
err(EXIT_FAILURE, "faild to create output columns");
|
||||
}
|
||||
|
||||
static struct libscols_line *add_line(struct libscols_table *tb, size_t i)
|
||||
{
|
||||
char *p;
|
||||
struct libscols_line *ln = scols_table_new_line(tb, NULL);
|
||||
|
||||
if (!ln)
|
||||
err(EXIT_FAILURE, "failed to create output line");
|
||||
|
||||
xasprintf(&p, "%zu", i);
|
||||
if (scols_line_refer_data(ln, COL_NUM, p))
|
||||
goto fail;
|
||||
|
||||
xasprintf(&p, "data-%02zu-%02zu-%02zu-end", i + 1, i + 2, i + 3);
|
||||
if (scols_line_refer_data(ln, COL_DATA, p))
|
||||
goto fail;
|
||||
|
||||
return ln;
|
||||
fail:
|
||||
scols_unref_table(tb);
|
||||
err(EXIT_FAILURE, "faild to create output line");
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
struct libscols_table *tb;
|
||||
size_t i;
|
||||
struct timeval last;
|
||||
|
||||
scols_init_debug(0);
|
||||
|
||||
tb = scols_new_table();
|
||||
if (!tb)
|
||||
err(EXIT_FAILURE, "faild to create output table");
|
||||
|
||||
setup_columns(tb);
|
||||
gettimeofday(&last, NULL);
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
struct libscols_line *line;
|
||||
struct timeval now;
|
||||
int done = 0;
|
||||
char *timecell = xmalloc( sizeof(stringify_value(UINT_MAX)) );
|
||||
|
||||
line = add_line(tb, i);
|
||||
|
||||
/* Make a reference from cell data to the buffer, then we can
|
||||
* update cell data without any interaction with libsmartcols
|
||||
*/
|
||||
scols_line_refer_data(line, COL_TIME, timecell);
|
||||
|
||||
do {
|
||||
double diff;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
diff = time_diff(&now, &last);
|
||||
|
||||
if (now.tv_sec == last.tv_sec + (long) TIME_PERIOD)
|
||||
done = 1;
|
||||
else
|
||||
xusleep(100000);
|
||||
|
||||
/* update "TIME" cell data */
|
||||
sprintf(timecell, "%f [%3d%%]", diff,
|
||||
done ? 100 : (int)(diff / (TIME_PERIOD / 100.0)));
|
||||
|
||||
/* don't print line separator when in progress */
|
||||
scols_table_enable_nolinesep(tb, !done);
|
||||
/* print the line */
|
||||
scols_table_print_range(tb, line, NULL);
|
||||
|
||||
if (!done) {
|
||||
/* terminal is waiting for \n, fflush() to force output */
|
||||
fflush(scols_table_get_stream(tb));
|
||||
/* move to the begin of the line */
|
||||
fputc('\r', scols_table_get_stream(tb));
|
||||
}
|
||||
} while (!done);
|
||||
|
||||
last = now;
|
||||
}
|
||||
|
||||
scols_unref_table(tb);
|
||||
return EXIT_SUCCESS;
|
||||
}
|
Loading…
Reference in New Issue