-
Notifications
You must be signed in to change notification settings - Fork 966
/
prompt-colors.sh
114 lines (99 loc) · 2.97 KB
/
prompt-colors.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env bash
# prompt-colors.sh
#
# source this file to get color definitions
# are also printed to STDERR.
# bash/zsh cross compatibility notes:
# - using colors modules to set colors in zsh, please autoload it
# - Dim colors, Intense Black not supported in zsh
define_color_names() {
ColorNames=( Black Red Green Yellow Blue Magenta Cyan White )
FgColors=( 30 31 32 33 34 35 36 37 )
BgColors=( 40 41 42 43 44 45 46 47 )
local AttrNorm=0
local AttrBright=1
local AttrDim=2
local AttrUnder=4
local AttrBlink=5
local AttrRev=7
local AttrHide=8
# define "BoldCOLOR", "BrightCOLOR", and "DimCOLOR" names
# _map_colors ATTRNAME ATTRVALUE
#
# Defines three names for every color, attribute combintaion:
# {ATTRNAME}{COLORNAME}
# {ATTRNAME}{COLORNAME}Fg
# {ATTRNAME}{COLORNAME}Bg
#
# Example: BoldRed, BoldRedFg, BoldRedBg
_map_colors() {
local x=0
local attrname="${1}"
local attrcode="${2}"
while (( x < 8 )) ; do
local colorname="${ColorNames[@]:$x:1}"
local fgcolorcode="${FgColors[@]:$x:1}"
local bgcolorcode="${BgColors[@]:$x:1}"
longcolorname="${attrname}${colorname}"
if [ -n "$ZSH_VERSION" ]; then
# zsh
lowercolorname=$(echo $colorname | tr '[A-Z]' '[a-z]')
_def_color_zsh "${longcolorname}" "${attrcode}" "${lowercolorname}" "fg"
_def_color_zsh "${longcolorname}Fg" "${attrcode}" "${lowercolorname}" "fg"
_def_color_zsh "${longcolorname}Bg" "${attrcode}" "${lowercolorname}" "bg"
else
# bash
_def_color "${longcolorname}" "${attrcode}" "${fgcolorcode}"
_def_color "${longcolorname}Fg" "${attrcode}" "${fgcolorcode}"
_def_color "${longcolorname}Bg" "${attrcode}" "${bgcolorcode}"
fi
(( x++ ))
done
}
# _term_color [ N | N M ]
_term_color() {
local cv
if [[ "${#}" -gt 1 ]]; then
cv="${1};${2}"
else
cv="${1}"
fi
echo "\[\033[${cv}m\]"
}
# def_color NAME ATTRCODE COLORCODE
_def_color() {
local def="${1}=\"\`_term_color ${2} ${3}\`\""
eval "${def}"
}
# def_color_zsh NAME ATTRCODE COLORNAME FG|BG
_def_color_zsh() {
if [ "${3}" = "0" ]; then
local def="${1}=\"%{\$reset_color%}\""
else
case ${2} in
1) # bold color
local def="${1}=\"%{\$${4}_bold[${3}]%}\""
;;
*)
local def="${1}=\"%{\$${4}[${3}]%}\""
;;
esac
fi
eval "${def}"
}
_map_colors Bold ${AttrBright}
_map_colors Bright ${AttrBright}
_map_colors Dim ${AttrDim}
_map_colors '' ${AttrNorm}
if [ -n "$ZSH_VERSION" ]; then
_def_color_zsh IntenseBlack 0 90
_def_color_zsh ResetColor 0 0
else
_def_color IntenseBlack 0 90
_def_color ResetColor 0 0
fi
}
# do the color definitions only once
if [[ -z "${ColorNames+x}" || "${#ColorNames[*]}" = 0 || -z "${IntenseBlack:+x}" || -z "${ResetColor:+x}" ]]; then
define_color_names
fi