├── LICENSE ├── README.md ├── clc └── examples ├── prompt.png ├── stacking.gif └── usage.gif /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 betafcc 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # clc 2 | Tiny bash utility for coloring terminal output and prompt 3 | 4 | ![](./examples/usage.gif) 5 | 6 | The motivating use case was easily stacking styles, which is not possible to do with other tools like [node's chalk](https://github.com/chalk/chalk-cli): 7 | 8 | ![](./examples/stacking.gif) 9 | 10 | Useful for templating colorful prompts (use `-e|--escape [zsh|bash]` for this) 11 | 12 | for bash: `PS1=$(clc -e bash ']>$ >')` 13 | 14 | for zsh: `PS1=$(clc -e zsh ']>$ >')` 15 | 16 | 17 | ![](./examples/prompt.png) 18 | 19 | ## Try it 20 | 21 | ```bash 22 | bash <(curl -s https://raw.githubusercontent.com/betafcc/clc/master/clc) '' 23 | ``` 24 | 25 | ## Install 26 | 27 | ```bash 28 | dest='/usr/local/bin/clc'; curl -s https://raw.githubusercontent.com/betafcc/clc/master/clc | sudo tee "${dest}" > /dev/null && sudo chmod +x "${dest}" 29 | ``` 30 | 31 | Then use: 32 | 33 | ```bash 34 | clc '' 35 | ``` 36 | 37 | ## More 38 | 39 | 40 | ### Use foreground colors: 41 | 42 | ```bash 43 | clc '' 44 | clc '>' 45 | clc '' 46 | clc '' 47 | clc '<#f66: hello>' 48 | clc '<#66fa73: hello>' 49 | ``` 50 | 51 | ### Use background colors: 52 | 53 | ```bash 54 | clc '' 55 | clc '>' 56 | clc '' 57 | clc '' 58 | ``` 59 | 60 | ### Use effects: 61 | 62 | ```bash 63 | clc '' 64 | clc '>' 65 | ``` 66 | 67 | ### Combine commands: 68 | 69 | ```bash 70 | clc '' 71 | ``` 72 | 73 | ### Escape characters with '%': 74 | 75 | ```bash 76 | clc '>' 77 | ``` 78 | 79 | ### Available Colors: 80 | - black 81 | - red 82 | - green 83 | - yellow 84 | - blue 85 | - magenta 86 | - cyan 87 | - white 88 | - rgb <0-255> <0-255> <0-255> 89 | - #000 - #fff 90 | - #000000 - #ffffff 91 | 92 | ### Available effects: 93 | - normal 94 | - bold 95 | - dim 96 | - strike 97 | - italic 98 | - underline 99 | - reverse 100 | - invisible 101 | - blink 102 | 103 | ### Version: 104 | 105 | ```bash 106 | clc -v 107 | clc --version 108 | ``` 109 | -------------------------------------------------------------------------------- /clc: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | clc() { 4 | local open="${open:-\e[%bm}" 5 | local close="${close:-\e[0m}" 6 | 7 | if [ $# -eq 0 ]; then 8 | _clc_parse "" 9 | else 10 | case "${1}" in 11 | -v|--version) echo 'v2.2.0';; 12 | -e|--escape) 13 | shift 14 | case "${1}" in 15 | zsh) shift; open='%%{\e[%bm%%}'; close='%{\e[0m%}';; 16 | bash) shift; open='\[\e[%bm\]'; close='\[\e[0m\]';; 17 | *) shift; open='\001\e[%bm\002'; close='\001\e[0m\002';; 18 | esac 19 | clc "$@" 20 | ;; 21 | *) printf '%b' "$@" | clc 22 | esac 23 | fi 24 | } 25 | 26 | _clc_parse() { 27 | local char 28 | while IFS= read -n 1 -d '' char; do 29 | case "${char}" in 30 | '>') set "${@:1:$#-1}"; printf '%b' "${close}" "${@}";; 31 | '<') set "${@}" "$(_clc_parse_directive)"; printf '%b' "${@:$#-1}";; 32 | '%') IFS= read -n 1 -d '' char; printf '%b' "${char}";; 33 | *) printf '%b' "${char}" ;; 34 | esac 35 | done 36 | } 37 | 38 | _clc_parse_directive() { 39 | local -a directives 40 | local directive 41 | IFS=+ read -r -d ':' -a directives 42 | printf "${open}" $( 43 | for directive in "${directives[@]}"; do 44 | directive=(${directive//\#/rgbhex }) 45 | eval _clc_code_${directive[0]} "${directive[@]:1}" 46 | printf '%b' '\n' 47 | done | paste -sd ';' - 48 | ) 49 | } 50 | 51 | _clc_code_black() { printf '%b' 30; } 52 | _clc_code_red() { printf '%b' 31; } 53 | _clc_code_green() { printf '%b' 32; } 54 | _clc_code_yellow() { printf '%b' 33; } 55 | _clc_code_blue() { printf '%b' 34; } 56 | _clc_code_magenta() { printf '%b' 35; } 57 | _clc_code_cyan() { printf '%b' 36; } 58 | _clc_code_white() { printf '%b' 37; } 59 | _clc_code_rgb() { printf '%b' "38;2;${1};${2};${3}"; } 60 | _clc_code_rgbhex() { eval _clc_code_rgb $(printf '%b' $1 | sed -E -e 's/^(.)(.)(.)$/\1\1\2\2\3\3/;s/.{2}/$((16#&)) /g'); } 61 | 62 | _clc_code_bg() { 63 | local fg="$(_clc_code_"${@}")" 64 | printf '%b' "4${fg:1}" 65 | } 66 | 67 | _clc_code_normal() { printf '%b' 0; } 68 | _clc_code_bold() { printf '%b' 1; } 69 | _clc_code_dim() { printf '%b' 2; } 70 | _clc_code_italic() { printf '%b' 3; } 71 | _clc_code_underline() { printf '%b' 4; } 72 | _clc_code_blink() { printf '%b' 5; } 73 | _clc_code_reverse() { printf '%b' 7; } 74 | _clc_code_invisible() { printf '%b' 8; } 75 | _clc_code_strike() { printf '%b' 9; } 76 | 77 | # run `clc` if not being sorced 78 | # https://stackoverflow.com/a/2684300 79 | if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then 80 | clc "${@}" 81 | fi 82 | -------------------------------------------------------------------------------- /examples/prompt.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/betafcc/clc/d013068df6166d96fb52596a7417c428b92a1fda/examples/prompt.png -------------------------------------------------------------------------------- /examples/stacking.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/betafcc/clc/d013068df6166d96fb52596a7417c428b92a1fda/examples/stacking.gif -------------------------------------------------------------------------------- /examples/usage.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/betafcc/clc/d013068df6166d96fb52596a7417c428b92a1fda/examples/usage.gif --------------------------------------------------------------------------------