Skip to content

Commit 68ae121

Browse files
author
Vasiliy Polyakov
committed
save
1 parent 66792bc commit 68ae121

File tree

2 files changed

+78
-44
lines changed

2 files changed

+78
-44
lines changed

lib/colors.sh

Lines changed: 74 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,83 @@ declare -gi COLORS
1111
declare -ga FG BG
1212
declare -gA SGR
1313

14-
local -i terminfo=0 c
15-
case $1 in
16-
0) declare -r COLORS FG BG SGR; return 0 ;;
17-
terminfo) terminfo=1 ;;
18-
esac
14+
local -i arrays=0 funcs=0 terminfo=0
15+
while (( $# )); do
16+
case $1 in
17+
0) declare -r COLORS=0 FG=() BG=() SGR=(); return 0 ;;
18+
funcs) funcs=1 ;;
19+
arrays) arrays=1 ;;
20+
terminfo) terminfo=1 ;;
21+
esac; shift
22+
do
1923

2024
COLORS="$(tput colors)"
2125

22-
if (( terminfo )); then
23-
for (( c = 0; c < COLORS; c++ )); do
24-
FG[c]="$(tput setaf "$c")"
25-
BG[c]="$(tput setab "$c")"
26-
done
27-
SGR[bold]="$(tput bold)"
28-
SGR[dim]="$(tput dim)"
29-
SGR[italics]="$(tput sitm)"
30-
SGR[uline]="$(tput smul)"
31-
SGR[blink]="$(tput blink)"
32-
SGR[reverse]="$(tput rev)"
33-
SGR[reset]="$(tput sgr0)"
34-
else
35-
local CSI=$'\e['
36-
for (( c = 0; c < 8 && COLORS >= 8; c++ )); do
37-
FG[c]="${CSI}$((30+c))m"
38-
BG[c]="${CSI}$((40+c))m"
39-
done
40-
for (( c = 8; c < 16 && COLORS >= 16; c++ )); do
41-
FG[c]="${CSI}$((82+c))m"
42-
BG[c]="${CSI}$((92+c))m"
43-
done
44-
for (( c = 16; c < 256 && COLORS >= 256; c++ )); do
45-
FG[c]="${CSI}38;5;${c}m"
46-
BG[c]="${CSI}48;5;${c}m"
26+
_colors::get() {
27+
28+
}
29+
30+
SGR() {
31+
local -i OPTIND
32+
local OPT OPTARG
33+
local -a a
34+
35+
shopt -qs extglob
36+
while (( $# )); do
37+
case $1 in
38+
fg=+([0-9])) a+=() ;;
39+
esac; shift
4740
done
48-
SGR[bold]="${CSI}1m"
49-
SGR[dim]="${CSI}2m"
50-
SGR[italics]="${CSI}3m"
51-
SGR[uline]="${CSI}4m"
52-
SGR[blink]="${CSI}5m"
53-
SGR[reverse]="${CSI}7m"
54-
SGR[reset]="${CSI}m"
55-
unset CSI
56-
fi; unset terminfo c
57-
# TODO: add one-letter and digit aliases for SGR
58-
SGR[rev]="${SGR[reverse]}"
59-
SGR[exit]="${SGR[reset]}"
60-
SGR[0]="${SGR[reset]}"
41+
}
42+
43+
_colors::init_arrays() {
44+
local -i c
45+
46+
if (( terminfo )); then
47+
for (( c = 0; c < COLORS; c++ )); do
48+
FG[c]="$(tput setaf "$c")"
49+
BG[c]="$(tput setab "$c")"
50+
done
51+
SGR[bold]="$(tput bold)"
52+
SGR[dim]="$(tput dim)"
53+
SGR[italics]="$(tput sitm)"
54+
SGR[uline]="$(tput smul)"
55+
SGR[blink]="$(tput blink)"
56+
SGR[reverse]="$(tput rev)"
57+
SGR[reset]="$(tput sgr0)"
58+
else
59+
local CSI=$'\e['
60+
for (( c = 0; c < 8 && COLORS >= 8; c++ )); do
61+
FG[c]="${CSI}$((30+c))m"
62+
BG[c]="${CSI}$((40+c))m"
63+
done
64+
for (( c = 8; c < 16 && COLORS >= 16; c++ )); do
65+
FG[c]="${CSI}$((82+c))m"
66+
BG[c]="${CSI}$((92+c))m"
67+
done
68+
for (( c = 16; c < 256 && COLORS >= 256; c++ )); do
69+
FG[c]="${CSI}38;5;${c}m"
70+
BG[c]="${CSI}48;5;${c}m"
71+
done
72+
SGR[bold]="${CSI}1m"
73+
SGR[dim]="${CSI}2m"
74+
SGR[italics]="${CSI}3m"
75+
SGR[uline]="${CSI}4m"
76+
SGR[blink]="${CSI}5m"
77+
SGR[reverse]="${CSI}7m"
78+
SGR[reset]="${CSI}m"
79+
unset CSI
80+
fi
81+
# TODO: add one-letter and digit aliases for SGR
82+
SGR[rev]="${SGR[reverse]}"
83+
SGR[exit]="${SGR[reset]}"
84+
SGR[0]="${SGR[reset]}"
85+
SGR[1]="${SGR[bold]}"
86+
}
87+
88+
colors::unset() {
89+
90+
}
6191

6292
declare -r COLORS FG BG SGR
93+

lib/prompt.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,10 @@ prompt::vcs() {
6666
}
6767

6868
prompt::venv() {
69-
[[ -n $VIRTUAL_ENV ]] && echo -en " (${PROMPT_COLOR[venv]}${VIRTUAL_ENV##*/}${PROMPT_COLOR[reset]})"
69+
[[ -n $VIRTUAL_ENV ]] || return
70+
71+
72+
echo -en " (${PROMPT_COLOR[venv]}${VIRTUAL_ENV##*/}${PROMPT_COLOR[reset]})"
7073
}
7174

7275
prompt::user_color() {

0 commit comments

Comments
 (0)