An open API service indexing awesome lists of open source software.

https://github.com/adoyle-h/shell-general-colors

A script to generate sets of shell variables (ANSI escape sequences) to control text color, boldness, underlining, blinking and other effects.
https://github.com/adoyle-h/shell-general-colors

ansi bash color shell shell-color terminal

Last synced: about 1 year ago
JSON representation

A script to generate sets of shell variables (ANSI escape sequences) to control text color, boldness, underlining, blinking and other effects.

Awesome Lists containing this project

README

          

# Shell General Colors

A script to generate sets of shell variables (ANSI escape sequences) to control text color, boldness, underlining, blinking and other effects.

Support general colors: `BLACK` `RED` `GREEN` `YELLOW` `BLUE` `PURPLE` `CYAN` `WHITE` `GREY`.

Not support custom RGB color. [ansi][] is a good choice.

The difference between ansi and Shell General Colors is usability.
Shell General Colors provides simple variables which aim to be fast in runtime while ansi provides flexible functions.

## Preview

See [Usage](#usage) and run `./test` to preview.

## Versioning

Read [tags][] for verions.
The versions follow the rules of [Semantic Versioning 2.0.0](http://semver.org/spec/v2.0.0.html).

## Installation

```sh
# Clone this repo
git clone --depth 1 https://github.com/adoyle-h/shell-general-colors.git
# Copy it to somewhere in your path
sudo ln -s "$PWD/generate" /usr/local/bin/shell-general-colors
```

## Usage

There are two ways to generate colors: list or map.

### Color List

#### Generate Color List

First, generate a colors.bash file to your project.

```sh
# cd to your project
# "shell-general-colors -h" to get usage
shell-general-colors
# Generated file: colors.bash
```

The generated file "colors.bash" will contain below codes.

```sh
# General Foreground Colors
BLACK='\e[30m'
RED='\e[31m'
GREEN='\e[32m'
YELLOW='\e[33m'
BLUE='\e[34m'
PURPLE='\e[35m'
CYAN='\e[36m'
WHITE='\e[37m'
GREY='\e[90m'

# ...

# RESET
RESET_FG='\e[39m'
RESET_BG='\e[49m'
RESET_ALL='\e[0m'
```

#### Use Color List

Then source the colors.bash file and use these variables directly.

```sh
source /colors.bash

echo -e "this is ${RED}red${RESET_ALL}. this is ${YELLOW}yellow${RESET_ALL}."
printf 'this is %bblue%b.' "${BLUE}" "${RESET_ALL}"
```

If you want to use color variables with [here documents][]. Use [escaped variables](#export-escaped-variables).

### Color Map

#### Generate Color Map

First, generate a colors.bash file to your project.

```sh
# cd to your project
# "shell-general-colors -h" to get usage
shell-general-colors --map
# Generated file: colors.bash
```

**Notice**: When use `--map` option and specific output, you must pass `--` before output path.

```sh
shell-general-colors --map -- colors.bash
```

The generated file "colors.bash" will contain below codes.

```sh
declare -g -A colors=(

# General Foreground Colors
[BLACK]='\e[30m'
[RED]='\e[31m'
[GREEN]='\e[32m'
[YELLOW]='\e[33m'
[BLUE]='\e[34m'
[PURPLE]='\e[35m'
[CYAN]='\e[36m'
[WHITE]='\e[37m'
[GREY]='\e[90m'

# ...

# RESET
[RESET_FG]='\e[39m'
[RESET_BG]='\e[49m'
[RESET_ALL]='\e[0m'
)
```

#### Use Color Map

Then source the colors.bash file and use these variables directly.

```sh
source /colors.bash

RESET_ALL=${colors[RESET_ALL]}
color1=RED
color2=YELLOW
color3=BLUE

echo -e "this is ${colors[color1]}red${RESET_ALL}. this is ${colors[color2]}yellow${RESET_ALL}."
printf 'this is %bblue%b.' "${colors[color3]}" "${RESET_ALL}"
```

If you want to use color variables with [here documents][]. Use [escaped variables](#export-escaped-variables).

## Advanced Usage

### Change generated file path

```sh
shell-general-colors "~/colors.bash"
# $output generated
```

### Force write

The script checks existed file by default. You can force write file with `-y` option.

```sh
shell-general-colors -y
# $output generated
```

### Set variable prefix

If `-p=` set, Add prefix `` to each name of exported variables.

```sh
shell-general-colors -p C_
# C_BLACK, C_RED, C_BOLD_BLACK ...
```

### Export escaped variables

If `-e` set, export escaped variables instead of general variables.

If `-e=` set, export escaped variables instead of general variables. And add suffix `` to each name of escaped variables.

```sh
shell-general-colors -e _ESC
# BLACK_ESC, RED_ESC, BOLD_BLACK_ESC ...
```

You can use escaped variables with [here documents][]. For example,

```sh
cat <

[issue]: https://github.com/adoyle-h/shell-general-colors/issues
[tags]: https://github.com/adoyle-h/shell-general-colors/tags
[LICENSE]: ./LICENSE
[NOTICE]: ./NOTICE
[ansi]: https://github.com/fidian/ansi
[here documents]: http://tldp.org/LDP/abs/html/here-docs.html