Skip to content
Browse files

streamlined processing

fraction of floats is now cut off in favor of speed and portability
scaling is now done by fixed point math
correct display of numbers that are all lower than the total of ticks
sparkline does always display relative difference between numbers
  • Loading branch information...
1 parent 48f2b23 commit ecdfe37ae6d36f4f0c6f061e45c571aca7c315bf @markusfisch markusfisch committed
Showing with 38 additions and 113 deletions.
  1. +38 −113 spark
View
151 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,132 +25,57 @@
# # => 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
- ▁▁▃▂▇
+ ƒ‚‡
spark 0,30,55,80,33,150
- ▁▂▃▅▂▇
+ ‚ƒ…‚‡
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 ecdfe37

Please sign in to comment.
Something went wrong with that request. Please try again.