/display-boxes

Bash function to display nice-looking boxes around your content

Primary LanguageShellMIT LicenseMIT

display-boxes

Description

Bash function or script to display nice-looking boxes around your content like:

The function will automatically determine the length of each column and draw the box appropriately. It accepts one or two multi-line strings which have any single-character delimiter or 2+ spaces to separate columns.

Input

Required:

$headers

The first contains the header data with tabs separating out the columns.

$body

The second contains the body data which is typically a multi-line string with tabs separating the columns.

Note: You can combine these into a single string if you prefer. Just separate them by one or more blank lines.

Optional:

$style

The -s argument can be used to set the box style. Default is 1.

$padding

The -p argument can be passed in to alter the padding left and right of the column data. Default is 1.

$delimiter

The -d argument can be passed in to specify the data delimiter if it is not a tab or 2+ spaces.

$margin

The -m argument can be passed in to add a margin (empty lines) above and below the display box. Default is 0.

Compatibility

It is recommended you use the argument prefixes to call these, but they are optional for backwards compatibility. The old syntax of: display-box "$headers" "$body" 4 2 is still valid.

Inline Script Option

The display-box.sh script can be sourced and called as a function, or a simplified option is the inline script option. Just pipe input to the script. It does still allowing you to pass in arguments to modify the settings.

The syntax of this inline example is:

echo "$multi_line_string" | /path/to/display-box.sh -s 2 -p 2 -d , -m 1

Symbolic Link

To make it more convenient, you may want to create a symlink in your path, such as: /usr/local/bin/display-box or perhaps ~/bin/display-box if you don't have sudo access.

sudo ln -s /path/to/display-box.sh /usr/local/bin/display-box

Then you can just pipe any string to display-box. Example:

echo "
ID,First Name,Last Name,Email

1,Jimmy,Smith,jimmy@example.com
2,Sarah,Jane,sarah@example.com
3,Mark,Jones,mark@example.com
" | display-box -d ,

Usage

Quick example (using the default Tab delimiter)

Deeper dive walk-through of each section:

Step 1)

Include the function

Step 2)

Create two strings that are tab-separated

Step 3)

Optionally define the box style and the padding

Step 4)

Call the display-box function passing in the required $headers and $body variables. Other optional variables are Box Style and Padding

Box Styles

There are several different box styles you can choose between

Type 1 - Single-bars

Type 2 - Double-bars

Type 3 - Double-outer, single-inner

Type 4 - Double-bars without side walls

Type 5 - Double Bars on top and bottom without side walls

Type 6 - Minimal

Examples

There are several example scripts included in the examples directory.

  1. example.sh shows a comma-separated column delimiter inline example.
  2. example-all-styles.sh displays the example in all available styles.
  3. example-tabs.sh shows an example of sourcing the script to make the display-box function available for repeated calls. It uses the default tab delimiter.
  4. example-whitespace.sh shows how you can maintain a data format that mirrors the display-box columns using only spaces.

Future enhancements under consideration

  1. Add Colors (borders, background, separate for header/body)
  2. Option to add drop-shadow
  3. Separate padding-left from padding-right
  4. Possibly add different padding for header vs body
  5. Option to add bars between each line in body (like Excel)
  6. Add style for solid block bars (https://en.wikipedia.org/wiki/Block_Elements)
  7. Add style with double-bars for header and single bars for body
  8. Add style with double-bars for header and outside but single bars for body columns
  9. Add style with only horizontal bars
  10. Option to add space between columns (as opposed to padding) for styles without vertical bars

Versions

Version Number Date Released Description of changes
v1.0 30 Apr 2020 Initial version
v1.0.1 22 Mar 2022 Minor bug fix
v1.1 13 May 2023 Added custom delimiter and inline functionality
v1.2 30 May 2024 Added margin and trimmed input text for new lines