console_table 0.1.1 → 0.1.2

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: 62dec8c67df25306a62e7c1ccaa8341a5ca603f0
4
- data.tar.gz: 7592bb485b9e110eab9395df2da50063625cad73
3
+ metadata.gz: 8df10271a0fee5c99ea39e43b65df2b2f7d4f047
4
+ data.tar.gz: 210bf58871ea3015c9ce28944018d4049d4dc89b
5
5
  SHA512:
6
- metadata.gz: 3331fee285ff870bcdd5e7ac8e44f7cb75a1b943a6ef2cbe656078a0ba44ea31bbad0c87c6e724dcad6a22ffd0788816bc789c031e5958dd5aaa3369d83d7f48
7
- data.tar.gz: 4738d8b0d88d6764df4e5e32e0314fa2829a0a79cfbb5df9fdc0367efa64d3a00e69fc9ed9ff37ab013dbff42337212a2805f8c074901203f6f26ba82b63f93d
6
+ metadata.gz: 9d05a9e405b606cb76de88b09be313ceccd17d847bf5880a96a8c6693b26e15bd287981e72ffe5fd52ed4921243aa9d5d43d67a53b526789f88e213b869028f9
7
+ data.tar.gz: cbd331465a4707a54852801b57a0d5f00bce0bb5e4d82a1ef546022781239cb4e09ec51f4c2fdcad73da1c51ada46ae7dd25cad15ac7937dcfbc35a5becf4eba
data/README.md CHANGED
@@ -9,7 +9,7 @@ It can be used to generate output similar to this screenshot:
9
9
 
10
10
  ![image](console_table_screenshot.png)
11
11
 
12
- You're able to specify left/right/center text justification, headers, footers, colors, and most importantly different sizes including exact character widths, screen percentages, and `*` for whatever is left. If the window resizes the class will notice and output all new lines with recalculated locations (previous lines are not re-printed).
12
+ You're able to specify left/right/center text justification, headers, footers, and most importantly different sizes including exact character widths, screen percentages, and `*` for whatever is left. If the window resizes the class will notice and output all new lines with recalculated locations (previous lines are not re-printed).
13
13
 
14
14
  **Note**: This project is _not_ like `hirb` and it makes no attempt to take any sort of ActiveRecord objects or an array of data and automatically fit the data to a nice table. It gives much, much more control over to the developer in how the output is formatted, but is much more difficult to work with as a trade-off - you will have to go through each element of your data set and manually munge it into the format needed by ConsoleTable to print a line. ConsoleTable is meant to save on a lot of math and calculation, but will do no analyzing of your data itself in order to format it, please consult the Usage section for more details.
15
15
 
@@ -123,8 +123,8 @@ table_config = [
123
123
 
124
124
  ConsoleTable.define(table_config) do |table|
125
125
  table << {
126
- :name=>{:text=>"Rod", :highlight=>{:regex=>/[A-Z]/, :color=>:red},
127
- :birthday=>{text: "04-14-80", :color=>:blue},
126
+ :name=>"Rod".colorize(:red) #Uses 'colorize' gem
127
+ :birthday=>"04-14-80".blue,
128
128
  :nickname=>{:text=>"Chainsaw", :justify=>:left},
129
129
  :motto=>{:text=>"This is a very long motto, I don't mind if it gets cut off but I'd like it to indicate as such with ellipses", :ellipsize=>true}
130
130
  }
@@ -138,9 +138,10 @@ will output
138
138
  Name DOB Nickname(s) Motto
139
139
  -------------------------------------------------------------------------------
140
140
  Rod 04-14-80 Chainsaw This is a very long motto, I don't...
141
+ ===============================================================================
141
142
  ```
142
143
 
143
- Due to limitations of this readme format, you'll have to take my word for it that the capital letters in the Name are highlighted Red, and the DOB is blue.
144
+ Due to limitations of this readme format, you'll have to take my word for it that the colors placed by the 'colorize' gem are preserved, and that the table correctly handles resetting ANSI colors when truncating messages that have formatting.
144
145
 
145
146
  You can also add a title and a footer to the table, or indent the entire table within the window using different options. Again, here's another example that should more-or-less speak for itself.
146
147
 
@@ -157,23 +158,23 @@ table_config = [
157
158
 
158
159
  ConsoleTable.define(table_config, :left_margin=>5, :right_margin=>10, :title=>"Movie Killers") do |table|
159
160
  table << {
160
- :title=>{:text=>"Friday the 13th", :highlight=>{:regex=>/[A-Z]/, :color=>:red}},
161
+ :title=>"Friday the 13th"
161
162
  :name=>{:text=>"Jason's Mom", :justify=>:left},
162
- :release_date=>{text: "05-09-80", :color=>:blue},
163
+ :release_date=>"05-09-80".blue
163
164
  :tagline=>{:text=>"They were warned...They are doomed...And on Friday the 13th, nothing will save them.", :ellipsize=>true}
164
165
  }
165
166
 
166
167
  table << {
167
- :title=>{:text=>"Halloween", :highlight=>{:regex=>/[A-Z]/, :color=>:red}, :background=>:orange},
168
+ :title=>"Halloween".white.on_red,
168
169
  :name=>{:text=>"Michael Meyers", :justify=>:left},
169
- :release_date=>{text: "10-25-80", :color=>:blue},
170
+ :release_date=>"10-25-80".blue
170
171
  :tagline=>{:text=>"Everyone is entitled to one good scare", :ellipsize=>true}
171
172
  }
172
173
 
173
174
  table << {
174
- :title=>{:text=>"Nightmare on Elm St.", :highlight=>{:regex=>/[A-Z]/, :color=>:red}, :background=>:orange},
175
+ :title=>{:text=>"Nightmare on Elm St."}
175
176
  :name=>{:text=>"Freddy Krueger", :justify=>:left},
176
- :release_date=>{text: "11-16-84", :color=>:blue},
177
+ :release_date=>{text: "11-16-84".blue}
177
178
  :tagline=>{:text=>"A scream that wakes you up, might be your own", :ellipsize=>true}
178
179
  }
179
180
 
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency 'rake', '~> 0'
22
22
  spec.add_development_dependency 'simplecov', '~> 0.9'
23
23
  spec.add_development_dependency 'minitest', '~> 5.5'
24
+ spec.add_development_dependency 'colorize', '~> 0.7'
24
25
 
25
- spec.add_runtime_dependency 'colorize', '~> 0.7'
26
26
  spec.add_runtime_dependency 'ruby-terminfo', '~> 0.1'
27
27
  end
data/lib/console_table.rb CHANGED
@@ -1,8 +1,7 @@
1
- require 'colorize'
2
1
  require 'terminfo'
3
2
 
4
3
  module ConsoleTable
5
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
6
5
 
7
6
  def self.define(layout, options={}, &block)
8
7
  table = ConsoleTableClass.new(layout, options)
@@ -58,11 +57,7 @@ module ConsoleTable
58
57
  if not @title.nil? and @title.length <= @working_width
59
58
  @out.print " "*@left_margin
60
59
  @out.print "|" if @borders
61
- left_side = (@working_width - @title.uncolorize.length)/2
62
- right_side = (@working_width - @title.uncolorize.length) - left_side
63
- @out.print " "*left_side
64
- @out.print @title
65
- @out.print " "*right_side
60
+ @out.print format(@working_width, @title, false, :center)
66
61
  @out.print "|" if @borders
67
62
  @out.print "\n"
68
63
  print_line if @borders
@@ -98,7 +93,7 @@ module ConsoleTable
98
93
  @out.print join_char
99
94
  @column_widths.each_with_index do |column, i|
100
95
  @out.print char*column[:size]
101
- if(edge_join_only and i < @column_widths.length - 1)
96
+ if edge_join_only and i < @column_widths.length - 1
102
97
  @out.print char
103
98
  else
104
99
  @out.print join_char
@@ -118,11 +113,10 @@ module ConsoleTable
118
113
  end
119
114
 
120
115
  footer_lines.each do |line|
121
- if line.uncolorize.length <= @working_width
116
+ if uncolorize(line).length <= @working_width
122
117
  @out.print " " * @left_margin
123
118
  @out.print "|" if @borders
124
- @out.print " " * (@working_width - line.uncolorize.length)
125
- @out.print line
119
+ @out.print format(@working_width, line, false, :right)
126
120
  @out.print "|" if @borders
127
121
  @out.print "\n"
128
122
  end
@@ -136,7 +130,7 @@ module ConsoleTable
136
130
  end
137
131
 
138
132
  def should_print_footer
139
- footer_lines.length > 0 && footer_lines.any? { |l| l.uncolorize.length <= @working_width }
133
+ footer_lines.length > 0 && footer_lines.any? { |l| uncolorize(l).length <= @working_width }
140
134
  end
141
135
 
142
136
  def footer_lines
@@ -144,7 +138,7 @@ module ConsoleTable
144
138
  @footer.each do |line|
145
139
  lines = line.split("\n")
146
140
  lines.each do |l|
147
- footer_lines << l.strip unless l.nil? or l.uncolorize.strip == ""
141
+ footer_lines << l.strip unless l.nil? or uncolorize(l).strip == ""
148
142
  end
149
143
  end
150
144
  footer_lines
@@ -152,7 +146,7 @@ module ConsoleTable
152
146
 
153
147
  def print(options)
154
148
 
155
- if (options.is_a? String)
149
+ if options.is_a? String
156
150
  print_plain(options)
157
151
  return
158
152
  end
@@ -181,36 +175,12 @@ module ConsoleTable
181
175
  if to_print.is_a? String
182
176
  @out.print format(column[:size], normalize(to_print), false, justify)
183
177
  elsif to_print.is_a? Hash
184
- color = to_print[:color] || :default
185
- background = to_print[:background] || :default
186
178
  text = normalize(to_print[:text]) || ""
187
179
  ellipsize = to_print[:ellipsize] || false
188
- highlight = to_print[:highlight]
189
180
  justify = to_print[:justify] || justify #can override
190
- mode = to_print[:mode] || :default
191
181
 
192
182
  formatted=format(column[:size], text, ellipsize, justify)
193
183
 
194
- if color != :default
195
- formatted = formatted.colorize(color)
196
- end
197
-
198
- if background != :default
199
- formatted = formatted.colorize(:background => background)
200
- end
201
-
202
- if mode != :default
203
- formatted = formatted.colorize(:mode => mode)
204
- end
205
-
206
- unless highlight.nil?
207
- highlight_regex = to_print[:highlight][:regex] || /wontbefoundbecauseit'sgobbledygookblahblahblahbah/
208
- highlight_color = to_print[:highlight][:color] || :blue
209
- highlight_background = to_print[:highlight][:background] || :default
210
-
211
- formatted = formatted.gsub(highlight_regex, '\0'.colorize(:color => highlight_color, :background => highlight_background))
212
- end
213
-
214
184
  @out.print formatted
215
185
  else
216
186
  @out.print format(column[:size], normalize(to_print.to_s))
@@ -287,23 +257,64 @@ module ConsoleTable
287
257
  @working_width = (@column_widths.inject(0) { |res, c| res+c[:size] }) + @column_widths.length - 1
288
258
  end
289
259
 
260
+
261
+ def uncolorize(string)
262
+ string.gsub(/\e\[\d.*?m/m, "")
263
+ end
264
+
265
+ #TODO: if you're doing center or right-justification, should it trim from the sides or from the left, respectively?
290
266
  def format(length, text, ellipsize=false, justify=:left)
291
- if text.length > length
267
+ uncolorized = uncolorize(text)
268
+ if uncolorized.length > length
269
+
292
270
  if ellipsize
293
- text[0, length-3] + '...'
271
+ goal_length = length-3
294
272
  else
295
- text[0, length]
273
+ goal_length = length
274
+ end
275
+
276
+ parts = text.scan(/(\e\[\d.*?m)|(.)/) #The idea here is to break the string up into control codes and single characters
277
+ #We're going to now count up until we hit goal length, but we're not going to ever count control codes against the count
278
+ #We're also going to keep track of if the last thing was a color code, so we know to reset if a color is "active"
279
+
280
+ #I can't think of a better way to do this, it's probably dumb
281
+ current_length = 0
282
+ current_index = 0
283
+ final_string_parts = []
284
+ color_active = false
285
+
286
+ while current_length < goal_length
287
+ color_code, regular_text = parts[current_index]
288
+ if not regular_text.nil?
289
+ current_length = current_length + 1
290
+ final_string_parts << regular_text
291
+ elsif not color_code.nil?
292
+ if color_code == "\e[0m"
293
+ color_active = false if color_active
294
+ else
295
+ color_active = true
296
+ end
297
+ final_string_parts << color_code
298
+ else
299
+ raise("Something very confusing happened")
300
+ end
301
+ current_index = current_index + 1
296
302
  end
303
+
304
+ final_string_parts << "..." if ellipsize
305
+ final_string_parts << "\e[0m" if color_active
306
+
307
+ final_string_parts.join("")
297
308
  else
298
309
  if justify == :right
299
- (" "*(length-text.length)) + text
310
+ (" "*(length-uncolorized.length)) + text
300
311
  elsif justify == :center
301
- space = length-text.length
312
+ space = length-uncolorized.length
302
313
  left_side = space/2
303
314
  right_side = space - left_side
304
315
  (" " * left_side) + text + (" "*right_side)
305
316
  else #assume left
306
- text + (" "*(length-text.length))
317
+ text + (" "*(length-uncolorized.length))
307
318
  end
308
319
  end
309
320
  end
@@ -317,18 +328,11 @@ module ConsoleTable
317
328
  if to_print.is_a? String
318
329
  @out.print format(@working_width, normalize(to_print))
319
330
  elsif to_print.is_a? Hash
320
- color = to_print[:color] || :default
321
- background = to_print[:background] || :default
322
331
  text = normalize(to_print[:text]) || ""
323
332
  ellipsize = to_print[:ellipsize] || false
324
333
  justify = to_print[:justify] || :left
325
- mode = to_print[:mode] || :default
326
334
 
327
- formatted=format(@working_width, text, ellipsize, justify)
328
- if text != :default or background != :default or mode != :default
329
- formatted = formatted.colorize(:color => color, :background => background, :mode => mode)
330
- end
331
- @out.print formatted
335
+ @out.print format(@working_width, text, ellipsize, justify)
332
336
  end
333
337
 
334
338
  @out.print "|" if @borders
@@ -1,5 +1,6 @@
1
1
  require 'minitest/autorun'
2
2
  require 'console_table'
3
+ require 'colorize'
3
4
 
4
5
  class ConsoleTableTest < Minitest::Test
5
6
 
@@ -512,7 +513,7 @@ One Two
512
513
 
513
514
  ConsoleTable.define(table_config, :left_margin=>10, :right_margin=>7, :width=> 100, :title=>"Test Title", :borders=>true, :output=>@mock_out) do |table|
514
515
  (1..5).each do |row|
515
- table << (1..4).collect{|i| "Row #{row}, Column #{i}"}
516
+ table << (1..4).collect{|i| "Row #{row}, Column #{i}".red}
516
517
  end
517
518
 
518
519
  table << "Plain line needs borders"
@@ -614,6 +615,48 @@ Row 5, Column 1 Row 5, Colu
614
615
  assert_output_equal expected, @mock_out.string
615
616
  end
616
617
 
618
+ def test_can_use_colors_without_affecting_layout
619
+ table_config = [
620
+ {:key=>:col1, :size=>10, :title=>"Column 1", :justify=>:left},
621
+ {:key=>:col2, :size=>10, :title=>"Column 2", :justify=>:center},
622
+ {:key=>:col3, :size=>10, :title=>"Column 3", :justify=>:right},
623
+ ]
624
+
625
+ ConsoleTable.define(table_config, :width=> 120, :output=>@mock_out) do |table|
626
+ table << ["Short".blue, "Short".bold, "Short".red.on_blue]
627
+
628
+ table << ["Much much longer".blue, "Much much longer".bold, "Much much longer".red.on_blue]
629
+
630
+ table << [
631
+ {:text=>"Much much longer".blue, :ellipsize=>true},
632
+ {:text=>"Much much longer".underline, :ellipsize=>true},
633
+ {:text=>"Much much longer".on_magenta, :ellipsize=>true}
634
+ ]
635
+
636
+ table << [
637
+ {:text=>"Much much longer".yellow, :ellipsize=>true},
638
+ {:text=>"Normal, should reset", :ellipsize=>true},
639
+ {:text=>"Much much longer".bold, :ellipsize=>true}
640
+ ]
641
+ end
642
+
643
+ expected=<<-END
644
+ ================================
645
+ Column 1 Column 2 Column 3
646
+ --------------------------------
647
+ Short Short Short
648
+ Much much Much much Much much
649
+ Much mu... Much mu... Much mu...
650
+ Much mu... Normal,... Much mu...
651
+ ================================
652
+ END
653
+
654
+ assert_includes @mock_out.string, "\e[1;39;49mShort\e[0m" #Should have normal color codes
655
+ assert_includes @mock_out.string, "\e[0;33;49mMuch mu...\e[0m" #the cut-off one should keep the color code for ellipses, then reset
656
+
657
+ assert_output_equal expected, @mock_out.string
658
+ end
659
+
617
660
  private
618
661
  def assert_output_equal(expected, actual)
619
662
  expected_lines = expected.split("\n")
@@ -621,7 +664,7 @@ Row 5, Column 1 Row 5, Colu
621
664
  assert_equal expected_lines.length, actual_lines.length
622
665
  expected_lines.each_with_index do |expected_line, i|
623
666
  actual_line = actual_lines[i]
624
- assert_equal expected_line.rstrip, actual_line.rstrip
667
+ assert_equal expected_line.uncolorize.rstrip, actual_line.uncolorize.rstrip
625
668
  end
626
669
 
627
670
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console_table
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rod Hilton
@@ -73,7 +73,7 @@ dependencies:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0.7'
76
- type: :runtime
76
+ type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements: