Printing tables
Last updated
Last updated
The print helper contains a feature that will generate ASCII tables to print tabular data from the CLI. The easiest way to see what this looks like is to run the outdated
command in the root of a project:
There are several easy ways to call the table printer. Here are the arguments to the print.table()
method:
data
- Any type of data for the table. Each item in the array may either be an array in the correct order matching the number of headers or a struct with keys matching the headers.
includedHeaders
- A list of headers to include. Used for query inputs
headerNames
- A list/array of column headers to use instead of the default
debug
- Only print out the names of the columns and the first row values
Below are some basic examples that all produce the same CLI output:
You can pass an array of column headers as the first argument and then an array of rows, where each row contains an array with data for each column.
You can also pass an array of structs where the keys of the structs match the column names in the headers array.
If you have data already in a query object, you can pass that directly as the first parameter. The data
parameter is ignored here.
There is some basic support for applying formatting at a cell level. To do this, use the "array" version of the data and instead of providing a string for the cell value, provide a struct containing:
value
- The actual string value to print
options
- A string of print helper options- typically a color or background color
The options
can contain any color from the system-colors
command and follows the same format as the Print Helper.
You can limit the columns that display in the table regardless of whether you use an array of data or a query object by using the includeHeaders
parameter. This example outputs a sorted list of the names and version of all the Lucee Extensions installed in the CLI. (extensionList()
is a built in Lucee function that returns a query object)
If the data on a given row is too much to display in the given terminal, the table printer will automatically shrink columns based on how much "whitespace" is in them. Values which are too long to display will be truncated and ...
will appear at the end. Here is the output of the outdated
command.
If a user has a terminal so small that all the columns simply won't fit, the table printer will automatically eliminate extra columns on the right hand side of the table and insert a single column whose header and values are all ...
to show missing data. Here is the output of the outdated
command.