Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add colored ticks version #88

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 46 additions & 3 deletions spark
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@ _echo()
fi
}

REGULAR_TICKS="▁:▂:▃:▄:▅:▆:▇:█"
FIRE_TICKS="$(echo "$(tput setaf 228)▁"):$(echo "$(tput setaf 227)▂"):$(echo "$(tput setaf 226)▃"):$(echo "$(tput setaf 220)▄"):$(echo "$(tput setaf 214)▅"):$(echo "$(tput setaf 208)▆"):$(echo "$(tput setaf 202)▇"):$(echo "$(tput setaf 196)█")"


ticks_styles=(
$REGULAR_TICKS
$FIRE_TICKS
)

tick_style=0

spark()
{
local n numbers=
Expand All @@ -56,7 +67,7 @@ spark()
done

# print ticks
local ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇ █)
local ticks=($(echo ${ticks_styles[$tick_style]} | tr : " "))

# use a high tick if data is constant
(( min == max )) && ticks=(▅ ▆)
Expand All @@ -71,6 +82,33 @@ spark()
_echo
}

parse_args()
{
OPTIND=1

while getopts "h?c:" opt; do
case "$opt" in
c)
case "$OPTARG" in
fire)
tick_style=1
;;
esac
;;
esac
done

shift $((OPTIND-1))

[ "$1" = "--" ] && shift

if [ $# -eq 0 ]; then
data=$(cat)
else
data=$@
fi
}

# If we're being sourced, don't worry about such things
if [ "$BASH_SOURCE" == "$0" ]; then
# Prints the help text for spark.
Expand All @@ -80,7 +118,10 @@ if [ "$BASH_SOURCE" == "$0" ]; then
cat <<EOF

USAGE:
$spark [-h|--help] VALUE,...
$spark [-h|--help|-c fire] VALUE,...

OPTIONS:
-c fire Output fire colored ticks (smallest = yellow, largest = red)

EXAMPLES:
$spark 1 5 22 13 53
Expand All @@ -99,5 +140,7 @@ EOF
exit 0
fi

spark ${@:-`cat`}
parse_args ${@:-`cat`}

spark $data
fi
8 changes: 8 additions & 0 deletions spark-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,11 @@ it_equalizes_at_midtier_on_same_data() {

test $graph = '▅▅▅▅'
}

it_charts_in_fire_colored_ticks() {
data="1,2,3,4,5,6,7,8"
graph="$($spark -c fire $data)"

expected_graph="$(echo "$(tput setaf 228)▁")$(echo "$(tput setaf 227)▂")$(echo "$(tput setaf 226)▃")$(echo "$(tput setaf 220)▄")$(echo "$(tput setaf 214)▅")$(echo "$(tput setaf 208)▆")$(echo "$(tput setaf 202)▇")$(echo "$(tput setaf 196)█")"
test $graph = $expected_graph
}