#!/bin/bash
#
# Example of outputting coloured text
# Sub Routines :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
# Returns a color code for the given foreground/background colors
# This code is echoed to the terminal before outputing text in
# order to generate a colored output.
#
# string foreground color name. Optional if no background provided.
# Defaults to "Default" which uses the system default
# string background color name. Optional. Defaults to $color_background
# which is set based on the current terminal background
# returns a string
function Color () {
local foreground=$1
local background=$2
if [ "$foreground" == "" ]; then foreground="Default"; fi
if [ "$background" == "" ]; then background="$color_background"; fi
if [ "$foreground" == "Contrast" ]; then
foreground=$(ContrastForeground ${background})
fi
local colorString='\033['
# Foreground Colours
case "$foreground" in
"Default") colorString='\033[0;39m';;
"Black" ) colorString='\033[0;30m';;
"DarkRed" ) colorString='\033[0;31m';;
"DarkGreen" ) colorString='\033[0;32m';;
"DarkYellow" ) colorString='\033[0;33m';;
"DarkBlue" ) colorString='\033[0;34m';;
"DarkMagenta" ) colorString='\033[0;35m';;
"DarkCyan" ) colorString='\033[0;36m';;
"Gray" ) colorString='\033[0;37m';;
"DarkGray" ) colorString='\033[1;90m';;
"Red" ) colorString='\033[1;91m';;
"Green" ) colorString='\033[1;92m';;
"Yellow" ) colorString='\033[1;93m';;
"Blue" ) colorString='\033[1;94m';;
"Magenta" ) colorString='\033[1;95m';;
"Cyan" ) colorString='\033[1;96m';;
"White" ) colorString='\033[1;97m';;
*) colorString='\033[0;39m';;
esac
# Background Colours
case "$background" in
"Default" ) colorString="${colorString}\033[49m";;
"Black" ) colorString="${colorString}\033[40m";;
"DarkRed" ) colorString="${colorString}\033[41m";;
"DarkGreen" ) colorString="${colorString}\033[42m";;
"DarkYellow" ) colorString="${colorString}\033[43m";;
"DarkBlue" ) colorString="${colorString}\033[44m";;
"DarkMagenta" ) colorString="${colorString}\033[45m";;
"DarkCyan" ) colorString="${colorString}\033[46m";;
"Gray" ) colorString="${colorString}\033[47m";;
"DarkGray" ) colorString="${colorString}\033[100m";;
"Red" ) colorString="${colorString}\033[101m";;
"Green" ) colorString="${colorString}\033[102m";;
"Yellow" ) colorString="${colorString}\033[103m";;
"Blue" ) colorString="${colorString}\033[104m";;
"Magenta" ) colorString="${colorString}\033[105m";;
"Cyan" ) colorString="${colorString}\033[106m";;
"White" ) colorString="${colorString}\033[107m";;
*) colorString="${colorString}\033[49m";;
esac
echo "${colorString}"
}
# Returns the name of a color that will providing a contrasting foreground
# color for the given background color. This function assumes $darkmode has
# been set globally.
#
# string background color name.
# returns a string representing a contrasting foreground colour name
function ContrastForeground () {
local color=$1
if [ "$color" == "" ]; then color="Default"; fi
if [ "$darkmode" == "true" ]; then
case "$color" in
"Default" ) echo "White";;
"Black" ) echo "White";;
"DarkRed" ) echo "White";;
"DarkGreen" ) echo "White";;
"DarkYellow" ) echo "White";;
"DarkBlue" ) echo "White";;
"DarkMagenta" ) echo "White";;
"DarkCyan" ) echo "White";;
"Gray" ) echo "Black";;
"DarkGray" ) echo "White";;
"Red" ) echo "White";;
"Green" ) echo "White";;
"Yellow" ) echo "Black";;
"Blue" ) echo "White";;
"Magenta" ) echo "White";;
"Cyan" ) echo "Black";;
"White" ) echo "Black";;
*) echo "White";;
esac
else
case "$color" in
"Default" ) echo "Black";;
"Black" ) echo "White";;
"DarkRed" ) echo "White";;
"DarkGreen" ) echo "White";;
"DarkYellow" ) echo "White";;
"DarkBlue" ) echo "White";;
"DarkMagenta" ) echo "White";;
"DarkCyan" ) echo "White";;
"Gray" ) echo "Black";;
"DarkGray" ) echo "White";;
"Red" ) echo "White";;
"Green" ) echo "Black";;
"Yellow" ) echo "Black";;
"Blue" ) echo "White";;
"Magenta" ) echo "White";;
"Cyan" ) echo "Black";;
"White" ) echo "Black";;
*) echo "White";;
esac
fi
echo "${colorString}"
}
# Gets the terminal background color. It's a very naive guess
# returns an RGB triplet, values from 0 - 64K
function getBackground () {
if [[ $OSTYPE == 'darwin'* ]]; then
osascript -e \
'tell application "Terminal"
get background color of selected tab of window 1
end tell'
else
# See https://github.com/rocky/shell-term-background/blob/master/term-background.bash
# for a comprehensive way to test for background colour. For now we're just going to
# assume that non-macOS terminals have a black background.
echo "0,0,0" # we're making assumptions here
fi
}
# Determines whether or not the current terminal is in dark mode (dark background, light text)
# returns "true" if running in dark mode; false otherwise
function isDarkMode () {
local bgColor=$(getBackground)
IFS=','; colors=($bgColor); IFS=' ';
# Is the background more or less dark?
if [ ${colors[0]} -lt 20000 ] && [ ${colors[1]} -lt 20000 ] && [ ${colors[2]} -lt 20000 ]; then
echo "true"
else
echo "false"
fi
}
# Outputs a line, including linefeed, to the terminal using the given foreground / background
# colors
#
# string The text to output. Optional if no foreground provided. Default is just a line feed.
# string Foreground color name. Optional if no background provided. Defaults to "Default" which
# uses the system default
# string Background color name. Optional. Defaults to $color_background which is set based on the
# current terminal background
function WriteLine () {
local resetColor='\033[0m'
local str=$1
local forecolor=$2
local backcolor=$3
if [ "$str" == "" ]; then
printf "\n"
return;
fi
# Note the use of the format placeholder %s. This allows us to pass "--" as strings without error
if [ "$useColor" == "true" ]; then
local colorString=$(Color ${forecolor} ${backcolor})
printf "${colorString}%s${resetColor}\n" "${str}"
else
printf "%s\n" "${str}"
fi
}
# Outputs a line without a linefeed to the terminal using the given foreground / background colors
#
# string The text to output. Optional if no foreground provided. Default is just a line feed.
# string Foreground color name. Optional if no background provided. Defaults to "Default" which
# uses the system default
# string Background color name. Optional. Defaults to $color_background which is set based on the
# current terminal background
function Write () {
local resetColor="\033[0m"
local forecolor=$1
local backcolor=$2
local str=$3
if [ "$str" == "" ]; then
return;
fi
# Note the use of the format placeholder %s. This allows us to pass "--" as strings without error
if [ "$useColor" == "true" ]; then
local colorString=$(Color ${forecolor} ${backcolor})
printf "${colorString}%s${resetColor}" "${str}"
else
printf "%s" "$str"
fi
}
useColor="true"
darkmode=$(isDarkMode)
# Setup some predefined colours. Note that we can't reliably determine the back
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
color.zip (1个子文件)
color.sh 13KB
共 1 条
- 1
寒冰屋
- 粉丝: 1004
- 资源: 575
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0