Skip to content

Commit c45613d

Browse files
committed
[small-prompt] use static values for rainbow, do not call python everytime
1 parent 73c2826 commit c45613d

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

unix/small-prompt/prompt.sh

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
# 2023-07-10 - Microeinstein
22

3-
# Features:
4-
# - dynamic prompt based on terminal
5-
# - colors:
6-
# - full, 256, 8
7-
# - rainbow colors
8-
# - user=green root=red
9-
# - (hack) detect light theme
10-
# - styles:
11-
# - {powerline, basic} x {long,short}
12-
# - detect return code
13-
# - compact current directory based on terminal size
14-
# - stay on previous line with no command given
15-
# - detect if last command did not end with newline
16-
#
17-
# Unimplemented (technical limitations):
18-
# - cannot reprint prompt right after resize
3+
4+
gen_rgb_rainbow() {
5+
# call by hand
6+
local py="$(cat <<EOF
7+
from sys import argv
8+
from colorsys import hsv_to_rgb
9+
l = 30
10+
f = lambda n: str(round(n*255))
11+
for n in range(0,l):
12+
c = map(f, hsv_to_rgb(n/l, float(argv[1]), float(argv[2])))
13+
c = ';'.join(c)
14+
# print(f"\x1b[0;30;48;2;{c}mlorem ipsum\x1b[0m")
15+
# print(f"'{c}'", end=' ')
16+
print(c)
17+
EOF
18+
)"
19+
mapfile -t __RAINBOW < <(python -c "$py" "$@")
20+
printf "'%s' " "${__RAINBOW[@]}" | fold -s
21+
echo
22+
}
1923

2024

2125
setup_PS1() {
@@ -74,29 +78,29 @@ setup_PS1() {
7478

7579

7680
# rainbow and specific
77-
local py="$(cat <<EOF
78-
from sys import argv
79-
from colorsys import hsv_to_rgb
80-
l = 30
81-
f = lambda n: str(round(n*255))
82-
for n in range(0,l):
83-
c = map(f, hsv_to_rgb(n/l, float(argv[1]), float(argv[2])))
84-
c = ';'.join(c)
85-
# print(f"\x1b[0;30;48;2;{c}mlorem ipsum\x1b[0m")
86-
# print(f"'{c}'", end=' ')
87-
print(c)
88-
EOF
89-
)"
90-
9181
local gray
9282
if ((rgb)); then
9383
C[fc]='38;2;%s' # truecolor
9484
C[bc]='48;2;%s'
9585
if ((light)); then
96-
mapfile -t __RAINBOW < <(python -c "$py" .5 .7)
86+
__RAINBOW=(
87+
'178;89;89' '178;107;89' '178;125;89' '178;143;89' '178;161;89'
88+
'178;178;89' '161;178;89' '143;178;89' '125;178;89' '107;178;89'
89+
'89;178;89' '89;178;107' '89;178;125' '89;178;143' '89;178;161'
90+
'89;178;178' '89;161;178' '89;143;178' '89;125;178' '89;107;178'
91+
'89;89;178' '107;89;178' '125;89;178' '143;89;178' '161;89;178'
92+
'178;89;178' '178;89;161' '178;89;143' '178;89;125' '178;89;107'
93+
)
9794
gray='218;218;218'
9895
else
99-
mapfile -t __RAINBOW < <(python -c "$py" .5 .9)
96+
__RAINBOW=( # .5 .95
97+
'242;121;121' '242;145;121' '242;170;121' '242;194;121' '242;218;121'
98+
'242;242;121' '218;242;121' '194;242;121' '170;242;121' '145;242;121'
99+
'121;242;121' '121;242;145' '121;242;170' '121;242;194' '121;242;218'
100+
'121;242;242' '121;218;242' '121;194;242' '121;170;242' '121;145;242'
101+
'121;121;242' '145;121;242' '170;121;242' '194;121;242' '218;121;242'
102+
'242;121;242' '242;121;218' '242;121;194' '242;121;170' '242;121;145'
103+
)
100104
gray='78;78;78'
101105
fi
102106
else
@@ -261,8 +265,8 @@ EOF
261265
$(FMT bc="$stil" fdbla ) ${B[user]}
262266
$(FMT fc="$stil" bc="$gray") ''
263267
$(FMT fr ) ' $(__MYPWD) '
264-
$(FMT blusr fc="$gray") ''
265-
$(FMT flusr br ) ''
268+
$(FMT bdusr fc="$gray") ''
269+
$(FMT fdusr br ) ''
266270
)
267271
fi
268272
ps1+=($(FMT r) ' ')

0 commit comments

Comments
 (0)