Skip to content

Instantly share code, notes, and snippets.

@Hayao0819
Created July 29, 2021 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hayao0819/5f628faddc6b93a1b4eb7e4b92de2678 to your computer and use it in GitHub Desktop.
Save Hayao0819/5f628faddc6b93a1b4eb7e4b92de2678 to your computer and use it in GitHub Desktop.
エスケープシーケンスを簡単に扱える関数とスクリプト
#!/usr/bin/env bash
# text [-b/-c color/-g color/-f/-l/]
# -b: 太字, -f: 点滅, -l: 下線, -n: リセット,
text() {
local OPTIND OPTARG _arg _textcolor _decotypes="" _bgcolor
while getopts "c:bflng:" _arg; do
case "${_arg}" in
c | g)
case "${OPTARG}" in
"black" ) [[ "${_arg}" = "c" ]] && _textcolor="30" || _bgcolor="40";;
"red" ) [[ "${_arg}" = "c" ]] && _textcolor="31" || _bgcolor="41";;
"green" ) [[ "${_arg}" = "c" ]] && _textcolor="32" || _bgcolor="42";;
"yellow" ) [[ "${_arg}" = "c" ]] && _textcolor="33" || _bgcolor="43";;
"blue" ) [[ "${_arg}" = "c" ]] && _textcolor="34" || _bgcolor="44";;
"magenta") [[ "${_arg}" = "c" ]] && _textcolor="35" || _bgcolor="45";;
"cyan" ) [[ "${_arg}" = "c" ]] && _textcolor="36" || _bgcolor="46";;
"white" ) [[ "${_arg}" = "c" ]] && _textcolor="37" || _bgcolor="47";;
* ) return 1 ;;
esac
;;
b) _decotypes="${_decotypes};1" ;;
f) _decotypes="${_decotypes};5" ;;
l) _decotypes="${_decotypes};4" ;;
n) _decotypes="${_decotypes};0" ;;
*) msg_error "Wrong use of text function" ;;
esac
done
shift "$((OPTIND - 1))"
echo -ne "\e[$([[ -v _textcolor ]] && echo -n ";${_textcolor}"; [[ -v _decotypes ]] && echo -n "${_decotypes}"; [[ -v _bgcolor ]] && echo -n ";${_bgcolor}")m${*}\e[m"
}
text "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment