tabular.h (2742B)
1#pragma once 2 3#include "allocator.h" 4 5#include <wchar.h> 6#include <stdio.h> 7#include <stdbool.h> 8#include <stdint.h> 9#include <stdlib.h> 10#include <wctype.h> 11 12enum { 13 TABULAR_ALIGN_LEFT, 14 TABULAR_ALIGN_CENTER, 15 TABULAR_ALIGN_RIGHT 16}; 17 18enum { 19 TABULAR_TRUNC, 20 TABULAR_WRAP, 21 TABULAR_WRAP_WORDAWARE 22}; 23 24enum { 25 TABULAR_ENTRY_HIDDEN, 26 TABULAR_ENTRY_STR_SET, 27 TABULAR_ENTRY_HIDDEN_SET, 28}; 29 30struct tabular_user { 31 union { 32 void *ptr; 33 size_t id; 34 }; 35}; 36 37struct tabular_entry { 38 char *str; 39 uint32_t flags; 40 uint32_t ulen; 41}; 42 43struct tabular_row { 44 struct tabular_user user; 45 struct tabular_entry *entries; 46 struct tabular_row *next; 47}; 48 49struct tabular_col { 50 struct tabular_user user; 51 52 /* column name displayed in header */ 53 const char *name; 54 55 /* create str entry to display from user objects */ 56 char *(*to_str)(const struct tabular_user *user_row, 57 const struct tabular_user *user_col); 58 59 /* only show content if atleast one row is not 'hidden' */ 60 bool (*is_hidden)(const struct tabular_user *user_row, 61 const struct tabular_user *user_col); 62 63 /* size restrictions */ 64 size_t minwidth, maxwidth; 65 66 /* whitespace padding */ 67 size_t lpad, rpad; 68 69 /* text alignment */ 70 int align; 71 72 /* content packing strategy */ 73 int strategy; 74 75 /* omiting column due to col contraints disallowed */ 76 bool essential; 77 78 /* reducing length due to col contraints allowed */ 79 bool squashable; 80}; 81 82struct tabular_cfg { 83 struct tabular_user user; 84 85 const struct tabular_col *columns; 86 size_t column_count; 87 88 /* color mode: 1, 16, 256 */ 89 int colors; 90 91 /* fit rows to output size */ 92 bool fit_rows; 93 94 /* amount of available lines to skip */ 95 size_t skip_lines; 96 97 /* output dimensions */ 98 size_t outw, outh; 99 100 /* entry separators */ 101 const char *hsep, *vsep, *xsep; 102 103 /* total lpad, rpad */ 104 size_t lpad, rpad; 105 106 /* lazy load rows */ 107 struct tabular_row *(*row_gen)(const struct tabular_user *user); 108 109 /* style printing hook for header, separators and content */ 110 bool (*print_style)(FILE *file, const struct tabular_cfg *cfg, 111 const struct tabular_row *row, const struct tabular_col *col); 112 113 const struct allocator *allocator; 114}; 115 116struct tabular_stats { 117 bool rows_truncated; 118 bool cols_truncated; 119 size_t rows_displayed; 120 size_t lines_used; 121}; 122 123int tabular_format(FILE *file, const struct tabular_cfg *cfg, 124 struct tabular_stats *stat, struct tabular_row **rows); 125 126void tabular_load_row_entry_hidden(const struct tabular_cfg *cfg, 127 struct tabular_row *row, size_t col); 128void tabular_load_row_entry_str(const struct tabular_cfg *cfg, 129 struct tabular_row *row, size_t col); 130 131struct tabular_row *tabular_alloc_row(const struct tabular_cfg *cfg, 132 int *error, struct tabular_user user); 133int tabular_free_rows(const struct tabular_cfg *cfg, struct tabular_row *rows);