sparkr 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aee5d3645985bf8644aafd37b96c4b9431204b05
4
- data.tar.gz: 546ae2e5aba6ac1da35d03f931409184b0a5b31d
3
+ metadata.gz: c8dd99340dc0555b7c2a29e091f62e406fbe5233
4
+ data.tar.gz: 2bda5d1efce7cca0959a8a03805a20b20b9266ef
5
5
  SHA512:
6
- metadata.gz: 3689591a7bd6c57ccdd8219bb01ee32e5668eafdab825c44c747b0946beaed88ba5f9fac8ee90634c38668175eae093dbabfbb904d2743acc6c950f0ab21fddf
7
- data.tar.gz: 3ac3b156b8eb4445fd3b87db872b897f53d397c3e22b9ae16eec3038ba8557a3d07021255ec147208ddb820aadc092951d1a433a66cdd38ea217015b0e882362
6
+ metadata.gz: f886432e12a6623ec1bf5554518d482c9beeabbf80781c6ab18df3439aa10811366c161aad33f9d212bd842196728e8ecd4b3cf316f918fb905591a86bbf88cb
7
+ data.tar.gz: 257c9d14e88ac0abb66de3118c92fe73cc548a064987ea296d61b50ccd2712f32b5e3d725445ca945f17ce2435eb5b518c3cf3bf6d8986dea3d0d4cf503571fb
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.0
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Sparkr
2
2
 
3
+ [![Build Status](https://travis-ci.org/rrrene/sparkr.png?branch=master)](https://travis-ci.org/rrrene/sparkr)
4
+
3
5
  Sparkr is a port of [spark](https://github.com/holman/spark) for Ruby.
4
6
 
5
7
  It let's you create ASCII sparklines for your Ruby CLIs: ▁▂▃▅▇
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require "sparkr/cli"
2
4
  require "sparkr/sparkline"
3
5
  require "sparkr/version"
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Sparkr
2
4
  class CLI
3
5
  def self.run(*args)
@@ -1,19 +1,93 @@
1
+ # encoding: utf-8
2
+
1
3
  module Sparkr
2
4
  class Sparkline
3
5
  TICKS = %w(▁ ▂ ▃ ▄ ▅ ▆ ▇ █)
4
6
  DEFAULT_SEPARATOR = ''
5
7
 
6
8
  def initialize(_numbers)
7
- numbers = _numbers.map(&:to_i)
8
- step_height = (numbers.max - numbers.min) / (TICKS.size - 1).to_f
9
- step_height = 1 if step_height == 0
9
+ @original_numbers = _numbers
10
+
11
+ numbers = normalize_numbers(_numbers)
12
+ one_step = step_height(numbers)
10
13
 
11
14
  @ticks = numbers.map do |n|
12
- index = ((n - numbers.min) / step_height).to_i
15
+ index = (n / one_step).to_i
13
16
  TICKS[index]
14
17
  end
15
18
  end
16
19
 
20
+ # Formats all the ticks in the Sparkline with a given block,
21
+ # returns itself
22
+ #
23
+ # Example:
24
+ #
25
+ # Let's say you have a list of open and closed issues and want
26
+ # to format it so the open ones are red and the closed ones are
27
+ # green, so you can quickly see how you are doing. Let's further
28
+ # suppose you use a gem that adds a #color method to String
29
+ # for ANSI coloring.
30
+ #
31
+ # list = [open_issue_count, closed_issue_count]
32
+ # sparkline = Sparkr::Sparkline.new(list)
33
+ # sparkline.format do |tick, count, index|
34
+ # if index == 0
35
+ # tick.color(:red)
36
+ # else
37
+ # tick.color(:green)
38
+ # end
39
+ # end
40
+ #
41
+ # sparkline.to_s
42
+ # # => "▁█" (colored, which you can't see)
43
+ #
44
+ # @return [Sparkline] itself
45
+ def format(&block)
46
+ new_ticks = []
47
+ @ticks.each_with_index do |tick, index|
48
+ if block.arity == 2
49
+ new_ticks << yield(tick, @original_numbers[index])
50
+ elsif block.arity == 3
51
+ new_ticks << yield(tick, @original_numbers[index], index)
52
+ end
53
+ end
54
+ @ticks = new_ticks
55
+ self
56
+ end
57
+
58
+ # Returns the normalized equivalent of a given list
59
+ #
60
+ # normalize_numbers([3,4,7])
61
+ # # => [0,1,4]
62
+ #
63
+ # @return [Fixnum] the normalized equivalent of the given +_numbers+
64
+ def normalize_numbers(_numbers)
65
+ numbers = _numbers.map(&:to_i)
66
+ min = numbers.min
67
+ numbers.map do |n|
68
+ n - min
69
+ end
70
+ end
71
+
72
+ # @return [Float] the numerical "height" of a single bar "step"
73
+ def step_height(_numbers)
74
+ min, max = _numbers.minmax
75
+ actual_height = max - min
76
+ step = actual_height / steps.to_f
77
+ if step == 0
78
+ 1
79
+ else
80
+ step
81
+ end
82
+ end
83
+
84
+ # @return [Fixnum] the count of steps between the smallest and highest bar
85
+ def steps
86
+ TICKS.size - 1
87
+ end
88
+
89
+ # @param sep [String,nil] separator used to join the bars of the sparkline
90
+ # @return [String] the sparkline, seperated by +sep+ (defaults to '')
17
91
  def to_s(sep = nil)
18
92
  @ticks.join(sep || DEFAULT_SEPARATOR)
19
93
  end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  module Sparkr
2
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
3
5
  end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
4
 
3
5
  describe ::Sparkr::CLI do
@@ -1,7 +1,9 @@
1
+ # encoding: utf-8
2
+
1
3
  require File.expand_path(File.dirname(__FILE__) + '/test_helper')
2
4
 
3
5
  describe ::Sparkr do
4
- it "should find work" do
6
+ it "should find work like holman/spark" do
5
7
  assert_sparkline '▁▂▃▄▅▆▇█', [1,2,3,4,5,6,7,8]
6
8
  assert_sparkline '▁▂█▅▂', [1,5,22,13,5]
7
9
  assert_sparkline '▁█', [5.5,20]
@@ -11,4 +13,32 @@ describe ::Sparkr do
11
13
  assert_sparkline '▁▂▃▄▂█', [0,30,55,80,33,150]
12
14
  assert_sparkline '▁▂▄▆█', [1,2,3,4,5]
13
15
  end
16
+
17
+ it "should find work with equal numbers" do
18
+ assert_sparkline '▁▁', [10, 10]
19
+ end
20
+
21
+ it "format should work with arity == 2" do
22
+ sparkline = Sparkr::Sparkline.new([5.5,20])
23
+ sparkline.format do |tick, number|
24
+ if number < 6.0
25
+ tick = tick + "-"
26
+ else
27
+ tick
28
+ end
29
+ end
30
+ assert_equal '▁-█', sparkline.to_s
31
+ end
32
+
33
+ it "format should work with arity == 3" do
34
+ sparkline = Sparkr::Sparkline.new([5.5,20])
35
+ sparkline.format do |tick, number, index|
36
+ if index == 1
37
+ tick = tick + "-"
38
+ else
39
+ tick
40
+ end
41
+ end
42
+ assert_equal '▁█-', sparkline.to_s
43
+ end
14
44
  end
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  require 'simplecov'
2
4
  SimpleCov.start
3
5
 
@@ -12,6 +14,8 @@ def assert_sparkline(expected, numbers)
12
14
  actual = Sparkr.sparkline(numbers)
13
15
 
14
16
  assert actual.include?('▁'), "there must be a minimum"
15
- assert actual.include?('█'), "there must be a maximum"
17
+ unless numbers.uniq.size == 1 # all numbers are the same
18
+ assert actual.include?('█'), "there must be a maximum"
19
+ end
16
20
  assert_equal expected, actual
17
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparkr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - René Föhring
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-01-21 00:00:00.000000000 Z
11
+ date: 2014-01-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -49,6 +49,7 @@ files:
49
49
  - .gitignore
50
50
  - .ruby-gemset
51
51
  - .ruby-version
52
+ - .travis.yml
52
53
  - Gemfile
53
54
  - LICENSE.txt
54
55
  - README.md