collimator 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- collimator (0.0.1)
4
+ collimator (0.0.2)
5
5
  colored
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -22,7 +22,61 @@ Or install it yourself as:
22
22
 
23
23
  ## Usage
24
24
 
25
- TODO: Write usage instructions here
25
+ ### Table
26
+ see a few samples in `examples`.
27
+
28
+ Table.header("Collimator")
29
+ Table.header("Usage Example")
30
+ Table.header("Can have lots of headers")
31
+
32
+ Table.column('', :width => 18, :padding => 2, :justification => :right)
33
+ Table.column('numbers', :width => 14, :justification => :center)
34
+ Table.column('words', :width => 12, :justification => :left, :padding => 2)
35
+ Table.column('decimal', :width => 12, :justification => :decimal)
36
+
37
+ Table.row(['george', 123, 'holla', 12.5])
38
+ Table.row(['jim', 8, 'hi', 76.58])
39
+ Table.row(['robert', 10000, 'greetings', 0.2])
40
+
41
+ Table.footer("gotta love it", :justification => :center)
42
+
43
+ Table.tabulate
44
+
45
+ will result in...
46
+
47
+ +---------------------------------------------------------------+
48
+ | Collimator |
49
+ | Usage Example |
50
+ | Can have lots of headers |
51
+ +---------------------------------------------------------------+
52
+ | | numbers | words | decimal |
53
+ |--------------------+--------------+--------------+------------|
54
+ | george | 123 | holla | 12.5 |
55
+ | jim | 8 | hi | 76.58 |
56
+ | robert | 10000 | greetings | 0.2 |
57
+ +---------------------------------------------------------------+
58
+ | gotta love it |
59
+ +---------------------------------------------------------------+
60
+
61
+ ### Spinner
62
+
63
+ Spinner.spin
64
+ # ...
65
+ Spinner.stop
66
+
67
+ ### Progress Bar
68
+
69
+ ProgressBar.start({:min => 0, :max => 100, :method => :percent, :step_size => 10})
70
+
71
+ 0.upto(10) do
72
+ # ...
73
+ ProgressBar.increment
74
+ end
75
+
76
+ ProgressBar.complete
77
+
78
+
79
+ Better usage coming. in the mean time, tests might show best how to use.
26
80
 
27
81
  ## Contributing
28
82
 
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require 'collimator'
4
+
5
+ include Collimator
6
+
7
+ ProgressBar.start({:min => 0, :max => 100, :method => :percent, :step_size => 10})
8
+ 0.upto(10) do
9
+ sleep 0.5
10
+ ProgressBar.increment
11
+ end
12
+
13
+ sleep 0.5
14
+ ProgressBar.complete
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require 'collimator'
4
+
5
+ include Collimator
6
+
7
+ Spinner.spin
8
+ sleep 5
9
+ Spinner.stop
data/examples/table.rb ADDED
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env ruby
2
+ require "rubygems"
3
+ require 'collimator'
4
+
5
+ include Collimator
6
+
7
+ Table.header("Collimator")
8
+ Table.header("Usage Example")
9
+ Table.header("Can have lots of headers")
10
+
11
+ Table.column('', :width => 18, :padding => 2, :justification => :right)
12
+ Table.column('numbers', :width => 14, :justification => :center)
13
+ Table.column('words', :width => 12, :justification => :left, :padding => 2)
14
+ Table.column('decimal', :width => 12, :justification => :decimal)
15
+
16
+ Table.row(['george', 123, 'holla', 12.5])
17
+ Table.row(['jim', 8, 'hi', 76.58])
18
+ Table.row(['robert', 10000, 'greetings', 0.2])
19
+
20
+ Table.footer("gotta love it", :justification => :center)
21
+
22
+ Table.tabulate
@@ -1,3 +1,3 @@
1
1
  module Collimator
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/collimator.rb CHANGED
@@ -25,6 +25,7 @@ module Collimator
25
25
  @table_string = []
26
26
  @use_capture_string = false
27
27
  @use_capture_html = false
28
+ @last_header_color = '#EEEEEE'
28
29
 
29
30
  def self.live_update=(live_upate)
30
31
  @live_update = live_upate
@@ -51,9 +52,9 @@ module Collimator
51
52
  end
52
53
 
53
54
  def self.header(text, opts = {})
54
- width, padding, justification = parse_options(opts)
55
+ width, padding, justification, color = parse_options(opts)
55
56
 
56
- @headers << { :text => text, :padding => padding, :justification => justification }
57
+ @headers << { :text => text, :padding => padding, :justification => justification , :color => color}
57
58
  end
58
59
 
59
60
  def self.footer(text, opts)
@@ -125,7 +126,7 @@ module Collimator
125
126
 
126
127
  def self.prep_html_table
127
128
  @table_string = []
128
- @table_string << "<table>"
129
+ @table_string << "<table STYLE=\"font-family: helvetica, verdana, tahoma; border-collapse: collapse;\">"
129
130
  end
130
131
 
131
132
  def self.complete_html_table
@@ -317,11 +318,40 @@ module Collimator
317
318
  put_horizontal_line_with_dividers
318
319
  end
319
320
 
320
- def self.put_column_heading_text_html
321
- out = "<tr>\n"
321
+ def self.style_color(rgb)
322
+ luminance = get_luminance(rgb)
323
+ color = luminance < 50 ? '#EEEEEE' : '#222222'
324
+ color_style = "color: #{color};"
325
+ end
326
+
327
+ def self.style_header_border(rgb)
328
+ luminance = get_luminance(rgb)
329
+ color = luminance < 50 ? '#EEEEEE' : '#222222'
330
+ color_style = "border-bottom: 1px solid #{color};"
331
+ end
332
+
333
+ def self.get_luminance(rgb)
334
+ rgb_temp = rgb.gsub("#", '')
335
+ luminance = 0
336
+ if rgb_temp.length == 6
337
+ r = rgb_temp[0..1].hex
338
+ g = rgb_temp[2..3].hex
339
+ b = rgb_temp[4..5].hex
340
+ luminance = (0.299*r + 0.587*g + 0.114*b)
341
+ end
342
+ luminance
343
+ end
322
344
 
345
+ def self.put_column_heading_text_html
346
+ c = @last_header_color
347
+ text_color = style_color(c)
348
+ border_color = style_header_border(c)
349
+ out = "<tr STYLE=\"background-color: #{@last_header_color}; #{text_color} #{border_color}\">\n"
350
+ column = 0
323
351
  @column_names.each do |cname|
324
- out += "<th>#{cname}</th>\n"
352
+ padding_style = @columns[column][:padding] ? "STYLE=\"padding-left: #{@columns[column][:padding]}em; padding-right: #{@columns[column][:padding]}em;\"" : ""
353
+ out += "<th #{padding_style}>#{cname}</th>\n"
354
+ column += 1
325
355
  end
326
356
 
327
357
  out += "</tr>\n"
@@ -368,8 +398,11 @@ module Collimator
368
398
  end
369
399
 
370
400
  def self.make_header_line_html(data)
401
+ @last_header_color = data[:color] || @last_header_color
402
+
403
+ text_color = style_color(@last_header_color)
371
404
  header_line = "<tr>"
372
- header_line += "<th colspan='#{@column_names.count + 1}'>#{data[:text]}</th>"
405
+ header_line += "<th STYLE=\"background-color: #{@last_header_color}; #{text_color}\" colspan='#{@column_names.count + 1}'>#{data[:text]}</th>"
373
406
  header_line += "</tr>"
374
407
  header_line
375
408
  end
@@ -404,7 +437,9 @@ module Collimator
404
437
  row_string = "<tr>\n"
405
438
 
406
439
  row_data.each do | val |
407
- row_string += "<td>#{val}</td>\n"
440
+ style_info = @columns[column][:padding] ? " STYLE=\"padding-left: #{@columns[column][:padding]}em; padding-right: #{@columns[column][:padding]}em;\"" : ''
441
+ row_string += "<td#{style_info}>#{val}</td>\n"
442
+ column += 1
408
443
  end
409
444
 
410
445
  row_string += "</tr>"
data/test/table_test.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'test/unit'
2
- require 'collimator'
2
+ require File.expand_path("../../lib/collimator", __FILE__)
3
+
3
4
  require 'date'
4
5
  require 'stringio'
5
6
  require 'test_helper'
@@ -640,8 +641,56 @@ class TestTable < Test::Unit::TestCase
640
641
  assert_equal '+--------------------------------------------+', table_lines[13]
641
642
  end
642
643
 
643
- def test_html_output
644
+ def test_html_output_formatted
645
+ header_text = ['a', 'b', 'c', 'd']
646
+
647
+ table_header = 'header'
648
+
649
+ Table.header(table_header, :color => '#FF9933')
650
+
651
+ (0..header_text.length - 1).each do |i|
652
+ Table.column(header_text[i], :width => 10, :padding => 1.2, :justification => :center)
653
+ end
654
+
655
+ Table.row([1, 2, 3, 4])
656
+ Table.row([1.1, 2.1, 3.1, 4.1])
644
657
 
658
+ table_text = Table.tabulate_to_html
659
+
660
+ # puts table_text
661
+
662
+ table_lines = table_text.split("\n")
663
+ assert_equal 25, table_lines.length, 'the number of lines in the table output'
664
+
665
+ assert_equal '<table STYLE="font-family: helvetica, verdana, tahoma; border-collapse: collapse;">', table_lines[0]
666
+ assert_equal '<thead>', table_lines[1]
667
+ assert_equal "<tr><th STYLE=\"background-color: #FF9933; color: #222222;\" colspan='5'>header</th></tr>", table_lines[2]
668
+ assert_equal '<tr STYLE="background-color: #FF9933; color: #222222; border-bottom: 1px solid #222222;">', table_lines[3]
669
+ assert_equal '<th STYLE="padding-left: 1.2em; padding-right: 1.2em;">a</th>', table_lines[4]
670
+ assert_equal '<th STYLE="padding-left: 1.2em; padding-right: 1.2em;">b</th>', table_lines[5]
671
+ assert_equal '<th STYLE="padding-left: 1.2em; padding-right: 1.2em;">c</th>', table_lines[6]
672
+ assert_equal '<th STYLE="padding-left: 1.2em; padding-right: 1.2em;">d</th>', table_lines[7]
673
+ assert_equal '</tr>', table_lines[8]
674
+ assert_equal '</thead>', table_lines[9]
675
+ assert_equal '<tbody>', table_lines[10]
676
+ assert_equal '<tr>', table_lines[11]
677
+ assert_equal '<td STYLE="padding-left: 1.2em; padding-right: 1.2em;">1</td>', table_lines[12]
678
+ assert_equal '<td STYLE="padding-left: 1.2em; padding-right: 1.2em;">2</td>', table_lines[13]
679
+ assert_equal '<td STYLE="padding-left: 1.2em; padding-right: 1.2em;">3</td>', table_lines[14]
680
+ assert_equal '<td STYLE="padding-left: 1.2em; padding-right: 1.2em;">4</td>', table_lines[15]
681
+ assert_equal '</tr>', table_lines[16]
682
+ assert_equal '<tr>', table_lines[17]
683
+ assert_equal '<td STYLE="padding-left: 1.2em; padding-right: 1.2em;">1.1</td>', table_lines[18]
684
+ assert_equal '<td STYLE="padding-left: 1.2em; padding-right: 1.2em;">2.1</td>', table_lines[19]
685
+ assert_equal '<td STYLE="padding-left: 1.2em; padding-right: 1.2em;">3.1</td>', table_lines[20]
686
+ assert_equal '<td STYLE="padding-left: 1.2em; padding-right: 1.2em;">4.1</td>', table_lines[21]
687
+ assert_equal '</tr>', table_lines[22]
688
+ assert_equal '</tbody>', table_lines[23]
689
+ assert_equal '</table>', table_lines[24]
690
+
691
+ end
692
+
693
+ def xtest_html_output
645
694
  header_text = ['a', 'b', 'c', 'd']
646
695
 
647
696
  table_header = 'header'
@@ -657,8 +706,6 @@ class TestTable < Test::Unit::TestCase
657
706
 
658
707
  table_text = Table.tabulate_to_html
659
708
 
660
- #assert_equal "booty", table_text
661
-
662
709
  table_lines = table_text.split("\n")
663
710
  assert_equal 25, table_lines.length, 'the number of lines in the table output'
664
711
 
@@ -687,8 +734,6 @@ class TestTable < Test::Unit::TestCase
687
734
  assert_equal '</tr>', table_lines[22]
688
735
  assert_equal '</tbody>', table_lines[23]
689
736
  assert_equal '</table>', table_lines[24]
690
-
691
-
692
737
  end
693
738
 
694
739
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: collimator
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - geordie
@@ -15,11 +15,12 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2013-03-01 00:00:00 Z
18
+ date: 2013-05-03 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rdoc
22
- version_requirements: &id001 !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
23
24
  none: false
24
25
  requirements:
25
26
  - - ">="
@@ -29,11 +30,11 @@ dependencies:
29
30
  - 0
30
31
  version: "0"
31
32
  type: :development
32
- prerelease: false
33
- requirement: *id001
33
+ version_requirements: *id001
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: rake
36
- version_requirements: &id002 !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
37
38
  none: false
38
39
  requirements:
39
40
  - - ~>
@@ -45,11 +46,11 @@ dependencies:
45
46
  - 2
46
47
  version: 0.9.2
47
48
  type: :development
48
- prerelease: false
49
- requirement: *id002
49
+ version_requirements: *id002
50
50
  - !ruby/object:Gem::Dependency
51
51
  name: colored
52
- version_requirements: &id003 !ruby/object:Gem::Requirement
52
+ prerelease: false
53
+ requirement: &id003 !ruby/object:Gem::Requirement
53
54
  none: false
54
55
  requirements:
55
56
  - - ">="
@@ -59,8 +60,7 @@ dependencies:
59
60
  - 0
60
61
  version: "0"
61
62
  type: :runtime
62
- prerelease: false
63
- requirement: *id003
63
+ version_requirements: *id003
64
64
  description: Collimator
65
65
  email:
66
66
  - george.speake@gmail.com
@@ -79,6 +79,9 @@ files:
79
79
  - README.md
80
80
  - Rakefile
81
81
  - collimator.gemspec
82
+ - examples/progress_bar.rb
83
+ - examples/spinner.rb
84
+ - examples/table.rb
82
85
  - lib/collimator.rb
83
86
  - lib/collimator/version.rb
84
87
  - test/progressbar_test.rb