#pragma once #include "allocator.h" #include #include #include #include #include #include enum { TABULAR_ALIGN_LEFT, TABULAR_ALIGN_CENTER, TABULAR_ALIGN_RIGHT }; enum { TABULAR_TRUNC, TABULAR_WRAP, TABULAR_WRAP_WORDAWARE }; enum { TABULAR_ENTRY_HIDDEN, TABULAR_ENTRY_STR_SET, TABULAR_ENTRY_HIDDEN_SET, }; struct tabular_user { union { void *ptr; size_t id; }; }; struct tabular_entry { char *str; uint32_t flags; uint32_t ulen; }; struct tabular_row { struct tabular_user user; struct tabular_entry *entries; struct tabular_row *next; }; struct tabular_col { struct tabular_user user; /* column name displayed in header */ const char *name; /* create str entry to display from user objects */ char *(*to_str)(const struct tabular_user *user_row, const struct tabular_user *user_col); /* only show content if atleast one row is not 'hidden' */ bool (*is_hidden)(const struct tabular_user *user_row, const struct tabular_user *user_col); /* size restrictions */ size_t minwidth, maxwidth; /* whitespace padding */ size_t lpad, rpad; /* text alignment */ int align; /* content packing strategy */ int strategy; /* omiting column due to col contraints disallowed */ bool essential; /* reducing length due to col contraints allowed */ bool squashable; }; struct tabular_cfg { struct tabular_user user; const struct tabular_col *columns; size_t column_count; /* color mode: 1, 16, 256 */ int colors; /* fit rows to output size */ bool fit_rows; /* amount of available lines to skip */ size_t skip_lines; /* output dimensions */ size_t outw, outh; /* entry separators */ const char *hsep, *vsep, *xsep; /* total lpad, rpad */ size_t lpad, rpad; /* lazy load rows */ struct tabular_row *(*row_gen)(const struct tabular_user *user); /* style printing hook for header, separators and content */ bool (*print_style)(FILE *file, const struct tabular_cfg *cfg, const struct tabular_row *row, const struct tabular_col *col); const struct allocator *allocator; }; struct tabular_stats { bool rows_truncated; bool cols_truncated; size_t rows_displayed; size_t lines_used; }; int tabular_format(FILE *file, const struct tabular_cfg *cfg, struct tabular_stats *stat, struct tabular_row **rows); void tabular_load_row_entry_hidden(const struct tabular_cfg *cfg, struct tabular_row *row, size_t col); void tabular_load_row_entry_str(const struct tabular_cfg *cfg, struct tabular_row *row, size_t col); struct tabular_row *tabular_alloc_row(const struct tabular_cfg *cfg, int *error, struct tabular_user user); int tabular_free_rows(const struct tabular_cfg *cfg, struct tabular_row *rows);