Last active
February 16, 2017 00:17
-
-
Save echelon/f3552151ddfb5d6fe5b3ba03c04459f3 to your computer and use it in GitHub Desktop.
Random terminal color for grep, silver searcher, etc.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Select a random color for search utilities such as 'grep' or 'ag'. | |
# Create an alias: alias ag='ag --color-match="$(random_color)"' | |
# Or, alias grep='GREP_COLOR="$(random_color)" grep' | |
random_color() { | |
# List of highlight colors to iterate between | |
# Legacy Terminal color codes, | |
# 01;33 - light yellow text | |
# 01;34 - dark blue text | |
# 01;35 - light pink text | |
# 01;36 - light cyan text | |
# 33;41 - yellow on red | |
# 33;44 - yellow on blue | |
# 33;45 - yellow on pink | |
# 1;33;46 - yellow on cyan | |
# Modern Terminal color codes, | |
# 32;2;(FOREGROUND RGB);48;2;(BACKGROUND RGB) | |
# 38;2;255;255;0;48;2;150;150;80 - yellow on mustard | |
# 38;2;255;255;255;48;2;100;100;150 - white on blueish gray | |
# 38;2;0;0;0;48;2;255;100;150 - black on pink | |
# | |
declare -r colors=( | |
"01;34" # dark blue text | |
"38;2;0;0;0;48;2;255;100;150" # black on pink | |
"33;44" # yellow on blue | |
"38;2;255;255;0;48;2;150;150;80" # yellow on mustard | |
"01;35" # light pink text | |
"38;2;255;255;255;48;2;100;100;150" # white on blueish gray | |
) | |
# File to maintain the counter (we can't set an env var). | |
declare -r file="${HOME}/.random_color" | |
# Read only a numeric value from file. | |
value=`cat $file 2> /dev/null` | |
counter=$(echo $value | sed 's/[^0-9]*//g') | |
# Increment the circular counter. | |
incremented=$((1 + $counter)) | |
index=$(($incremented % ${#colors[@]})) | |
# Write back for persistence. | |
echo "${index}" > $file | |
echo ${colors[$index]} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment