test.c (2874B)
1#include "tabular.h" 2#include "allocator.h" 3 4#include <sys/ioctl.h> 5#include <err.h> 6#include <string.h> 7#include <stdbool.h> 8#include <stdio.h> 9 10#define ARRLEN(x) (sizeof(x)/sizeof(*(x))) 11 12bool print_style(FILE *file, const struct tabular_cfg *cfg, 13 const struct tabular_row *row, const struct tabular_col *col); 14 15char *col_pos_str(const struct tabular_user *row, const struct tabular_user *col); 16bool col_pos_hidden(const struct tabular_user *user); 17 18char *col_name_str(const struct tabular_user *row, const struct tabular_user *col); 19bool col_name_hidden(const struct tabular_user *user); 20 21static const char **argv = NULL; 22 23static struct tabular_col columns[] = { 24 { 25 .name = "Pos", 26 27 .to_str = col_pos_str, 28 .is_hidden = NULL, 29 30 .minwidth = 3, 31 .maxwidth = 3, 32 .lpad = 0, 33 .rpad = 0, 34 35 .align = TABULAR_ALIGN_CENTER, 36 37 .strategy = TABULAR_TRUNC, 38 .essential = true, 39 }, 40 { 41 .name = "Value", 42 43 .to_str = col_name_str, 44 .is_hidden = NULL, 45 46 .minwidth = 5, 47 .maxwidth = 80, 48 .lpad = 0, 49 .rpad = 0, 50 51 .align = TABULAR_ALIGN_LEFT, 52 53 .strategy = TABULAR_WRAP_WORDAWARE, 54 .essential = true, 55 }, 56}; 57 58static struct tabular_cfg cfg = { 59 .colors = 256, 60 61 .columns = columns, 62 .column_count = ARRLEN(columns), 63 64 .fit_rows = true, 65 66 .hsep = "│ ", 67 .vsep = "─", 68 .xsep = "┼─", 69 70 .outw = 0, 71 .outh = 0, 72 73 .lpad = 1, 74 .rpad = 1, 75 76 .print_style = print_style, 77 78 .skip_lines = 3, 79 80 .allocator = &stdlib_heap_allocator 81}; 82 83bool 84print_style(FILE *file, const struct tabular_cfg *cfg, 85 const struct tabular_row *row, const struct tabular_col *col) 86{ 87 if (cfg->colors == 256) { 88 if (!col) { /* separators */ 89 fprintf(file, "\x1b[90m"); 90 return true; 91 } else if (!row) { /* header */ 92 fprintf(file, "\x1b[1m"); 93 return true; 94 } else if (!strcmp(col->name, "Name")) { 95 fprintf(file, "\x1b[35m"); 96 return true; 97 } 98 } 99 100 return false; 101} 102 103char * 104col_pos_str(const struct tabular_user *row, const struct tabular_user *col) 105{ 106 char buf[16]; 107 108 snprintf(buf, sizeof(buf), "%li", row->id); 109 110 return strdup(buf); 111} 112 113char * 114col_name_str(const struct tabular_user *row, const struct tabular_user *col) 115{ 116 return strdup(argv[row->id]); 117} 118 119int 120main(int argc, const char **_argv) 121{ 122 struct tabular_row *rows, **end; 123 struct tabular_stats stats; 124 struct winsize ws; 125 int i, rc; 126 127 argv = _argv; 128 129 rc = ioctl(1, TIOCGWINSZ, &ws); 130 if (!rc) { 131 cfg.outw = ws.ws_col; 132 cfg.outh = ws.ws_row; 133 } else { 134 cfg.outw = 80; 135 cfg.outh = 26; 136 } 137 138 rows = NULL; 139 end = &rows; 140 for (i = 0; i < argc; i++) { 141 *end = tabular_alloc_row(&cfg, &rc, 142 (struct tabular_user) { .id = (size_t) i }); 143 if (!*end) errx(1, "tabular_append_row %i", rc); 144 end = &(*end)->next; 145 } 146 147 rc = tabular_format(stdout, &cfg, &stats, &rows); 148 if (rc) errx(1, "tabular_format %i", rc); 149 150 printf("\n%lu lines, %lu rows\n", 151 stats.lines_used, stats.rows_displayed); 152 153 tabular_free_rows(&cfg, rows); 154}