Printing tables
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:
Usage
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 inputsheaderNames
- An list/array of column headers to use instead of the defaultdebug
- 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:
![](https://commandbox.ortusbooks.com/~gitbook/image?url=https%3A%2F%2F546394773-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-legacy-files%2Fo%2Fassets%252F-LA-UVvV3_TgzQyCXMWK%252F-MZJm4r9KbymIOrIfX7j%252F-MZJmdXScewdzyGmq17k%252Fimage.png%3Falt%3Dmedia%26token%3D3ddb7ddf-8b94-4db3-a080-dfb3d8728ba8&width=768&dpr=4&quality=100&sign=9f1d3465&sv=1)
Array of arrays plus column headers
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.
Array of structs plus column headers
You can also pass an array of structs where the keys of the structs match the column names in the headers array.
Query Object
If you have data already in a query object, you can pass that directly as the first parameter. The data
parameter is ignored here.
Formatting cells
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 printoptions
- 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.
Include only certain columns
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)
![](https://commandbox.ortusbooks.com/~gitbook/image?url=https%3A%2F%2F546394773-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-legacy-files%2Fo%2Fassets%252F-LA-UVvV3_TgzQyCXMWK%252F-MZJm4r9KbymIOrIfX7j%252F-MZJo3NmPydodHbPESes%252Fimage.png%3Falt%3Dmedia%26token%3D88ec5d74-dc69-46f1-a743-0f8777b4465e&width=768&dpr=4&quality=100&sign=fcf78ce9&sv=1)
Auto-resizing columns
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.
Removal of columns in small terminals
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.
Last updated