From 65ce6e3249d427b3f9b2c945050ce1f7ee75d31d Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 16 Jan 2015 16:23:47 +0100 Subject: [PATCH] Xtypes: Documentation cleanup Many missing bits have been added, many a wording has been polished. The man page for table options has been moved to section 5, as it describes a general convention, not a program. --- ucw/doc/Makefile | 2 +- ucw/doc/index.txt | 3 +- ucw/doc/relnotes.txt | 1 + ucw/doc/table.txt | 45 +++++++---------- ucw/doc/ucw-tableprinter.1.txt | 80 ------------------------------- ucw/doc/ucw-tableprinter.5.txt | 88 ++++++++++++++++++++++++++++++++++ ucw/table.h | 46 +++++------------- ucw/xtypes-extra.h | 14 +++++- 8 files changed, 135 insertions(+), 144 deletions(-) delete mode 100644 ucw/doc/ucw-tableprinter.1.txt create mode 100644 ucw/doc/ucw-tableprinter.5.txt diff --git a/ucw/doc/Makefile b/ucw/doc/Makefile index 9c09efe2..26afcd6e 100644 --- a/ucw/doc/Makefile +++ b/ucw/doc/Makefile @@ -6,7 +6,7 @@ UCW_DOCS=basics log fastbuf index config configure install basecode hash docsys UCW_INDEX=$(o)/ucw/doc/def_index.html UCW_DOCS_HTML=$(addprefix $(o)/ucw/doc/,$(addsuffix .html,$(UCW_DOCS))) -UCW_MANPAGES=ucw-tableprinter.1 +UCW_MANPAGES=ucw-tableprinter.5 UCW_MANPAGES_HTML=$(patsubst %,$(o)/ucw/doc/%.html,$(UCW_MANPAGES)) UCW_MANPAGES_NROFF=$(addprefix $(o)/ucw/doc/,$(UCW_MANPAGES)) MANPAGES+=$(UCW_MANPAGES_NROFF) diff --git a/ucw/doc/index.txt b/ucw/doc/index.txt index 9d439184..5d20d5e6 100644 --- a/ucw/doc/index.txt +++ b/ucw/doc/index.txt @@ -46,7 +46,8 @@ Modules - <> - <> - <> -- <> +- <> and its <> +- <> Other features -------------- diff --git a/ucw/doc/relnotes.txt b/ucw/doc/relnotes.txt index 94d626d3..85e64f37 100644 --- a/ucw/doc/relnotes.txt +++ b/ucw/doc/relnotes.txt @@ -6,6 +6,7 @@ Release notes * Fixed race conditions in <>. * The <> have been documented and integrated in the table printer, command-line option parser, and configuration parser. +* The <> have been documented. 6.1 (2014-08-09) ---------------- diff --git a/ucw/doc/table.txt b/ucw/doc/table.txt index 65cd291b..94d91012 100644 --- a/ucw/doc/table.txt +++ b/ucw/doc/table.txt @@ -4,34 +4,23 @@ Table printer The table printer module provides formatting of 2-dimensional tables in various ways. -Each table print-out consists of a number of rows, which are processed -one after another. All rows have the same number of columns. -Internally, the columns have a fixed order, each column has its name -and a data type (e.g., int, uint, u64, etc.). The data type is -realized using extended types (referred to as xtypes). The table -printer checks that each cell is filled by a value of the appropriate -type, except that a string value is allowed in any cell (for example, -this allows a numeric cell to be set to "--" or "unknown"). Once a -table is defined, it can be printed using a variety of formatters -(human-readable, tab-separated values, etc.) and its cells can be -formatted using at least three different formats: 1) pretty (or -human-readable); 2) raw; 3) default. - -The table definition consists of various column types, each column -type is a pair consisting of a name and a data type. Name of each -column must be unique in the whole table definition. Each column type -can have multiple instances in the final table print-out. The columns -are printed using xtypes. For example: let have a column definition -with name 'size' of type xt_size. The column can be printed on -position 0 (in bytes) and position 5 (in kilobytes). This allows easy -filtering using standart cmd line filters. For example, we want to -remove all rows that has size greater then 1572864 bytes (1.5MB) and -print the size in human readable format. Human readable format means -that if there is size of 1610612736 bytes it will be rather printed as -1536MB. - -Also, columns can be printed in an arbitrary order and with repeated -columns. +Each table print-out consists of a number of 'rows', which are processed +one after another. All rows have the same number of 'columns', each intersection +of a row with a column is called a 'cell'. Each cell holds data, represented +using the <> (a.k.a. 'xtypes'). The table printer +checks that the cells are filled by values of the appropriate types; +additionally, a string value is allowed in any cell (for example, +this allows a numeric cell to be set to "--" or "unknown"). + +Once a table is defined, it can be printed using a variety of 'formatters' +(human-readable, tab-separated values, etc.) and its cells can be formatted +using at least three different formats: pretty (or human-readable), raw, +default. Formatting of cells is handled by the xtype module. + +The order of columns can be modified, columns can be omitted, or even printed +multiple times with different formatting options. We therefore distinguish +between columns (according to the definition of the table) and 'column +instances' (in the specific print-out). Example ------- diff --git a/ucw/doc/ucw-tableprinter.1.txt b/ucw/doc/ucw-tableprinter.1.txt deleted file mode 100644 index fb42eb7a..00000000 --- a/ucw/doc/ucw-tableprinter.1.txt +++ /dev/null @@ -1,80 +0,0 @@ -UCW-TABLEPRINTER(1) -=================== - -NAME ----- - -ucw-tableprinter - UCW table printer is a program module that allows user-customized printing of tables. - -DESCRIPTION ------------ - -The table printer module provides formatting of 2-dimensional tables -in various ways. - -Each table print-out consists of a number of rows, which are processed -one after another. All rows have the same number of columns. Once a -table is defined, it can be printed using a variety of formatters -(block-line, human-readable, machine-readable, etc.). - -The table definition consists of various column definitions, each -column definition is a pair consisting of a name and a type. Name of -each column must be unique in the whole table. Each column definition -can be instantiated(printed) in its own format, e.g., a size column -can be printed three-times: 1) in bytes; 2) in gigabytes; 3) -human-readable. - -The table can be controlled using various options: - -[options="header"] -|=============================================================================================================== -| option | argument | meaning -| `header` | 0 or 1 | set whether a table header should be printed -| `noheader` | 'none' | equivalent to `header`=0 -| `cols` | comma-separated column list | set order of columns -| `fmt` | `human`/`machine`/`block` | set table formatter to one of the built-in formatters -| `col-delim` | string | set column delimiter -| `cells` | string | set column cell format mode, possibilities are: default, raw, pretty -| `raw` | 'none' | set column cell format to raw data, equivalent to cells:raw -| `pretty` | 'none' | set column cell format to pretty-printing, equivalent to cells:pretty -|=============================================================================================================== - -Column order (`cols` option) has the following format: - - := [,]* - - := [ '[' ']' ] - - is a string that does not contain comma ',' or '[',']' brackets - - := [ ',' ] - -Where is a comma-separated list of options. - -Column delimiter option (`col-delim`) can be an arbitrary string. The -cell format option `cells` can have one of the possible three values: -1) `pretty`, 2) `default`, 3) `raw`. The `raw` and `pretty` value of -`cells` option can be shortcutted as options `raw` and `pretty` with -no value. - -Additionally to standard types (like integer, floating point) there -are the two following types: 1) size; 2) timestamp. The 'size' type -has the following case-sensitive column options: `KB`, `MB`, `GB`, -`TB`, `auto`, e.g., a column of type size and name `file-size` can be -specified as `file-size[MB]` or `file-size[auto]`. The 'timestamp' -has the following case-sensitive column options: 1) `datetime` which -corresponds to date(1) format "%F %T", 2) `timestamp` which -corresponds to a number of seconds relative to start of some epoch. - -Description of table formats ----------------------------- - -'human-readable': prints columns separated by single space, rows -separated by newline '\n'. - -'block-line': prints each column on one line, i.e., the column -separator is '\n', rows are separated by double newlines -'\n\n'. Therefore, rows forms newline separated blocks of rows. - -'machine-readable': prints columns separated by a tabulator '\t', rows -separated by '\n'. diff --git a/ucw/doc/ucw-tableprinter.5.txt b/ucw/doc/ucw-tableprinter.5.txt new file mode 100644 index 00000000..5928abc9 --- /dev/null +++ b/ucw/doc/ucw-tableprinter.5.txt @@ -0,0 +1,88 @@ +UCW-TABLEPRINTER(5) +=================== + +NAME +---- + +ucw-tableprinter - a program module for customizable printing of tables. + +DESCRIPTION +----------- +The table printer module is a part of the LibUCW library. It provides formatting +of 2-dimensional tables in various ways. Users seldom interact with the table +printer itself, but programs using it often provide means for customizing table +output by passing options. + +This manual page describes the overall logic of the table printer and its +options. + +Each table print-out consists of a number of rows, which are processed +one after another. All rows have the same number of columns. Once a +table is defined, it can be printed using a variety of formatters +(block-line, human-readable, machine-readable, etc.). + +The table definition consists of various column definitions, each +column definition is a pair consisting of a name and a type. Name of +each column must be unique in the whole table. Each column definition +can be instantiated (printed) in its own format, e.g., a size column +can be printed three times: first in bytes, then in gigabytes, and +finally in a human-readable form. + +The table can be controlled using various options: + +[options="header"] +|=============================================================================================================== +| option | argument | meaning +| `header` | 0 or 1 | set whether a table header should be printed +| `noheader` | 'none' | equivalent to `header:0` +| `cols` | comma-separated column list | set order of columns and per-column options (see below) +| `fmt` | `human`/`machine`/`block` | set table formatter to one of the built-in formatters (see below) +| `col-delim` | string | set column delimiter +| `cells` | string | set column cell format mode, possibilities are: `default`, `raw`, `pretty` +| `raw` | 'none' | set column cell format to raw data, equivalent to `cells:raw` +| `pretty` | 'none' | set column cell format to pretty-printing, equivalent to `cells:pretty` +|=============================================================================================================== + +Table formats +------------- +The `fmt` option sets the overall format of the table. Currently, +the following formats are available: + +* 'human-readable' (`human`): prints columns separated by a single space, rows +separated by a newline character (ASCII 0x0a). + +* 'machine-readable' (`machine`): prints columns separated by a tab character (ASCII 0x09), +rows separated by a newline character. + +* 'block-line' (`block`): prints each column on one line, rows separated by a single +blank line. That is, the column separator is set to the newline character +and an extra newline is printed at the end of each row. + +Column definitions +------------------ +The `cols` option allows to specify a list of table columns and their options. +For instance, you can use `name,size[raw],size[pretty]` to request a table with three columns: +name, size as a raw value, and the same size pretty-printed. + +Formally, the argument of the `cols` option follows this grammar: + + := [,]* + := [ '[' ']' ] + contains no commas nor square brackets + := [ ',' ] + contains no commas nor square brackets + +Column options +-------------- +All column types accept these standard formatting modes: + +* `default`: human-readable, but not hostile to machine parsing +* `raw`: raw data with no frills +* `pretty`: tries to please humans (e.g., like `ls -h`) + +There are also formatting modes specific for particular column types: + +* Sizes can be given a unit (`KB`, `MB`, `GB`, `TB`, or `auto`; case-insensitive). +* Timestamps can be formatted as `timestamp` or `epoch` (both are seconds since the + Unix epoch), or `datetime` (corresponds to date(1) format `"%F %T"`. Currently, + `raw` is an alias for `timestamp` and `pretty` is the same as `datetime`. diff --git a/ucw/table.h b/ucw/table.h index 88f721c7..494276ba 100644 --- a/ucw/table.h +++ b/ucw/table.h @@ -151,7 +151,7 @@ struct table { * * `TBL_COL_`'type'`_FMT(name, width, fmt)` defines a column with a custom format string * * `TBL_COL_END` ends the column definitions * * `TBL_COL_ORDER` specifies custom ordering of columns in the output - * * `TBL_COL_DELIMITER` and `TBL_APPEND_DELIMITER` override default delimiters + * * `TBL_COL_DELIMITER` overrides the default delimiter * * `TBL_FMT_HUMAN_READABLE` requests human-readable formatting (this is the default) * * `TBL_FMT_MACHINE_READABLE` requests machine-readable TSV output * * `TBL_FMT_BLOCKLINE` requests block formatting (each cell printed a pair of a key and value on its own line) @@ -238,7 +238,6 @@ void table_end(struct table *tbl); * to the `value` ***/ - #define TABLE_COL_PROTO(_name, _type) void table_col_##_name(struct table *tbl, int col, _type val); TABLE_COL_PROTO(int, int) @@ -304,14 +303,16 @@ void table_reset_row(struct table *tbl); int table_get_col_idx(struct table *tbl, const char *col_name); /** - * Sets a string option to an instance of a column type. This is the default version that checks - * whether the xtype::parse_fmt can be called and calls it. However, there are situation in which - * the xtype::parse_fmt is not sufficient, e.g., column decoration, post-processing, etc. + * Sets a string option on a column instance. + * + * By default, the option is parsed as a formatting mode of the corresponding <> + * using <>. * - * Each struct table_column has a pointer to a customized version of table_set_col_opt (called - * set_col_opt). The hook set_call_opt should be always called through @table_set_col_opt. The hook - * and @table_set_col_opt has the same prototype, but @table_set_col_opt should never be used as the - * table_set_opt hook. + * As special cases might require special handling (e.g., column decoration, post-processing, etc.), + * a column can define a `set_col_opt` hook, which takes over option parsing. (Beware, the hook must + * not be called directly and it must not call this function.) + * + * See <> for more. **/ const char *table_set_col_opt(struct table *tbl, uint col_inst_idx, const char *col_opt); @@ -337,15 +338,7 @@ void table_set_col_order(struct table *tbl, const struct table_col_instance *col * names. Returns NULL for success and an error message otherwise. The string is not referenced after * this function returns. * - * The format of the col_order string is the following: - * := [,]* - * - * := [ '[' ']' ] - * - * is a string that does not contain comma ',' or '[',']' brackets - * - * := [ ',' ] - * - is a comma-separated list of options + * See <> for full syntax. **/ const char *table_set_col_order_by_name(struct table *tbl, const char *col_order); @@ -360,21 +353,8 @@ bool table_col_is_printed(struct table *tbl, uint col_def_idx); void table_set_formatter(struct table *tbl, const struct table_formatter *fmt); /** - * Set a table option. All options have a key and a value. Currently, - * the following keys are defined (other keys can be accepted by formatters): - * - * [options="header"] - * |=================================================================================================== - * | key | value | meaning - * | `header` | 0 or 1 | set whether a table header should be printed - * | `noheader` | 'none' | equivalent to `header`=0 - * | `cols` | comma-separated column list | set order of columns - * | `fmt` | `human`/`machine`/`block` | set table formatter to one of the built-in formatters - * | `col-delim`| string | set column delimiter - * | `cells` | string | set column format mode - * | `raw` | 'none' | set column format to raw data - * | `pretty` | 'none' | set column format to pretty-printing - * |=================================================================================================== + * Set a table option. All options have a key and a value. + * See <>. **/ const char *table_set_option_value(struct table *tbl, const char *key, const char *value); diff --git a/ucw/xtypes-extra.h b/ucw/xtypes-extra.h index fc94a969..e82a7c1b 100644 --- a/ucw/xtypes-extra.h +++ b/ucw/xtypes-extra.h @@ -37,7 +37,10 @@ enum size_units { XT_SIZE_UNIT_AUTO }; -/** Custom formatting mode: use a specified unit (`XT_SIZE_UNIT_`'xxx') **/ +/** + * Custom formatting mode: use a specified unit (`XT_SIZE_UNIT_`'xxx'). + * Textual representation of the mode is the name of the unit (case-insensitive). + **/ #define XT_SIZE_FMT_UNIT(_unit) (_unit | XT_SIZE_FMT_FIXED_UNIT) #define XT_SIZE_FMT_FIXED_UNIT XTYPE_FMT_CUSTOM @@ -53,7 +56,16 @@ TABLE_COL_PROTO(size, u64) * `xt_timestamp` is a timestamp, internally represented as `time_t`. ***/ +/** + * Custom formatting mode: seconds since Unix epoch. Currently, + * this is the same as the raw format. Textual representation: `timestamp` or `epoch`. + **/ #define XT_TIMESTAMP_FMT_EPOCH XTYPE_FMT_RAW + +/** + * Custom formatting mode: date and time. Currently, this is the same + * as the human-readable format. Textual representation: `datetime`. + **/ #define XT_TIMESTAMP_FMT_DATETIME XTYPE_FMT_PRETTY extern const struct xtype xt_timestamp; -- 2.39.2