-
Notifications
You must be signed in to change notification settings - Fork 0
/
colorit
executable file
·34 lines (30 loc) · 1 KB
/
colorit
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
#!/bin/bash
function colorit(){
local banned_colors=" 22 23 24 25 16 17 18 19 52 53 54 55 88 89 90 91 22 23 24 25 58 59 60 61 94 95 96 97 232 233 234 235 236 237 238 239 240 241 242 243 244 29 256 257 258 259 260 261 262 263 264 265 266 267 "
local bg="$2"
if [[ $bg == "" ]]; then
bg=38
else
bg=48
fi
local item="$1"
if [[ $item == "" ]]; then
return 0
fi
local salt="xyz1"
while true; do
local hash_val=$(( ( 0x$(md5 <<<"${item}$salt")0 ) % 227 + 3))
if [[ $hash_val -lt 0 ]]; then
hash_val=$(( hash_val * -1 ))
fi
if ! echo "$banned_colors" | grep -qw "$hash_val"; then
break
else
salt+="$hash_val"
fi
done
local value=0
local colored_item=$( printf '\001\e[%d;5;%dm\002%s' "$bg" "$hash_val" "$item" )
printf "%b" '\001\e[0m\002'"${colored_item}"'\001\e[0m\002';
}
colorit "${@:-}"