├── .travis.yml ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── VERSION ├── spark ├── spark-test.sh └── test /.travis.yml: -------------------------------------------------------------------------------- 1 | language: bash 2 | 3 | before_script: 4 | - curl -L https://github.com/bmizerany/roundup/tarball/v0.0.5 | tar xvzf - 5 | - cd bmizerany-roundup-5c5dcb1 && ./configure && make && sudo make install 6 | - cd - 7 | 8 | script: 9 | - ./test 10 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # changes 2 | 3 | ## 1.0.0 4 | 5 | Hey! A 1.0! Now's a decent time as any. Starting to cut versions just for the 6 | sake of things like Homebrew. So, might as well start with 1.0. 7 | 8 | 1.0 encompasses things that happened during 9 | [the first week](https://zachholman.com/posts/from-hack-to-popular-project/) 10 | of development. -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License 2 | 3 | Copyright (c) Zach Holman, https://zachholman.com 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # spark 2 | ### sparklines for your shell 3 | 4 | See? Here's a graph of your productivity gains after using spark: ▁▂▃▅▇ 5 | 6 | ## install 7 | 8 | spark is a [shell script][bin], so drop it somewhere and make sure it's added 9 | to your `$PATH`. It's helpful if you have a super-neat collection of dotfiles, 10 | [like mine][dotfiles]. Or you can use the following one-liner: 11 | 12 | ```sh 13 | sudo sh -c "curl https://raw.githubusercontent.com/holman/spark/master/spark -o /usr/local/bin/spark && chmod +x /usr/local/bin/spark" 14 | ``` 15 | 16 | If you're on OS X, spark is also on [Homebrew][brew]: 17 | 18 | brew install spark 19 | 20 | Depending on the fonts you have in your system and you use in the 21 | terminal, you might end up with irregular blocks. This is due to some 22 | fonts providing only part of the blocks, while the others are taken from 23 | a different, fallback font. 24 | 25 | ## usage 26 | 27 | Just run `spark` and pass it a list of numbers (comma-delimited, spaces, 28 | whatever you'd like). It's designed to be used in conjunction with other 29 | scripts that can output in that format. 30 | 31 | spark 0 30 55 80 33 150 32 | ▁▂▃▅▂▇ 33 | 34 | Invoke help with `spark -h`. 35 | 36 | ## cooler usage 37 | 38 | There's a lot of stuff you can do. 39 | 40 | Number of commits to the github/github Git repository, by author: 41 | 42 | ```sh 43 | › git shortlog -s | 44 | cut -f1 | 45 | spark 46 | ▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▃▁▁▁▁▁▁▁▁▂▁▁▅▁▂▁▁▁▂▁▁▁▁▁▁▁▁▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁ 47 | ``` 48 | 49 | Magnitude of earthquakes worldwide 2.5 and above in the last 24 hours: 50 | 51 | ```sh 52 | › curl -s https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.csv | 53 | sed '1d' | 54 | cut -d, -f5 | 55 | spark 56 | ▃█▅▅█▅▃▃▅█▃▃▁▅▅▃▃▅▁▁▃▃▃▃▃▅▃█▅▁▃▅▃█▃▁ 57 | ``` 58 | 59 | Code visualization. The number of characters of `spark` itself, by line, ignoring empty lines: 60 | 61 | ```sh 62 | › awk '{ print length($0) }' spark | 63 | grep -Ev 0 | 64 | spark 65 | ▁▁▁▁▅▁▇▁▁▅▁▁▁▁▁▂▂▁▃▃▁▁▃▁▃▁▂▁▁▂▂▅▂▃▂▃▃▁▆▃▃▃▁▇▁▁▂▂▂▇▅▁▂▂▁▇▁▃▁▇▁▂▁▇▁▁▆▂▁▇▁▂▁▁▂▅▁▂▁▆▇▇▂▁▂▁▁▁▂▂▁▅▁▂▁▁▃▁▃▁▁▁▃▂▂▂▁▁▅▂▁▁▁▁▂▂▁▁▁▂▂ 66 | ``` 67 | 68 | Since it's just a shell script, you could pop it in your prompt, too: 69 | 70 | ``` 71 | ruby-1.8.7-p334 in spark/ on master with history: ▂▅▇▂ 72 | › 73 | ``` 74 | 75 | ## wicked cool usage 76 | 77 | Sounds like a wiki is a great place to collect all of your 78 | [wicked cool usage][wiki] for spark. 79 | 80 | ## contributing 81 | 82 | Contributions welcome! Like seriously, I think contributions are real nifty. 83 | 84 | Make your changes and be sure the tests all pass: 85 | 86 | ./test 87 | 88 | That also means you should probably be adding your own tests as well as changing 89 | the code. Wouldn't want to lose all your good work down the line, after all! 90 | 91 | Once everything looks good, open a pull request. 92 | 93 | ## ▇▁ ⟦⟧ ▇▁ 94 | 95 | This is a [@holman][holman] joint. 96 | 97 | [dotfiles]: https://github.com/holman/dotfiles 98 | [brew]: https://github.com/mxcl/homebrew 99 | [bin]: https://github.com/holman/spark/blob/master/spark 100 | [wiki]: https://github.com/holman/spark/wiki/Wicked-Cool-Usage 101 | [holman]: https://twitter.com/holman 102 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 1.0.0 -------------------------------------------------------------------------------- /spark: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # spark 4 | # https://github.com/holman/spark 5 | # 6 | # Generates sparklines for a set of data. 7 | # 8 | # Here's 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 or space-separated list of data and then prints 14 | # a sparkline out of 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 | 27 | # Generates sparklines. 28 | # 29 | # $1 - The data we'd like to graph. 30 | _echo() 31 | { 32 | if [ "X$1" = "X-n" ]; then 33 | shift 34 | printf "%s" "$*" 35 | else 36 | printf "%s\n" "$*" 37 | fi 38 | } 39 | 40 | spark() 41 | { 42 | local n numbers= 43 | 44 | # find min/max values 45 | local min=0xffffffff max=0 46 | 47 | for n in ${@//,/ } 48 | do 49 | # on Linux (or with bash4) we could use `printf %.0f $n` here to 50 | # round the number but that doesn't work on OS X (bash3) nor does 51 | # `awk '{printf "%.0f",$1}' <<< $n` work, so just cut it off 52 | n=${n%.*} 53 | (( n < min )) && min=$n 54 | (( n > max )) && max=$n 55 | numbers=$numbers${numbers:+ }$n 56 | done 57 | 58 | # print ticks 59 | local ticks=(▁ ▂ ▃ ▄ ▅ ▆ ▇ █) 60 | 61 | # use a high tick if data is constant 62 | (( min == max )) && ticks=(▅ ▆) 63 | 64 | local f=$(( (($max-$min)<<8)/(${#ticks[@]}-1) )) 65 | (( f < 1 )) && f=1 66 | 67 | for n in $numbers 68 | do 69 | _echo -n ${ticks[$(( ((($n-$min)<<8)/$f) ))]} 70 | done 71 | _echo 72 | } 73 | 74 | # If we're being sourced, don't worry about such things 75 | if [ "$BASH_SOURCE" == "$0" ]; then 76 | # Prints the help text for spark. 77 | help() 78 | { 79 | local spark=$(basename $0) 80 | cat <<EOF 81 | 82 | USAGE: 83 | $spark [-h|--help] VALUE,... 84 | 85 | EXAMPLES: 86 | $spark 1 5 22 13 53 87 | ▁▁▃▂█ 88 | $spark 0,30,55,80,33,150 89 | ▁▂▃▄▂█ 90 | echo 9 13 5 17 1 | $spark 91 | ▄▆▂█▁ 92 | EOF 93 | } 94 | 95 | # show help for no arguments if stdin is a terminal 96 | if { [ -z "$1" ] && [ -t 0 ] ; } || [ "$1" == '-h' ] || [ "$1" == '--help' ] 97 | then 98 | help 99 | exit 0 100 | fi 101 | 102 | spark ${@:-`cat`} 103 | fi 104 | -------------------------------------------------------------------------------- /spark-test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env roundup 2 | 3 | describe "spark: Generates sparklines for a set of data." 4 | 5 | spark="./spark" 6 | 7 | it_shows_help_with_no_argv() { 8 | $spark | grep USAGE 9 | } 10 | 11 | it_graphs_argv_data() { 12 | graph="$($spark 1,5,22,13,5)" 13 | 14 | test $graph = '▁▂█▅▂' 15 | } 16 | 17 | it_charts_pipe_data() { 18 | data="0,30,55,80,33,150" 19 | graph="$(echo $data | $spark)" 20 | 21 | test $graph = '▁▂▃▄▂█' 22 | } 23 | 24 | it_charts_spaced_data() { 25 | data="0 30 55 80 33 150" 26 | graph="$($spark $data)" 27 | 28 | test $graph = '▁▂▃▄▂█' 29 | } 30 | 31 | it_charts_way_spaced_data() { 32 | data="0 30 55 80 33 150" 33 | graph="$($spark $data)" 34 | 35 | test $graph = '▁▂▃▄▂█' 36 | } 37 | 38 | it_handles_decimals() { 39 | data="5.5,20" 40 | graph="$($spark $data)" 41 | 42 | test $graph = '▁█' 43 | } 44 | 45 | it_charts_100_lt_300() { 46 | data="1,2,3,4,100,5,10,20,50,300" 47 | graph="$($spark $data)" 48 | 49 | test $graph = '▁▁▁▁▃▁▁▁▂█' 50 | } 51 | 52 | it_charts_50_lt_100() { 53 | data="1,50,100" 54 | graph="$($spark $data)" 55 | 56 | test $graph = '▁▄█' 57 | } 58 | 59 | it_charts_4_lt_8() { 60 | data="2,4,8" 61 | graph="$($spark $data)" 62 | 63 | test $graph = '▁▃█' 64 | } 65 | 66 | it_charts_no_tier_0() { 67 | data="1,2,3,4,5" 68 | graph="$($spark $data)" 69 | 70 | test $graph = '▁▂▄▆█' 71 | } 72 | 73 | it_equalizes_at_midtier_on_same_data() { 74 | data="1,1,1,1" 75 | graph="$($spark $data)" 76 | 77 | test $graph = '▅▅▅▅' 78 | } 79 | -------------------------------------------------------------------------------- /test: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # 3 | # Run roundup tests for spark. 4 | # 5 | 6 | roundup=$(which roundup) 7 | 8 | [ ! -z $roundup ] || { 9 | cat <<MESSAGE 1>&2 ; 10 | error: roundup missing 11 | 12 | Check out https://github.com/bmizerany/roundup for instructions on installing roundup. 13 | MESSAGE 14 | 15 | exit 1; 16 | } 17 | 18 | $roundup ./*-test.sh 19 | --------------------------------------------------------------------------------