Skip to content

npnicholson/colorize.sh

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

colorize.sh

Shell Script for replacing editor safe color tags with ANSI Escape Sequences using sed

Replaces ASCII friendly tags (as defined below) from piped input with their ANSI Escape Code equivalent. To use colorize.sh, place these tags (listed below under ANSI Functions) in a text file and pipe it to this program using cat (see examples). The tags within the pipe will be converted to their ANSI Escape Code equivalents and output to stdout.

Usage

pipe | colorize.sh [-hqvse] [| pipe out]

  • -h : Show this help message, then exit
  • -q : Supress warning and error messages
  • -v : Show the version number, then exit
  • -s : Set the tag start charactor (default is '{'. Can also be set using export CLR_START="{"
  • -e : Set the tag end charactor (default is '}'. Can also be set using export CLR_END="}"

Tags

Tags should be placed in the piped data. Each valid tag will be replaced with an ANSI Escape Code, depending on the operation and argument provided, as shown below. The specific starting and ending strings for the tags ('{' and '}' by default) can be changed using the -s and -e arguments.

{op[:arg]}

  • op : ANSI operation to execute
  • arg : Argument for the selected ANSI operation, if required

ANSI Functions

Each of the following ANSI functions can be invoked using the provided tags

Basic Operations

  • {c} Clear : Clears all ANSI Formatting (ESC[0m)
  • {e} Escape : Substitutes the ANSI Escape [dec 27 / hex 0x1B / oct 033]

Color Operations:

  • {f:_} Foreground Color

    • {f:k} Black (ESC[30m)
    • {f:r} Red (ESC[31m)
    • {f:g} Green (ESC[32m)
    • {f:y} Yellow (ESC[33m)
    • {f:b} Blue (ESC[34m)
    • {f:m} Magenta (ESC[35m)
    • {f:c} Cyan (ESC[36m)
    • {f:w} White (ESC[37m)
    • {f:bk} Bright Black (ESC[90m)
    • {f:br} Bright Red (ESC[91m)
    • {f:bg} Bright Green (ESC[92m)
    • {f:by} Bright Yellow (ESC[93m)
    • {f:bb} Bright Blue (ESC[94m)
    • {f:bm} Bright Magenta (ESC[95m)
    • {f:bc} Bright Cyan (ESC[96m)
    • {f:bw} Bright White (ESC[97m)
    • {f:#} Select (ESC[38;5;#m)
    • {f:R:G:B} RGB (ESC[38;2;R;G;Bm)
    • {f} Clear (ESC[39m)
  • {b:_} Background Color

    • {b:k} Black (ESC[40m)
    • {b:r} Red (ESC[41m)
    • {b:g} Green (ESC[42m)
    • {b:y} Yellow (ESC[43m)
    • {b:b} Blue (ESC[44m)
    • {b:m} Magenta (ESC[45m)
    • {b:c} Cyan (ESC[46m)
    • {b:w} White (ESC[47m)
    • {b:bk} Bright Black (ESC[100m)
    • {b:br} Bright Red (ESC[101m)
    • {b:bg} Bright Green (ESC[102m)
    • {b:by} Bright Yellow (ESC[103m)
    • {b:bb} Bright Blue (ESC[104m)
    • {b:bm} Bright Magenta (ESC[105m)
    • {b:bc} Bright Cyan (ESC[106m)
    • {b:bw} Bright White (ESC[107m)
    • {b:#} Select (ESC[48;5;#m)
    • {b:R:G:B} RGB (ESC[48;2;R;G;Bm)
    • {b} Clear (ESC[49m)

Text Modifiers

  • {r:1/0} Reverse (ESC[7m / ESC[27m)
  • {i:1/0} Italic (ESC[3m / ESC[23m)
  • {u:1/0} Underline (ESC[4m / ESC[24m)
  • {x:1/0} Strike (ESC[9m / ESC[29m)
  • {s:1/0} Strong (ESC[1m / ESC[22m)
  • {w:1/0} Weak (ESC[2m / ESC[22m)

Note that text can only be Strong (bold) or Weak (light) or neither (normal). Clearing Strong or Weak will clear the other as well.

Text Modifiers are enabled or diabled with a 1 or a 0 as their argument. For example: {i:1} would activate italics and {x:0} would disable strike

Example

Pipe text with tags into colorize.sh to produce a colored output:

printf "Something {f:b}blue{f} and something {u:1}underlined{u:0}\n" | colorize.sh

cat color_tagged_file | colorize.sh

Output from colorize.sh can also be piped elsewhere:

printf "{f:r}Something red and {u:1}underlined{c}\n" | colorize.sh | less -r

Issues

  • colorize.sh: command not found : Make sure you are using the /full/path/to/colorize.sh or place colorize.sh within your path. For most systems, try placing colorize.sh under /usr/bin or /usr/local/bin. If you prefer, you can also link colorize.sh to one of these folders using something like ln -s /full/path/to/colorize.sh /usr/bin/colorize.sh

References

For more information on ANSI Codes, see ANSI Escape Codes Wiki