console_table 0.1.7 → 0.2.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: d29f00cea80b04a26dc82d97bfa2f6223a37966d
4
- data.tar.gz: 66059cc9c33ec69fe028422c3fc707a53fb94f29
3
+ metadata.gz: d31ea023e41a11349f456744423ec4ebf540102c
4
+ data.tar.gz: cba32a643b607437ece928dbed1dd818ea64d0ac
5
5
  SHA512:
6
- metadata.gz: 17cd329d715430896670cd02dcaf547af1cd93199192cee28d6b85e594b196e9d3152c7d0a24656a5a48dd331fae4ef5e52bb387bc42002e73379fd337afa490
7
- data.tar.gz: 6592e9a36084707eec3b715c90ff8118943be9a8c6d9b7ce7112a8503d5d511747e3aff97664164d07ccfe1053e777acc2ac0fd792356d81c8dceaafa702578b
6
+ metadata.gz: b607321df3045b48f45ca3e77570def4fa316d221d853a594c62b6bd689d9b93b493bc7a50ca420214d63f4cca97f96d7d674ce4e08b4463c0749f102daee9b8
7
+ data.tar.gz: 9382b05787a3453d447d00dc8e83a0a6bd5ec09d55553d69ae31af8409058640b61086ac90a9ffc75575ff9abac35ed254cd33db27422ae3cb2cfc5ab7dd11ac
data/README.md CHANGED
@@ -1,6 +1,5 @@
1
- # ConsoleTable
2
-
3
- [![Gem Version](https://badge.fury.io/rb/console_table.svg)](https://rubygems.org/gems/console_table) [![Build Status](https://travis-ci.org/rodhilton/console_table.svg?branch=master)](https://travis-ci.org/rodhilton/console_table)
1
+ ConsoleTable [![Gem Version](https://badge.fury.io/rb/console_table.svg)](https://rubygems.org/gems/console_table) [![Build Status](https://travis-ci.org/rodhilton/console_table.svg?branch=master)](https://travis-ci.org/rodhilton/console_table)
2
+ =========
4
3
 
5
4
  ConsoleTable is a helper class that allows you to print data to a console in a clean, table-like fashion. It's intended for use
6
5
  in commandline applications with information-dense output. It checks your terminal window size (or COLUMNS environment variable) to ensure
@@ -214,6 +213,7 @@ You can also add a title and a footer to the table, or indent the entire table w
214
213
 
215
214
  ```ruby
216
215
  require 'console_table'
216
+ require 'colorize'
217
217
 
218
218
  table_config = [
219
219
  {:key=>:title, :size=>15, :title=>"Movie Title"},
@@ -224,23 +224,23 @@ table_config = [
224
224
 
225
225
  ConsoleTable.define(table_config, :left_margin=>5, :right_margin=>10, :title=>"Movie Killers") do |table|
226
226
  table << {
227
- :title=>"Friday the 13th"
227
+ :title=>"Friday the 13th",
228
228
  :name=>{:text=>"Jason's Mom", :justify=>:left},
229
- :release_date=>"05-09-80".blue
229
+ :release_date=>"05-09-80".blue,
230
230
  :tagline=>{:text=>"They were warned...They are doomed...And on Friday the 13th, nothing will save them.", :ellipsize=>true}
231
231
  }
232
232
 
233
233
  table << {
234
234
  :title=>"Halloween".white.on_red,
235
235
  :name=>{:text=>"Michael Meyers", :justify=>:left},
236
- :release_date=>"10-25-80".blue
236
+ :release_date=>"10-25-80".blue,
237
237
  :tagline=>{:text=>"Everyone is entitled to one good scare", :ellipsize=>true}
238
238
  }
239
239
 
240
240
  table << {
241
- :title=>{:text=>"Nightmare on Elm St."}
241
+ :title=>{:text=>"Nightmare on Elm St."},
242
242
  :name=>{:text=>"Freddy Krueger", :justify=>:left},
243
- :release_date=>{text: "11-16-84".blue}
243
+ :release_date=>{text: "11-16-84".blue},
244
244
  :tagline=>{:text=>"A scream that wakes you up, might be your own", :ellipsize=>true}
245
245
  }
246
246
 
@@ -251,6 +251,7 @@ ConsoleTable.define(table_config, :left_margin=>5, :right_margin=>10, :title=>"M
251
251
  end
252
252
  ```
253
253
 
254
+ which yields:
254
255
 
255
256
  ```
256
257
  =================================================================
@@ -412,6 +413,7 @@ end
412
413
  ```
413
414
 
414
415
  Which yields:
416
+
415
417
  ```
416
418
  ================================================================================
417
419
  Name DOB Title
@@ -1,11 +1,11 @@
1
1
  # coding: utf-8
2
2
  lib = File.expand_path('../lib', __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'console_table'
4
+ # require 'console_table'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "console_table"
8
- spec.version = ConsoleTable::VERSION
8
+ spec.version = "0.2.0"
9
9
  spec.authors = ["Rod Hilton"]
10
10
  spec.email = ["consoletable@rodhilton.com"]
11
11
  spec.summary = %q{Simplifies printing tables of information to commandline consoles}
@@ -17,6 +17,7 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
+ spec.add_development_dependency 'simplecov', '~> 0.9'
20
21
  spec.add_development_dependency "bundler", "~> 1.5"
21
22
  spec.add_development_dependency 'rake', '~> 0'
22
23
  spec.add_development_dependency 'minitest', '~> 5.5'
data/lib/console_table.rb CHANGED
@@ -1,6 +1,24 @@
1
+ # ConsoleTable allows you to define a table with columns set to specific sizes,
2
+ # and then give that table rows of data which it will format into a view that fits
3
+ # inside a terminal/console.
4
+ #
5
+ # Author:: Rod Hilton
6
+ # License:: MIT
7
+ #
8
+ #--
9
+ # TODO: it's a little weird how, if a footer is too long, it simply doesn't print at all.
10
+ # This seems like not what someone would want to have happen.
11
+ # Could we take footer lines and split them every X characters where X is the working
12
+ # width, then use those? Long lines effectively wrap, but not on word boundaries
13
+ # TODO: if you're doing center or right-justification, should it trim from the sides or
14
+ # from the left, respectively?
15
+ #++
16
+
1
17
  module ConsoleTable
2
- VERSION = "0.1.7"
18
+ VERSION = "0.1.8"
3
19
 
20
+ # Define a console table. Requires a table layout which specifies column information
21
+ # like sizes, titles, and key names.
4
22
  def self.define(layout, options={}, &block)
5
23
  table = ConsoleTableClass.new(layout, options)
6
24
  table.send(:print_header)
@@ -9,8 +27,11 @@ module ConsoleTable
9
27
  end
10
28
 
11
29
  class ConsoleTableClass
30
+
31
+ # Add strings to the footer array to have them formatted when the table closes
12
32
  attr_reader :footer
13
33
 
34
+ # Add rows to the table. Prints immediately, consult documentation for examples
14
35
  def <<(options)
15
36
  print(options)
16
37
  end
@@ -92,6 +113,30 @@ module ConsoleTable
92
113
  end
93
114
  end
94
115
 
116
+ def print_footer()
117
+ if should_print_footer
118
+ print_line
119
+ end
120
+
121
+ footer_lines.each do |line|
122
+ if uncolorize(line).length <= @working_width
123
+ @out.print " " * @left_margin
124
+ @out.print "|" if @borders
125
+ @out.print format(@working_width, line, false, :right)
126
+ @out.print "|" if @borders
127
+ @out.print "\n"
128
+ end
129
+ end
130
+
131
+ if should_print_footer
132
+ print_line("=", "*", true)
133
+ else
134
+ print_line("=", "*", false)
135
+ end if @outline
136
+ end
137
+
138
+ private
139
+
95
140
  def print_headings()
96
141
  @headings_printed = true
97
142
 
@@ -137,30 +182,6 @@ module ConsoleTable
137
182
  end
138
183
  end
139
184
 
140
- #TODO: it's a little weird how, if a footer is too long, it simply doesn't print at all. This seems like not what someone would want to have happen.
141
- #Could we take footer lines and split them every X characters where X is the working width, then use those? Long lines effectively wrap, but not on word boundaries
142
- def print_footer()
143
- if should_print_footer
144
- print_line
145
- end
146
-
147
- footer_lines.each do |line|
148
- if uncolorize(line).length <= @working_width
149
- @out.print " " * @left_margin
150
- @out.print "|" if @borders
151
- @out.print format(@working_width, line, false, :right)
152
- @out.print "|" if @borders
153
- @out.print "\n"
154
- end
155
- end
156
-
157
- if should_print_footer
158
- print_line("=", "*", true)
159
- else
160
- print_line("=", "*", false)
161
- end if @outline
162
- end
163
-
164
185
  def should_print_footer
165
186
  footer_lines.length > 0 && footer_lines.any? { |l| uncolorize(l).length <= @working_width }
166
187
  end
@@ -321,7 +342,6 @@ module ConsoleTable
321
342
  string.gsub(/\e\[\d[^m]*?m/m, "")
322
343
  end
323
344
 
324
- #TODO: if you're doing center or right-justification, should it trim from the sides or from the left, respectively?
325
345
  def format(length, text, ellipsize=false, justify=:left)
326
346
  uncolorized = uncolorize(text)
327
347
  if uncolorized.length > length
@@ -354,8 +374,6 @@ module ConsoleTable
354
374
  color_active = true
355
375
  end
356
376
  final_string_parts << color_code
357
- else
358
- raise("Something very confusing happened")
359
377
  end
360
378
  current_index = current_index + 1
361
379
  end
@@ -385,13 +403,8 @@ module ConsoleTable
385
403
  @out.print "|" if @borders
386
404
 
387
405
  if to_print.is_a? String
388
- @out.print format(@working_width, normalize(to_print))
389
- elsif to_print.is_a? Hash
390
- text = normalize(to_print[:text]) || ""
391
- ellipsize = to_print[:ellipsize] || false
392
- justify = to_print[:justify] || :left
393
-
394
- @out.print format(@working_width, text, ellipsize, justify)
406
+ justify = infer_justify_from_string(to_print, :left)
407
+ @out.print format(@working_width, normalize(to_print), false, justify)
395
408
  end
396
409
 
397
410
  @out.print "|" if @borders
@@ -1,13 +1,15 @@
1
1
  # encoding: utf-8
2
+ require 'simplecov'
3
+ SimpleCov.start
4
+
2
5
  require 'minitest/autorun'
3
- #TODO: trimming from different sides depending on justification?
4
6
 
5
7
  class ConsoleTableTest < Minitest::Test
6
8
 
7
- require 'console_table'
8
- require 'colorize'
9
9
 
10
10
  def setup
11
+ require 'console_table'
12
+ require 'colorize'
11
13
  @mock_out = StringIO.new
12
14
  end
13
15
 
@@ -1260,8 +1262,6 @@ Row 2, Column 1 Row 2, Column 1
1260
1262
  ==========================================================
1261
1263
  END
1262
1264
 
1263
- puts @mock_out.string
1264
-
1265
1265
  assert_output_equal expected, @mock_out.string
1266
1266
 
1267
1267
  end
@@ -1288,6 +1288,86 @@ Row 2, Column 1 Row 2, Column 2
1288
1288
 
1289
1289
  end
1290
1290
 
1291
+ def test_can_print_basically_anything
1292
+ table_config = [
1293
+ {:key => :col1, :size => 20, :title => "Column 1"}
1294
+ ]
1295
+
1296
+ ConsoleTable.define(table_config, :width => 100, :output=>@mock_out) do |table|
1297
+ table << ["String"]
1298
+ table << [42]
1299
+ table << [0.3]
1300
+ table << [:why_not]
1301
+ table << [nil]
1302
+
1303
+ end
1304
+
1305
+ expected=<<-END
1306
+ ====================
1307
+ Column 1
1308
+ --------------------
1309
+ String
1310
+ 42
1311
+ 0.3
1312
+ why_not
1313
+
1314
+ ====================
1315
+ END
1316
+
1317
+ assert_output_equal expected, @mock_out.string
1318
+ end
1319
+
1320
+ def test_plain_line_can_be_justified
1321
+ table_config = [
1322
+ {:key => :col1, :size => 20, :title => "Column 1"}
1323
+ ]
1324
+
1325
+ ConsoleTable.define(table_config, :width => 30, :output => @mock_out) do |table|
1326
+ table << ["One"]
1327
+
1328
+ table << "Plain line"
1329
+ table << "Plain line\t"
1330
+ table << "\tPlain line\t"
1331
+ table << "\tPlain line"
1332
+
1333
+ table << ["Two"]
1334
+
1335
+ end
1336
+
1337
+ expected=<<-END
1338
+ ====================
1339
+ Column 1
1340
+ --------------------
1341
+ One
1342
+ Plain line
1343
+ Plain line
1344
+ Plain line
1345
+ Plain line
1346
+ Two
1347
+ ====================
1348
+ END
1349
+
1350
+ assert_output_equal expected, @mock_out.string
1351
+ end
1352
+
1353
+ def test_trimming_without_cutting_off_color_doesnt_result_in_extra_color_reset
1354
+ ConsoleTable.define([30], :width => 30, :output => @mock_out) do |table|
1355
+ table << ["This is way too long and #{"will".red} be truncated"]
1356
+ end
1357
+
1358
+ expected=<<-END
1359
+ ==============================
1360
+ Column 1
1361
+ ------------------------------
1362
+ This is way too long and will
1363
+ ==============================
1364
+ END
1365
+
1366
+ assert_includes @mock_out.string, "and \e[0;31;49mwill\e[0m \n"
1367
+
1368
+ assert_output_equal expected, @mock_out.string
1369
+ end
1370
+
1291
1371
  private
1292
1372
  def assert_output_equal(expected, actual)
1293
1373
  expected_lines = expected.split("\n")
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.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rod Hilton
@@ -10,6 +10,20 @@ bindir: bin
10
10
  cert_chain: []
11
11
  date: 2015-02-14 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: simplecov
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.9'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: bundler
15
29
  requirement: !ruby/object:Gem::Requirement