Permalink
Browse files

Merge branch 'master' of https://github.com/markusfisch/spark into ma…

…rkusfisch-master
  • Loading branch information...
holman committed Nov 17, 2011
2 parents c42cbc9 + 853ee4d commit 0d43d21dbd0696b0ce988ac5ff8e2cecb58c14f8
Showing with 36 additions and 111 deletions.
  1. +36 −111 spark
View
147 spark
@@ -1,6 +1,6 @@
#!/usr/bin/env bash
#
-# spark
+# original spark by
# https://github.com/holman/spark
#
# Generates sparklines for a set of data.
@@ -25,14 +25,12 @@
# # => Prints the spark help text.
# Prints the help text for spark.
-#
-# Returns nothing.
help()
{
- cat <<-EOF
+ cat <<EOF
USAGE:
- spark [comma,separated,value,list]
+ spark VALUE,...
EXAMPLES:
spark 1,5,22,13,53
@@ -42,115 +40,42 @@ help()
EOF
}
-# The actual fun characters we are generating in the sparkline.
-ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇ █)
-
-# The numbers the user gave us.
-numbers=()
-
-# The sorted array of the numbers.
-sorted=()
-
-# This sets up our secondary array so we can actually generate the correct
-# tick.
+# Generates sparklines.
#
-# Returns nothing.
-setup_array() {
- # 3,6,2 => 2,3,6
- sorted=$(echo $1 | tr ',' '\n' | sort -k1,1n | paste -s -d',' -)
-
- # convert comma-separated string to array
- IFS=,
- sorted=($sorted)
- numbers=($1)
-}
-
-# The maximum value of the sorted array. In other words, the last value.
-sort_max()
+# @param ... - data
+spark()
{
- last=${#sorted[@]}
- echo $(echo ${sorted[$last - 1]} | awk '{printf "%.0f",$1}')
+ local n numbers=
+
+ # find min/max values
+ local min=0xffffffff max=0
+
+ for n in ${@//,/ }
+ do
+ # on Linux (or with bash4) we could use `printf %.0f $n` here to
+ # round the number but that doesn't work on OS X (bash3) nor does
+ # `awk '{printf "%.0f",$1}' <<< $n` work, so just cut it off
+ n=${n%.*}
+ (( n < min )) && min=$n
+ (( n > max )) && max=$n
+ numbers=$numbers${numbers:+ }$n
+ done
+
+ # print ticks
+ local ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇ █)
+ local f=$(( (($max-$min)<<8)/(${#ticks[@]}-1) ))
+ (( f < 1 )) && f=1
+
+ for n in $numbers
+ do
+ echo -n ${ticks[$(( ((($n-$min)<<8)/$f) ))]}
+ done
+ echo
}
-# The minimum value of the sorted array. In other words, the first value.
-sort_min()
-{
- echo ${sorted[0]}
-}
-
-# Find the distance between tiers so we know which tick to assign a character.
-tier()
-{
- number_of_ticks=${#ticks[@]}
- distance=$(( $(sort_max) / $number_of_ticks ))
- if [ $distance -eq 0 ]
- then
- distance=1
- fi
- echo $distance
+[ "$1" == '-h' ] && {
+ help
+ exit
}
-# Determines what tick we should print for this number and prints it.
-#
-# Returns nothing.
-print_tick()
-{
- tier=$(tier)
-
- for (( i = 0 ; i < ${#ticks[@]} ; i++ ))
- do
-
- number=$(echo $1 | awk '{printf "%.0f",$1}')
- less_than=$(( $i * $tier + sort_min + $tier ))
- greater_than=$(( ($i - 1) * $tier + sort_min + $tier ))
- result=$(( $number <= $less_than && $number >= $greater_than ))
-
- if [ $result -eq 1 ]
- then
- printf '%s' "${ticks[$i]}"
- return
- fi
- done
-
- last=${#ticks[@]}
- printf '%s' ${ticks[$last-1]}
-}
-
-# Iterate over all of our ticks and print them out.
-#
-# Returns nothing.
-print_ticks()
-{
- for number in ${numbers[@]}
- do
- print_tick $number
- done
- echo
-}
-
-while getopts ":h" option; do
- case "$option" in
- h) help && exit ;;
-# [?]) echo "$OPTARG";;
- esac
-done
-
-# Accept input from $1 or from the pipeline.
-if test "$*" != ""
-then
- data="$*"
-else
- # check to see if stdin's a tty
- if [ -t 0 ]; then
- help
- exit
- fi
-
- data="`cat`"
-fi
-
-# Trim spaces to allow input like '1, 2, 3'
-data=$(echo $data | tr -s '[:space:]' ',')
-
-setup_array $data
-print_ticks $data
+spark ${@:-`cat`}

0 comments on commit 0d43d21

Please sign in to comment.