sparkr 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 13933a0323bc68a37ae398dbe0de249769d57d0e
4
- data.tar.gz: 96bd30bbd54ec4c09327e920ceb0046f47b9f77a
3
+ metadata.gz: a2b803ac74d70b8ad2eaf16e843476e2d9be2e6b
4
+ data.tar.gz: de4f7507f9f15f93464d88c12cb0747338a12895
5
5
  SHA512:
6
- metadata.gz: 6a75738f6a5420adba764234f409003e3f6d0e9db0de3f6217b316bb596f78f97a82f62b364656170ba88a24f819c7b74e388c3611c63021b649a0f4239b0369
7
- data.tar.gz: 8f21ae8ee6f677e6c2446045fa081a913cbda07756f00db5d3ebdeed9ac3daddcf38e60bb15b74f83747434d89a7311ffde5811d7d9e5ee5421485fb3d36558b
6
+ metadata.gz: 593c4038a8bf0fc0dbeee096d5f8ae725034c4a94ce30dd00fccae9cd169842519208d79a1a45fa64a17cd51493db2f9e65ed90443705666099c02e3d55c5c93
7
+ data.tar.gz: 5466b0f309cab197763c9e085c30b72185f907e377a18dd8a01c9c845e6ce208ad76bc516f6e757dd09f1bb0b1b6785346e8e8b5f9b2f4cc785a7bddae17c988
data/README.md CHANGED
@@ -1,10 +1,9 @@
1
- # Sparkr
2
-
3
- [![Build Status](https://travis-ci.org/rrrene/sparkr.png?branch=master)](https://travis-ci.org/rrrene/sparkr)
1
+ # Sparkr [![Build Status](https://travis-ci.org/rrrene/sparkr.png?branch=master)](https://travis-ci.org/rrrene/sparkr) [![Code Climate](https://codeclimate.com/github/rrrene/sparkr.png)](https://codeclimate.com/github/rrrene/sparkr)
4
2
 
5
3
  Sparkr is a port of [spark](https://github.com/holman/spark) for Ruby.
6
4
 
7
- It let's you create ASCII sparklines for your Ruby CLIs: ▁▂▃▅▇
5
+ It lets you create ASCII sparklines for your Ruby CLIs: ▁▂▃▅▇
6
+
8
7
 
9
8
 
10
9
  ## Installation
@@ -22,6 +21,7 @@ Or install it yourself as:
22
21
  $ gem install sparkr
23
22
 
24
23
 
24
+
25
25
  ## Usage
26
26
 
27
27
  ### Shell
@@ -37,7 +37,7 @@ you would expect:
37
37
 
38
38
  The real reason for this port:
39
39
 
40
- Sparkr.sparkline([0,30,55,80,33,150])
40
+ Sparkr.sparkline([0, 30, 55, 80, 33, 150])
41
41
  # => "▁▂▃▅▂▇"
42
42
 
43
43
 
@@ -45,9 +45,16 @@ The real reason for this port:
45
45
 
46
46
  Let's say you have your list of open and closed issues.
47
47
 
48
- list = [open_issue_count, closed_issue_count]
49
- Sparkr.sparkline(list)
50
- # => "▁█"
48
+ ```ruby
49
+ require 'sparkr'
50
+
51
+ open_issue_count = 3
52
+ closed_issue_count = 13
53
+
54
+ list = [open_issue_count, closed_issue_count]
55
+ puts "Issues: " + Sparkr.sparkline(list)
56
+ # => "Issues: ▁█"
57
+ ```
51
58
 
52
59
  But now you want to format the sparkline so that the open issues are red
53
60
  and the closed ones are green (to quickly see how you are doing).
@@ -56,19 +63,34 @@ Let's further suppose you use a gem that adds a `#color` method to `String`
56
63
  for ANSI coloring, like
57
64
  [Term::ANSIColor](https://github.com/flori/term-ansicolor).
58
65
 
59
- Sparkr.sparkline(list) do |tick, count, index|
60
- if index == 0
61
- tick.color(:red)
62
- else
63
- tick.color(:green)
64
- end
65
- end
66
- # => "▁█" (colored, trust me)
66
+ ```ruby
67
+ require 'sparkr'
68
+ require 'term/ansicolor'
69
+
70
+ class String
71
+ include Term::ANSIColor
72
+ end
73
+
74
+ open_issue_count = 3
75
+ closed_issue_count = 13
76
+
77
+ list = [open_issue_count, closed_issue_count]
78
+ sparkline = Sparkr.sparkline(list) do |tick, count, index|
79
+ if index == 0
80
+ tick.color(:red)
81
+ else
82
+ tick.color(:green)
83
+ end
84
+ end
85
+ puts "Issues: " + sparkline
86
+ # => "Issues: ▁█" (colored, trust me)
87
+ ```
67
88
 
68
89
  To see how this looks live and in full colour, take a look at
69
90
  [Inch](http://rrrene.github.io/inch).
70
91
 
71
92
 
93
+
72
94
  ## Contributing
73
95
 
74
96
  1. Fork it ( http://github.com/rrrene/sparkr/fork )
@@ -78,16 +100,19 @@ To see how this looks live and in full colour, take a look at
78
100
  5. Create new Pull Request
79
101
 
80
102
 
103
+
81
104
  ## Author
82
105
 
83
106
  René Föhring (@rrrene)
84
107
 
85
108
 
109
+
86
110
  ## Credits
87
111
 
88
112
  Sparkr would not exist without Zach Holman's [spark](https://github.com/holman/spark).
89
113
 
90
114
 
115
+
91
116
  ## License
92
117
 
93
118
  Sparkr is released under the MIT License. See the LICENSE.txt file for further
data/bin/sparkr CHANGED
@@ -1,10 +1,20 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- path = __FILE__
4
- while File.symlink?(path)
5
- path = File.expand_path(File.readlink(path), File.dirname(path))
3
+ # @return [String] the path of sparkr's lib directory
4
+ def find_lib_path
5
+ path = __FILE__
6
+ while File.symlink?(path)
7
+ path = File.expand_path(File.readlink(path), File.dirname(path))
8
+ end
9
+ File.join(File.dirname(File.expand_path(path)), '..', 'lib')
6
10
  end
7
- $:.unshift(File.join(File.dirname(File.expand_path(path)), '..', 'lib'))
11
+
12
+ # @return [Array<String>] the arguments passed or piped via the command-line
13
+ def cli_arguments
14
+ STDIN.tty? ? ARGV : STDIN.read.split(/\s+/)
15
+ end
16
+
17
+ $LOAD_PATH.unshift(find_lib_path)
8
18
 
9
19
  require 'sparkr'
10
- Sparkr::CLI.run(*ARGV)
20
+ Sparkr::CLI.run(*cli_arguments)
@@ -0,0 +1,7 @@
1
+ require 'sparkr'
2
+
3
+ open_issue_count = 3
4
+ closed_issue_count = 13
5
+
6
+ list = [open_issue_count, closed_issue_count]
7
+ puts "Issues: " + Sparkr.sparkline(list)
@@ -0,0 +1,24 @@
1
+ require 'sparkr'
2
+ #
3
+ # You need Term::ANSIColor for this example:
4
+ #
5
+ # $ gem install term-ansicolor
6
+ #
7
+ require 'term/ansicolor'
8
+
9
+ class String
10
+ include Term::ANSIColor
11
+ end
12
+
13
+ open_issue_count = 3
14
+ closed_issue_count = 13
15
+
16
+ list = [open_issue_count, closed_issue_count]
17
+ sparkline = Sparkr.sparkline(list) do |tick, count, index|
18
+ if index == 0
19
+ tick.color(:red)
20
+ else
21
+ tick.color(:green)
22
+ end
23
+ end
24
+ puts "Issues: " + sparkline
@@ -32,7 +32,7 @@ module Sparkr
32
32
  # line
33
33
  # # => "▁█" (colored, trust me)
34
34
  #
35
- # @param numbers [Array<String,Fixnum,Float>] the numbers to be rendered
35
+ # @param numbers [Array<String, Fixnum, Float>] the numbers to be rendered
36
36
  # @param &block [Proc] optional, can be used to format the rendered string
37
37
  #
38
38
  # @return [String]
@@ -2,8 +2,43 @@
2
2
 
3
3
  module Sparkr
4
4
  class CLI
5
+ # Helper method to run an instance with the given +args+
6
+ #
7
+ # @see #run
8
+ # @return [CLI] the instance that ran
5
9
  def self.run(*args)
6
- puts Sparkline.new(args.map(&:to_f)).to_s
10
+ instance = new
11
+ instance.run(*args)
12
+ instance
13
+ end
14
+
15
+ # Runs sparkr with the given +args+
16
+ # @return [void]
17
+ def run(*args)
18
+ if args.empty?
19
+ puts help
20
+ else
21
+ sparkline = Sparkline.new(args.map(&:to_f))
22
+ puts sparkline.to_s
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ # Returns usage information
29
+ def help
30
+ """
31
+ USAGE:
32
+ sparkr [-h|--help] VALUE,...
33
+
34
+ EXAMPLES:
35
+ sparkr 1 5 22 13 53
36
+ ▁▁▃▂█
37
+ sparkr 0,30,55,80,33,150
38
+ ▁▂▃▄▂█
39
+ echo 9 13 5 17 1 | sparkr
40
+ ▄▆▂█▁
41
+ """
7
42
  end
8
43
  end
9
44
  end
@@ -46,23 +46,17 @@ module Sparkr
46
46
  # # => "▁█" (colored, which you can't see)
47
47
  #
48
48
  # @return [Sparkline] itself
49
- def format(&block)
50
- new_ticks = []
51
- @ticks.each_with_index do |tick, index|
52
- if block.arity == 2
53
- new_ticks << yield(tick, @original_numbers[index])
54
- elsif block.arity == 3
55
- new_ticks << yield(tick, @original_numbers[index], index)
56
- end
49
+ def format
50
+ @ticks = @ticks.map.with_index do |tick, index|
51
+ yield tick, @original_numbers[index], index
57
52
  end
58
- @ticks = new_ticks
59
53
  self
60
54
  end
61
55
 
62
56
  # Returns the normalized equivalent of a given list
63
57
  #
64
- # normalize_numbers([3,4,7])
65
- # # => [0,1,4]
58
+ # normalize_numbers([3, 4, 7])
59
+ # # => [0, 1, 4]
66
60
  #
67
61
  # @return [Fixnum] the normalized equivalent of the given +_numbers+
68
62
  def normalize_numbers(_numbers)
@@ -90,7 +84,7 @@ module Sparkr
90
84
  TICKS.size - 1
91
85
  end
92
86
 
93
- # @param sep [String,nil] separator used to join the bars of the sparkline
87
+ # @param sep [String, nil] separator used to join the bars of the sparkline
94
88
  # @return [String] the sparkline, seperated by +sep+ (defaults to '')
95
89
  def to_s(sep = nil)
96
90
  @ticks.join(sep || DEFAULT_SEPARATOR)
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Sparkr
4
- VERSION = "0.3.2"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -3,12 +3,23 @@
3
3
  require File.expand_path(File.dirname(__FILE__) + '/../test_helper')
4
4
 
5
5
  describe ::Sparkr::CLI do
6
- it "should find work" do
6
+ it ".run should work with array of numbers as strings" do
7
+ string_numbers = %w(1 2 3 4 100 5 10 20 50 300)
7
8
  out, err = capture_io do
8
- ::Sparkr::CLI.run("1", "2", "3", "4", "100", "5", "10", "20", "50", "300")
9
+ ::Sparkr::CLI.run(*string_numbers)
9
10
  end
10
11
  refute out.empty?, "there should be some output"
11
12
  assert err.empty?, "there should be no errors"
12
13
  assert_equal "▁▁▁▁▃▁▁▁▂█\n", out
13
14
  end
15
+
16
+ it ".run should print usage information if args are missing" do
17
+ string_numbers = %w()
18
+ out, err = capture_io do
19
+ ::Sparkr::CLI.run(*string_numbers)
20
+ end
21
+ refute out.empty?, "there should be some output"
22
+ assert err.empty?, "there should be no errors"
23
+ assert_match /USAGE\:/, out
24
+ end
14
25
  end
@@ -10,7 +10,7 @@ describe ::Sparkr::Sparkline do
10
10
  end
11
11
 
12
12
  it "format should work with arity == 2" do
13
- sparkline = Sparkr::Sparkline.new([5.5,20])
13
+ sparkline = Sparkr::Sparkline.new([5.5, 20])
14
14
  sparkline.format do |tick, number|
15
15
  if number < 6.0
16
16
  tick = tick + "-"
@@ -22,7 +22,7 @@ describe ::Sparkr::Sparkline do
22
22
  end
23
23
 
24
24
  it "format should work with arity == 3" do
25
- sparkline = Sparkr::Sparkline.new([5.5,20])
25
+ sparkline = Sparkr::Sparkline.new([5.5, 20])
26
26
  sparkline.format do |tick, number, index|
27
27
  if index == 1
28
28
  tick = tick + "-"
@@ -4,23 +4,22 @@ require File.expand_path(File.dirname(__FILE__) + '/test_helper')
4
4
 
5
5
  describe ::Sparkr do
6
6
  it "should find work like holman/spark" do
7
- assert_sparkline '▁▂▃▄▅▆▇█', [1,2,3,4,5,6,7,8]
8
- assert_sparkline '▁▂█▅▂', [1,5,22,13,5]
9
- assert_sparkline '▁█', [5.5,20]
10
- assert_sparkline '▁▁▁▁▃▁▁▁▂█', [1,2,3,4,100,5,10,20,50,300]
11
- assert_sparkline '▁▄█', [1,50,100]
12
- assert_sparkline '▁▃█', [2,4,8]
13
- assert_sparkline '▁▂▃▄▂█', [0,30,55,80,33,150]
14
- assert_sparkline '▁▂▄▆█', [1,2,3,4,5]
7
+ assert_sparkline '▁▂▃▄▅▆▇█', [1, 2, 3, 4, 5, 6, 7, 8]
8
+ assert_sparkline '▁▂█▅▂', [1, 5, 22, 13, 5]
9
+ assert_sparkline '▁█', [5.5, 20]
10
+ assert_sparkline '▁▁▁▁▃▁▁▁▂█', [1, 2, 3, 4, 100, 5, 10, 20, 50, 300]
11
+ assert_sparkline '▁▄█', [1, 50, 100]
12
+ assert_sparkline '▁▃█', [2, 4, 8]
13
+ assert_sparkline '▁▂▃▄▂█', [0, 30, 55, 80, 33, 150]
14
+ assert_sparkline '▁▂▄▆█', [1, 2, 3, 4, 5]
15
15
  end
16
16
 
17
17
  it "should find work with equal numbers" do
18
18
  assert_sparkline '▁▁', [10, 10]
19
19
  end
20
20
 
21
-
22
21
  it ".sparkline should work with arity == 2" do
23
- sparkline = Sparkr.sparkline([5.5,20]) do |tick, number|
22
+ sparkline = Sparkr.sparkline([5.5, 20]) do |tick, number|
24
23
  if number < 6.0
25
24
  tick = tick + "-"
26
25
  else
@@ -31,7 +30,7 @@ describe ::Sparkr do
31
30
  end
32
31
 
33
32
  it ".sparkline should work with arity == 3" do
34
- sparkline = Sparkr.sparkline([5.5,20]) do |tick, number, index|
33
+ sparkline = Sparkr.sparkline([5.5, 20]) do |tick, number, index|
35
34
  if index == 1
36
35
  tick = tick + "-"
37
36
  else
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.3.2
4
+ version: 0.4.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-30 00:00:00.000000000 Z
11
+ date: 2014-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -55,6 +55,8 @@ files:
55
55
  - README.md
56
56
  - Rakefile
57
57
  - bin/sparkr
58
+ - examples/issues.rb
59
+ - examples/issues_colored.rb
58
60
  - lib/sparkr.rb
59
61
  - lib/sparkr/cli.rb
60
62
  - lib/sparkr/sparkline.rb
@@ -84,7 +86,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
86
  version: '0'
85
87
  requirements: []
86
88
  rubyforge_project:
87
- rubygems_version: 2.0.6
89
+ rubygems_version: 2.0.3
88
90
  signing_key:
89
91
  specification_version: 4
90
92
  summary: '["ASCII", "Sparklines", "in", "Ruby"]'