Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100755 151 lines (130 sloc) 2.703 kB
4155609 @nrr Make the spark script work on systems like NetBSD where bash lives so…
nrr authored
1 #!/usr/bin/env bash
8b17457 @holman ▁▂▃▅▂▇
holman authored
2 #
3 # spark
4 # https://github.com/holman/spark
5 #
6 # Generates sparklines for a set of data.
7 #
8 # Here's a a good web-based sparkline generator that was a bit of inspiration
9 # for spark:
10 #
11 # https://datacollective.org/sparkblocks
12 #
13 # spark takes a comma-separated list of data and then prints a sparkline out of
14 # it.
15 #
16 # Examples:
17 #
18 # spark 1,5,22,13,53
19 # # => ▁▁▃▂▇
20 #
21 # spark 0,30,55,80,33,150
22 # # => ▁▂▃▅▂▇
23 #
24 # spark -h
25 # # => Prints the spark help text.
26 set -e
27
28 # Prints the help text for spark.
29 #
30 # Returns nothing.
31 help()
32 {
4ecdbe2 @rentalcustard Fix usage text formatting
rentalcustard authored
33 cat <<-EOF
1602499 @holman reformat help
holman authored
34
4ecdbe2 @rentalcustard Fix usage text formatting
rentalcustard authored
35 USAGE:
36 spark [comma,separated,value,list]
1602499 @holman reformat help
holman authored
37
4ecdbe2 @rentalcustard Fix usage text formatting
rentalcustard authored
38 EXAMPLES:
39 spark 1,5,22,13,53
40 ▁▁▃▂▇
41 spark 0,30,55,80,33,150
42 ▁▂▃▅▂▇
43 EOF
8b17457 @holman ▁▂▃▅▂▇
holman authored
44 }
45
46 # The actual fun characters we are generating in the sparkline.
f07309f @holman THE MERGE BUTTON, ITS TOO POWERFUL
holman authored
47 ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇)
8b17457 @holman ▁▂▃▅▂▇
holman authored
48
49 # The numbers the user gave us.
50 numbers=()
51
52 # The sorted array of the numbers.
53 sorted=()
54
55 # This sets up our secondary array so we can actually generate the correct
56 # tick.
57 #
58 # Returns nothing.
59 setup_array() {
60 # 3,6,2 => 2,3,6
61 sorted=$(echo $1 | tr ',' '\n' | sort -k1,1n | paste -s -d',' -)
62
63 # convert comma-separated string to array
64 IFS=,
65 sorted=($sorted)
66 numbers=($1)
67 }
68
69 # The maximum value of the sorted array. In other words, the last value.
70 sort_max()
71 {
72 last=${#sorted[@]}
73 echo ${sorted[$last - 1]}
74 }
75
76 # The minimum value of the sorted array. In other words, the first value.
77 sort_min()
78 {
79 echo ${sorted[0]}
80 }
81
82 # Find the distance between tiers so we know which tick to assign a character.
83 tier()
84 {
85 number_of_ticks=${#ticks[@]}
2243964 @peff use shell arithmetic expansion
peff authored
86 distance=$(( $(sort_max) / $number_of_ticks ))
8b17457 @holman ▁▂▃▅▂▇
holman authored
87 echo $distance
88 }
89
90 # Determines what tick we should print for this number and prints it.
91 #
92 # Returns nothing.
93 print_tick()
94 {
95 tier=$(tier)
96
97 for (( i = 0 ; i < ${#numbers[@]} ; i++ ))
98 do
99 tick=${ticks[$i]}
100 number=$1
2243964 @peff use shell arithmetic expansion
peff authored
101 less_than=$(( $i * $tier + sort_min + $tier ))
102 greater_than=$(( ($i - 1) * $tier + sort_min + $tier ))
103 result=$(( $number <= $less_than && $number >= $greater_than ))
8b17457 @holman ▁▂▃▅▂▇
holman authored
104
105 if [ $result -eq 1 ]
106 then
a179f1f @peff print sparks incrementally instead of building string
peff authored
107 printf '%s' "$tick"
8b17457 @holman ▁▂▃▅▂▇
holman authored
108 return
109 fi
110 done
111
112 last=${#ticks[@]}
a179f1f @peff print sparks incrementally instead of building string
peff authored
113 printf '%s' ${ticks[$last-1]}
8b17457 @holman ▁▂▃▅▂▇
holman authored
114 }
115
116 # Iterate over all of our ticks and print them out.
117 #
118 # Returns nothing.
119 print_ticks()
120 {
121 for number in ${numbers[@]}
122 do
a179f1f @peff print sparks incrementally instead of building string
peff authored
123 print_tick $number
8b17457 @holman ▁▂▃▅▂▇
holman authored
124 done
a179f1f @peff print sparks incrementally instead of building string
peff authored
125 echo
8b17457 @holman ▁▂▃▅▂▇
holman authored
126 }
127
128 while getopts ":h" option; do
129 case "$option" in
130 h) help && exit ;;
131 # [?]) echo "$OPTARG";;
132 esac
133 done
134
135 # Accept input from $1 or from the pipeline.
136 if test "$1" != ""
137 then
138 data="$1"
139 else
c4fde8a @holman don't hang on tty (closes #1)
holman authored
140 # check to see if stdin's a tty
141 if [ -t 0 ]; then
142 help
143 exit
144 fi
145
424194d @peff drop pointless loop
peff authored
146 read data
8b17457 @holman ▁▂▃▅▂▇
holman authored
147 fi
148
149 setup_array $data
150 print_ticks $data
Something went wrong with that request. Please try again.