Skip to content

loopmode/bash-print

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

bash-print

Helper for printing messages in colored blocks.

Usage:

Include the script in your bash script, for example using source. Then, use print_info, print_success or print_error.

  • print_info: Prints message in a blue block
  • print_success: Prints message in a blue block
  • print_error: Prints message in a red block
  • print_block: Prints message in a colored block. You must pass an ansi color as last argument

By default, a block starts and ends with a blank line. When printing multiple blocks, pass + after the message to avoid duplicate linebreaks.

Example

Using predefined colors:

source print.sh
print_error "Invalid target \"$TARGET\"" + 
print_info "Usage: watch <client|server|all>"

Using print_block and custom ansi color codes:

source print.sh
print_block "Invalid target \"$TARGET\"" + "\033[31m"
print_block "Usage: watch <client|server|all>" + "\e[46m"

Known limitations

$JOIN_PARAM

Even a single quoted string passed to the function is treated as an array of many strings separated by spaces. Thus, the join mechanism that uses the magic rule "Last argument is a +" gets confused and fails when printing messages that themselves end with a +. In that case, the plus is simply not displayed and instead the trailing linebreak is omitted on the block.

# both are treated the same way
print_info "foo +"
print_info "foo" +

Normally, this is not an issue at all, but you can still change script to something else if needed (Change the JOIN_PARAM="+" line at the top).

Including the script

If source print.sh and source ./print.sh fail on your system, you can go the more reliable but also verbose way using the directory of the running script:

# Assumes that print.sh is in the same directory as the running script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $DIR/print.sh

print_success "It works!"

About

Bash helper for displaying messages in colored blocks

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages