Skip to content

Commit 4222077

Browse files
committed
Make host based color a generic function.
1 parent ea03949 commit 4222077

File tree

4 files changed

+39
-21
lines changed

4 files changed

+39
-21
lines changed

functions/geometry_status

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,9 @@ geometry_status() {
66

77
local color=${GEOMETRY_STATUS_COLOR:-default}
88

9-
if ( $error ); then color=${GEOMETRY_STATUS_COLOR_ERROR:-red}; else
10-
( ${GEOMETRY_STATUS_SYMBOL_COLOR_HASH:=false} ) && {
11-
local colors=({1..9})
12-
13-
(($(echotc Co) == 256)) && colors+=({17..230})
14-
15-
if (( ${+GEOMETRY_STATUS_SYMBOL_COLOR_HASH_COLORS} )); then
16-
colors=(${GEOMETRY_STATUS_SYMBOL_COLOR_HASH_COLORS})
17-
fi
18-
19-
local sum=0; for c in ${(s::)^HOST}; do ((sum += $(print -f '%d' "'$c"))); done
20-
21-
local index="$(($sum % ${#colors}))"
22-
23-
[[ "$index" -eq 0 ]] && index=1
24-
25-
color=${colors[${index}]}
26-
}
9+
if ( $error );
10+
then
11+
color=${GEOMETRY_STATUS_COLOR_ERROR:-red};
2712
fi
2813

2914
( $root && $error ) && ansi $color ${GEOMETRY_STATUS_SYMBOL_ROOT_ERROR:=▽} && return

geometry.zsh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ geometry::time() {
3737
${2:-false} && ansi $color ${(j: :)human} || ansi $color $human[1]
3838
}
3939

40+
# Generate a color based on hostname.
41+
geometry::hostcolor() {
42+
43+
if (( ${+GEOMETRY_HOST_COLOR} )); then
44+
echo ${GEOMETRY_HOST_COLOR}
45+
return
46+
fi
47+
48+
if (( ${+GEOMETRY_HOST_COLORS} )); then
49+
local colors=(${GEOMETRY_HOST_COLORS})
50+
else
51+
local colors=({1..9})
52+
(($(echotc Co) == 256)) && colors+=({17..230})
53+
fi
54+
55+
local sum=0; for c in ${(s::)^HOST}; do ((sum += $(print -f '%d' "'$c"))); done
56+
local index="$(($sum % ${#colors}))"
57+
58+
[[ "$index" -eq 0 ]] && index = 1
59+
60+
echo ${colors[${index}]}
61+
}
62+
4063
# set title to COMMAND @ CURRENT_DIRECTORY
4164
geometry::set_title() { print -n "\e]0;${2} @ ${PWD##*/}\a"; }
4265
add-zsh-hook preexec geometry::set_title

images/screenshots/colorize.png

-130 KB
Loading

readme.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,8 @@ GEOMETRY_STATUS_SYMBOL_ERROR="△" # displayed when exit value is != 0
8484
GEOMETRY_STATUS_COLOR_ERROR="magenta" # prompt symbol color when exit value is != 0
8585
GEOMETRY_STATUS_COLOR="default" # prompt symbol color
8686
GEOMETRY_STATUS_COLOR_ROOT="red" # root prompt symbol color
87-
GEOMETRY_STATUS_COLOR_HASH=true # color status symbol based on hostname
8887
```
8988

90-
![colorize](/images/screenshots/colorize.png)
91-
9289
### geometry_exitcode
9390
This renders the exit code of the previous function if it is not success.
9491

@@ -113,6 +110,19 @@ GEOMETRY_GIT_TIME_DETAILED=true # show full time (e.g. `12h 30m 53s`) instea
113110

114111
![git_conflicts](/images/screenshots/git_conflicts.png)
115112

113+
## Hostname based colors
114+
Geometry provides a generic function (`geometry::hostcolor`) for setting any color based on hostname. The color is calculated as the sum of each character converted to an integer. For example the hostname 'abc' generates a value of 294.
115+
116+
By default the colors 1-9 and 17-230 are used as colors depending on what the environment supports. The calculated value is then modded by the size of the color list to choose a value. This value will be consistent given the same hostname.
117+
118+
These colors can be overridden by setting variables.
119+
120+
```shell
121+
GEOMETRY_HOST_COLORS=({1..7}) # Only use the colors 1-7
122+
GEOMETRY_HOST_COLOR=4 # Override the color for a specific host.
123+
```
124+
![colorize](/images/screenshots/colorize.png)
125+
116126
## Thanks
117127

118128
geometry is maintained by [frm](https://github.com/frm), [desyncr](https://github.com/desyncr) and [jedahan](https://github.com/jedahan).

0 commit comments

Comments
 (0)