From 206a69d8d076cfaa4114eb6f9fc30e90445a3442 Mon Sep 17 00:00:00 2001 From: Alexandre Pretto Nunes Date: Fri, 14 Aug 2015 16:57:19 +0200 Subject: [PATCH] Add command line parsing and colored ticks --- spark | 44 ++++++++++++++++++++++++++++++++++++++++++-- spark-test.sh | 8 ++++++++ 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/spark b/spark index 53a38be..ec7150e 100755 --- a/spark +++ b/spark @@ -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= @@ -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=(▅ ▆) @@ -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. @@ -99,5 +137,7 @@ EOF exit 0 fi - spark ${@:-`cat`} + parse_args ${@:-`cat`} + + spark $data fi diff --git a/spark-test.sh b/spark-test.sh index 4430db2..cf2b0f4 100644 --- a/spark-test.sh +++ b/spark-test.sh @@ -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 +}