Last active
January 8, 2020 07:58
-
-
Save yun-cloud/0a49c28b7e89a03e9552dd1a28b6c414 to your computer and use it in GitHub Desktop.
some common used function, to show the infomation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
BLACK="$(tput setaf 0; tput bold)" | |
RED="$(tput setaf 1; tput bold)" | |
GREEN="$(tput setaf 2; tput bold)" | |
YELLOW="$(tput setaf 3; tput bold)" | |
BLUE="$(tput setaf 4; tput bold)" | |
MAGENTA="$(tput setaf 5; tput bold)" | |
CYAN="$(tput setaf 6; tput bold)" | |
WHITE="$(tput setaf 7; tput bold)" | |
BOLD="$(tput bold)" | |
RESET="$(tput sgr0)" | |
# === info === | |
BLUE="$(tput setaf 4; tput bold)" | |
WHITE="$(tput setaf 7; tput bold)" | |
RESET="$(tput sgr0)" | |
info() { | |
for msg do | |
echo "${BLUE}>>>${WHITE} $msg ${RESET}" | |
done | |
} | |
# === success === | |
GREEN="$(tput setaf 2; tput bold)" | |
WHITE="$(tput setaf 7; tput bold)" | |
RESET="$(tput sgr0)" | |
success() { | |
for msg do | |
echo "${GREEN}>>>${WHITE} $msg ${RESET}" | |
done | |
} | |
# === warning === | |
YELLOW="$(tput setaf 3; tput bold)" | |
WHITE="$(tput setaf 7; tput bold)" | |
RESET="$(tput sgr0)" | |
warning() { | |
for msg do | |
echo "${YELLOW}>>>${WHITE} $msg ${RESET}" | |
done | |
} | |
# === danger === | |
RED="$(tput setaf 1; tput bold)" | |
WHITE="$(tput setaf 7; tput bold)" | |
RESET="$(tput sgr0)" | |
danger() { | |
for msg do | |
echo "${RED}>>>${WHITE} $msg ${RESET}" | |
done | |
} | |
# === section === | |
WHITE="$(tput setaf 7; tput bold)" | |
RESET="$(tput sgr0)" | |
section() { | |
echo "=== ${WHITE}$1${RESET} ===" | |
} | |
# =============== | |
# === big box === | |
# =============== | |
WHITE="$(tput setaf 7; tput bold)" | |
RESET="$(tput sgr0)" | |
bbox() { | |
max=$( maxlen $@ ) | |
n=$(( $max + 8 )) | |
for i in `seq 1 $n`; do printf "="; done | |
echo | |
for arg do | |
difference=$(( $max - ${#arg} )) | |
half=$(( $difference / 2 )) | |
odd=$(( $difference % 2 )) | |
printf "=== " | |
for i in `seq 1 $half`; do printf " "; done | |
printf "${WHITE}$arg${RESET}" | |
for i in `seq 1 $half`; do printf " "; done | |
[[ $odd -eq 1 ]] && printf " " | |
printf " ===" | |
echo | |
done | |
for i in `seq 1 $n`; do printf "="; done | |
echo | |
} | |
maxlen() { | |
max=0 | |
for arg do | |
len=${#arg} | |
if [[ $len -gt $max ]]; then | |
max=$len | |
fi | |
done | |
echo "$max" | |
} | |
# https://stackoverflow.com/a/1885534 | |
ask_confirm() { | |
msg=$1 | |
read -p "$msg(y/N) " -n 1 -r | |
echo | |
echo "\$REPLY = $REPLY" | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
true | |
else | |
false | |
fi | |
} | |
# if ask_confirm "hello?"; then | |
# echo "confirm" | |
# else | |
# echo "failed" | |
# fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment