diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 13:14:44 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-27 13:14:44 +0000 |
commit | 30ff6afe596eddafacf22b1a5b2d1a3d6254ea15 (patch) | |
tree | 9b788335f92174baf7ee18f03ca8330b8c19ce2b /libsmartcols/samples/continuous.c | |
parent | Initial commit. (diff) | |
download | util-linux-30ff6afe596eddafacf22b1a5b2d1a3d6254ea15.tar.xz util-linux-30ff6afe596eddafacf22b1a5b2d1a3d6254ea15.zip |
Adding upstream version 2.36.1.upstream/2.36.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | libsmartcols/samples/continuous.c | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/libsmartcols/samples/continuous.c b/libsmartcols/samples/continuous.c new file mode 100644 index 0000000..fe3a875 --- /dev/null +++ b/libsmartcols/samples/continuous.c @@ -0,0 +1,139 @@ +/* + * 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) +{ + scols_table_enable_maxout(tb, 1); + if (!scols_table_new_column(tb, "#NUM", 0.1, SCOLS_FL_RIGHT)) + goto fail; + if (!scols_table_new_column(tb, "DATA", 0.7, 0)) + goto fail; + if (!scols_table_new_column(tb, "TIME", 0.2, 0)) + goto fail; + return; +fail: + scols_unref_table(tb); + err(EXIT_FAILURE, "failed 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, "failed 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, "failed 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 + */ + if (scols_line_refer_data(line, COL_TIME, timecell) != 0) + err(EXIT_FAILURE, "failed to add data to table"); + + 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))); + + /* Note that libsmartcols don't print \n for last line + * in the table, but if you print a line somewhere in + * the midle of the table you need + * + * scols_table_enable_nolinesep(tb, !done); + * + * to disable line breaks. In this example it's + * unnecessary as we print the latest line only. + */ + + /* 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)); + } else + fputc('\n', scols_table_get_stream(tb)); + } while (!done); + + last = now; + } + + scols_unref_table(tb); + return EXIT_SUCCESS; +} |