Home

Awesome

ANSI Code Generator

This bash script will generate the proper ANSI escape sequences to move the cursor around the screen, make text bold, add colors and do much more. It is designed to help you colorize words and bits of text.

If it helps, you can think of it as a curses / ncurses library for bash, or a tool that helps you using tricks from DOS's ANSI.SYS. Or you might consider this to be your magic highlighter that has always been missing from bash.

Demonstrations

Colors

--color-table

--color-codes

Installation

Download ansi and put it somewhere in your path. Make sure that it is executable (chmod a+x ansi works well). Bash will need to be present, but it is often installed by default. No external commands are used; this only uses bash's built-in features.

# Download
curl -OL git.io/ansi

# Make executable
chmod 755 ansi

# Copy to somewhere in your path
sudo mv ansi /usr/local/bin/

Not all features will work with all terminals. Your terminal determines if particular codes work. You can override the terminal detection logic and force ansi to output color codes with the environment variable ANSI_FORCE_SUPPORT set to any non-empty value.

BPM Installation

This package is also published as a BPM library. This means it can be loaded into other scripts or installed easily. The ansi library does not need any dependencies from BPM in order to work.

To use this as a standalone command, first install BPM and then run: bpm install ansi

To use this as a library, list ansi as a dependency in your bpm.ini file and include these lines at the top of your shell script:

. bpm
bpm::include ansi

Once loaded into the environment, the list of library functions below are available for your use.

Upgrading Issues

The newer version of ansi has reversed an earlier decision about newlines at the end. Previously, calling ansi test would write "test" to the screen without a newline and ansi -n test would add a newline. As of August 2018, this has switched and ansi test will write out "test" with a newline. The -n flag is also switched. Sorry for the inconvenience that the earlier decision has caused and future confusion due to this flag switching. The goal is to align more closely with echo.

Usage

ansi [OPTIONS] [TEXT TO DISPLAY]

The OPTIONS are numerous and are detailed below. You can specify as many as you like. Option processing stops at the first unknown option and at --. Options are applied in the order specified on the command line. Colors are reset and attributes are restored to default unless --no-restore is used.

Examples

It often helps when one can see how to use a new tool, so here's how this library can be used.

Command-Line

# Write "Tests pass" in green on its own line
ansi --green --newline "Tests pass"

# Change the terminal's title to the working directory and
# do not write anything to the terminal.
ansi --title="$(pwd)" --no-newline

# Reset the terminal colors, reset to the default font, move the cursor
# to row 1 column 1, and show the cursor if it was previously hidden.
# This long line is the same as the --reset option.
ansi --erase-display=2 --position=1,1 --show-cursor \
    --reset-font --reset-color

# Find out how many lines the terminal can display
ansi --report-window-chars | cut -d , -f 1

Need more? Check out the examples/ folder.

Command-Line Options

When using the ansi command from a shell script or a prompt, there are a plethora of different options you can use. The syntax for using these options is as follows:

ansi [OPTIONS] [TEXT TO DISPLAY]

All options must be placed first. See the Examples section for more information. You can also see the entire list of options by running ansi --help.

Display Manipulation

The short version of these options comes from the command they are implementing.

Cursor

The short version of these options comes from the command they are implementing.

Text (Attributes)

All of these options will automatically reset to normal text unless --no-reset is used. Below, the codes are grouped into similar functionality and the flag to disable that attribute is listed with the group it controls, so you can correlate the flags more easily.

Text (Foreground)

All of these options will automatically reset to the default color unless --no-reset is used. To preview the colors, use --color-table.

Text (Background)

All of these options will automatically reset to the default color unless --no-reset is used. To preview the colors, use --color-table.

Text (Reset)

These options force a reset of colors. This is useful if you used --no-reset or are correcting the appearance of a misbehaving terminal.

Reporting

All of these commands send a special code to the terminal. The terminal responds as though someone typed something very fast. In order for these to work, ansi must read from stdin directly. This won't work if you are piping in a file or replace stdin in another way.

The ANSI codes are written to stdout in order that the terminal might respond immediately. The result of the report is written to stdout.

Miscellaneous

Library Functions

If you decide to leverage ansi as a Bash library and you load its functions into your environment, you gain access to the following functions. You will notice that they, for the most part, mirror the option names of the command-line program. These functions only do their one task and do not reset the terminal when done. Any exceptions to this are clearly called out.

In order to source ansi into the environment you will have to use one of a few different methods.

Loading via BPM, after you add ansi to your dependencies in your bpm.ini file.

. bpm
bpm::include ansi

Loading when ansi is installed manually into your path somewhere. Coincidentally, this also works when you use BPM to install the tool globally.

. ansi

Loading when you include ansi in your project.

. path/to/the/file/ansi

When using this to change the color or perform terminal manipulation, make sure to send the reset codes.

ansi::green
echo "This is green text"

# You must manually reset the colors
ansi::resetForeground

All of the functions write to stdout except ansi::report. That one uses stderr to query the terminal and its result will go into the ANSI_REPORT environment variable.

None of the functions care if the terminal supports ANSI unless otherwise noted. They will happily write out ANSI codes even if the terminal will not recognize them.

Display Manipulation

The short version of these options comes from the command they are implementing.

Cursor

The short version of these options comes from the command they are implementing.

Text (Attributes)

All of these options will automatically reset to normal text unless --no-reset is used. Below, the codes are grouped into similar functionality and the flag to disable that attribute is listed with the group it controls, so you can correlate the flags more easily.

Text (Foreground)

All of these options will automatically reset to the default color unless --no-reset is used. To preview the colors, use --color-table.

Text (Background)

All of these options will automatically reset to the default color unless --no-reset is used. To preview the colors, use --color-table.

Text (Reset)

These options force a reset of colors. This is useful if you used --no-reset or are correcting the appearance of a misbehaving terminal.

Reporting

All of these commands send a special code to the terminal. The terminal responds as though someone typed something very fast. In order for these to work, ansi must read from stdin directly. This won't work if you are piping in a file or replace stdin in another way.

The ANSI codes are written to stdout in order that the terminal might respond immediately. The result of the report is written to stdout.

Miscellaneous

When setting the icon and the title, make sure to quote the value correctly so spaces are sent within the argument.

# This will not work and instead just sets the title to "Tests"
ansi::title Tests are running

# This will work because everything is sent as a single argument
ansi::title "Tests are running"

License

This project is licensed under a MIT style license with an additional non-advertising clause. See LICENSE.md for more information.