Skip to content

Commit

Permalink
Add command line parsing and colored ticks
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexandre Pretto Nunes committed Aug 14, 2015
1 parent 3220671 commit 206a69d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
44 changes: 42 additions & 2 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 Down Expand Up @@ -99,5 +137,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
}

0 comments on commit 206a69d

Please sign in to comment.