Awesome
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 and run ./test
to preview.
Versioning
Read tags for verions. The versions follow the rules of Semantic Versioning 2.0.0.
Installation
# 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.
# 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.
# 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.
source <path>/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.
Color Map
Generate Color Map
First, generate a colors.bash file to your project.
# 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.
shell-general-colors --map -- colors.bash
The generated file "colors.bash" will contain below codes.
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.
source <path>/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.
Advanced Usage
Change generated file path
shell-general-colors "~/colors.bash"
# $output generated
Force write
The script checks existed file by default. You can force write file with -y
option.
shell-general-colors -y
# $output generated
Set variable prefix
If -p=<prefix>
set, Add prefix <prefix>
to each name of exported variables.
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=<escaped_suffix>
set, export escaped variables instead of general variables. And add suffix <escaped_suffix>
to each name of escaped variables.
shell-general-colors -e _ESC
# BLACK_ESC, RED_ESC, BOLD_BLACK_ESC ...
You can use escaped variables with here documents. For example,
cat <<EOF
this is ${RED_ESC}red${RESET_ALL_ESC}.
this is ${YELLOW_ESC}yellow${RESET_ALL_ESC}.
EOF
Export both general variables and escaped variables
shell-general-colors -a -e _ESC
# BLACK, BLACK_ESC, RED, RED_ESC, BOLD_BLACK, BOLD_BLACK_ESC ...
Suggestion, Bug Reporting, Contributing
Before opening new Issue/Discussion/PR and posting any comments, please read Contributing Guidelines.
Copyright and License
Copyright 2019-2023 ADoyle (adoyle.h@gmail.com). Some Rights Reserved. The project is licensed under the BSD 3-clause License.
See the LICENSE file for the specific language governing permissions and limitations under the License.
See the NOTICE file distributed with this work for additional information regarding copyright ownership.
References
- Wikipedia - ANSI escape code
- Stackoverflow - List of ANSI color escape sequences
- FLOZz' MISC » bash:tip_colors_and_formatting
- ASCII Table - ANSI Escape sequences
- ansi codes
- vt100.net - ANSI Control Functions Summary
- JAFROG'S DEV BLOG - Colors In Terminal
Other Projects
- lobash: A modern, safe, powerful utility/library for Bash script development.
- one.bash: An elegant framework to manage commands, completions, dotfiles for bash players.
- Other shell projects created by me.