Skip to content

Commit

Permalink
▁▂▃▅▂▇
Browse files Browse the repository at this point in the history
  • Loading branch information
holman committed Nov 15, 2011
0 parents commit 8b17457
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) Zach Holman, http://zachholman.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# spark
### sparklines for your shell

See? Here's a graph of your productivity gains after using spark: ▁▂▃▅▇

## install

spark is a [shell script][bin], so drop it somewhere and make sure it's added
to your `$PATH`. It's helpful if you have a super-neat collection of dotfiles,
[like mine][dotfiles].

## usage

Just run `spark` and pass it a comma-delimited list of numbers. It's designed
to be used in conjunction with other scripts that can output in that format.

spark 0,30,55,80,33,150
▁▂▃▅▂▇

Invoke help with `spark -h`.

## cooler usage

There's a lot of stuff you can do.

Number of commits to the github/github Git repository, by author:

```bash
› git shortlog -s |
cut -f1 |
tr "\n" ',' |
sed 's/ //g' |
spark
▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▂▁▁▅▁▂▁▁▁▂▁▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁
```

Magnitude of earthquakes over 1.0 in the last 24 hours:

```bash
› curl http://earthquake.usgs.gov/earthquakes/catalogs/eqs1day-M1.txt --silent |
sed '1d' |
cut -d, -f9 |
tr "\n" ',' |
sed 's/ //g' |
spark
▅▆▂▃▂▂▂▅▂▂▅▇▂▂▂▃▆▆▆▅▃▂▂▂▁▂▂▆▁▃▂▂▂▂▃▂▆▂▂▂▁▂▂▃▂▂▃▂▂▃▂▂▁▂▂▅▂▂▆▆▅▃▆
```

Code visualization. The number of characters of `spark` itself, by line, ignoring empty lines:

```bash
› awk '{ print length($0) }' spark |
grep -Ev 0 |
tr "\n" ',' |
spark
▁▁▁▁▅▁▇▁▁▅▁▁▁▁▁▂▂▁▃▃▁▁▃▁▃▁▂▁▁▂▂▅▂▃▂▃▃▁▆▃▃▃▁▇▁▁▂▂▂▇▅▁▂▂▁▇▁▃▁▇▁▂▁▇▁▁▆▂▁▇▁▂▁▁▂▅▁▂▁▆▇▇▂▁▂▁▁▁▂▂▁▅▁▂▁▁▃▁▃▁▁▁▃▂▂▂▁▁▅▂▁▁▁▁▂▂▁▁▁▂▂
```

Since it's just a shell script, you could pop it in your prompt, too:

```
ruby-1.8.7-p334 in spark/ on master with history: ▂▅▇▂
```

## wicked cool usage

Sounds like a wiki is a great place to collect all of your
[wicked cool usage][wiki] for spark.

## todo

- Speedup. It's a little more sluggish than it should be since we're doing a
few unnecessary loops.
- I'd like to constrain character widths with a `-w` switch.

## ▇▁ ⟦⟧ ▇▁

This is a [@holman][holman] joint.

[dotfiles]: https://github.com/holman/dotfiles
[bin]: https://github.com/holman/spark/blob/master/spark
[wiki]: https://github.com/holman/spark/wiki/Wicked-Cool-Usage
[holman]: https://twitter.com/holman
145 changes: 145 additions & 0 deletions spark
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#!/bin/sh
#
# spark
# https://github.com/holman/spark
#
# Generates sparklines for a set of data.
#
# Here's a a good web-based sparkline generator that was a bit of inspiration
# for spark:
#
# https://datacollective.org/sparkblocks
#
# spark takes a comma-separated list of data and then prints a sparkline out of
# it.
#
# Examples:
#
# spark 1,5,22,13,53
# # => ▁▁▃▂▇
#
# spark 0,30,55,80,33,150
# # => ▁▂▃▅▂▇
#
# spark -h
# # => Prints the spark help text.
set -e

# Prints the help text for spark.
#
# Returns nothing.
help()
{
echo "spark\n"
echo "USAGE:"
echo " spark [comma,separated,value,list]\n"
echo "EXAMPLES:"
echo " spark 1,5,22,13,53"
echo " ▁▁▃▂▇"
echo " spark 0,30,55,80,33,150"
echo " ▁▂▃▅▂▇"
}

# 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.
#
# 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()
{
last=${#sorted[@]}
echo ${sorted[$last - 1]}
}

# 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=$(echo "$(sort_max) / $number_of_ticks" | bc)
echo $distance
}

# Determines what tick we should print for this number and prints it.
#
# Returns nothing.
print_tick()
{
tier=$(tier)

for (( i = 0 ; i < ${#numbers[@]} ; i++ ))
do
tick=${ticks[$i]}
number=$1
less_than=$(echo "$i * $tier + sort_min + $tier" | bc)
greater_than=$(echo "($i - 1) * $tier + sort_min + $tier" | bc)
result=$(echo "$number <= $less_than && $number >= $greater_than" | bc)

if [ $result -eq 1 ]
then
echo "$tick"
return
fi
done

last=${#ticks[@]}
echo ${ticks[$last-1]}
}

# Iterate over all of our ticks and print them out.
#
# Returns nothing.
print_ticks()
{
for number in ${numbers[@]}
do
echo $"$(print_tick $number)\c"
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 "$1" != ""
then
data="$1"
else
data=''
while read data; do
data=$data
break
done
fi

setup_array $data
print_ticks $data

0 comments on commit 8b17457

Please sign in to comment.