console_table 0.2.2 → 0.2.3

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: 668c3bea75d37322b4d956247d640d60677182c6
4
- data.tar.gz: 4362c0f568da256a1020868bae1c15b34754ab21
3
+ metadata.gz: 20cc9ca26b036bfe447968e6e351a9c701a22e56
4
+ data.tar.gz: 3dff948fbf2f07604b93189d323b6d55a69efba3
5
5
  SHA512:
6
- metadata.gz: d03313dda6802650143631a93a7df42e672def160ab475075c92097540edc5f617d3b811cfb80ee60b91d5123070d34e28902acf6e6f0e1926e2fdfb95d876af
7
- data.tar.gz: 3e96bdee594c595645402385863345487dc5f3285f78e11cd6557293e021c02283e73f392513f2a1bfb0055d4ce4e2eb4c29a93e8e53b1a9ebb989aeeee772b6
6
+ metadata.gz: 1da3fe51b14f347d17e7670840d00a13c73599e4642518f9ae94769205dc74c222676fbad746b3c4719d476c8125131eaa4a2aac6f94b1d38606f462ad4b5bd0
7
+ data.tar.gz: 920c48265acf2873c2ff40e86d17ea250a6ef8641b278bc4af148e380951984c625d50dd0c904b646ee3c50f0f2a9301c0ae2f31042db981ebd0033e96ed16fd
@@ -4,7 +4,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "console_table"
7
- spec.version = "0.2.2"
7
+ spec.version = "0.2.3"
8
8
  spec.authors = ["Rod Hilton"]
9
9
  spec.email = ["consoletable@rodhilton.com"]
10
10
  spec.summary = %q{Simplifies printing tables of information to commandline consoles}
Binary file
@@ -0,0 +1,41 @@
1
+ require 'console_table'
2
+ require 'faker'
3
+ require 'colorize'
4
+
5
+ # Just a goofy example that simulates a big table of purchase data.
6
+
7
+ layout = [
8
+ {size: 6, title: "Pur.#"},
9
+ {size: 20, title: "Customer"},
10
+ {size: 13, title: "Product Code"},
11
+ {size: "*", title: "Product Name", ellipsize: true},
12
+ {size: 5, title: "Price", justify: :right},
13
+ {size: 3, title: "Qty"},
14
+ {size: 8, title: "Date"},
15
+ {size: 11, title: "Profit/Loss", justify: :right}
16
+ ]
17
+
18
+ sum = 0
19
+
20
+ ConsoleTable.define(layout, :title=>"Product Purchases".upcase.bold.green.underline) do |table|
21
+ (0..25).each do |i|
22
+ name = Faker::Name.name
23
+ profit = (rand(1*100000)-30000)/100.00
24
+ table << [
25
+ sprintf("%010d", rand(1..999999999)).blue,
26
+ name,
27
+ Faker::Code.ean.red,
28
+ Faker::Commerce.product_name,
29
+ sprintf("%#.2f", Faker::Commerce.price).bold.green,
30
+ rand(1..20).to_s.magenta,
31
+ Faker::Date.backward(14).strftime("%m/%d/%y").yellow,
32
+ sprintf("%#.2f", profit).colorize(profit < 0 ? :red : :green)
33
+ ]
34
+
35
+ sum = sum + profit
36
+
37
+ sleep(rand(1..10)/20.0)
38
+ end
39
+
40
+ table.footer << "Total Profit: #{sprintf("%#.2f", sum).to_s.colorize(sum < 0 ? :red : :green)}"
41
+ end
@@ -0,0 +1,48 @@
1
+ require 'console_table'
2
+
3
+ require 'json'
4
+ require 'net/http'
5
+ require 'open-uri'
6
+ require 'colorize'
7
+
8
+ symbols = ["YHOO", "AAPL", "GOOG", "MSFT", "C", "MMM", "KO", "WMT", "GM", "IBM", "MCD", "VZ", "HD", "DIS", "INTC"]
9
+
10
+ params = symbols.collect{|s| "\"#{s}\"" }.join(",")
11
+ url = "http://query.yahooapis.com/v1/public/yql?q=select * from yahoo.finance.quotes where symbol in (#{params})&env=http://datatables.org/alltables.env&format=json"
12
+ uri = URI.parse(URI::encode(url))
13
+ response = Net::HTTP.get_response(uri)
14
+ json = JSON.parse(response.body)
15
+
16
+ table_config = [
17
+ {:key=>:symbol, :title=>"Symbol", :size=>6},
18
+ {:key=>:name, :title=>"Name", :size=>17},
19
+ {:key=>:price, :title=>"Price", :size=>5, :justify=>:right},
20
+ {:key=>:change, :title=>"Change", :size=>7, :justify=>:right},
21
+ {:key=>:recommendation, :title=>"Recommendation", :size=>15, :justify=>:right}
22
+ ]
23
+
24
+ ConsoleTable.define(table_config, :title=>"Stock Prices") do |table|
25
+
26
+ json["query"]["results"]["quote"].each do |j|
27
+ change = j["ChangeRealtime"]
28
+ if change.start_with?("+")
29
+ change = change.green
30
+ else
31
+ change = change.red
32
+ end
33
+
34
+ recommendation = (rand() <= 0.5) ? "BUY!".white.on_green.bold.underline : "Sell".yellow
35
+
36
+ table << [
37
+ j["Symbol"].magenta,
38
+ j["Name"],
39
+ j["LastTradePriceOnly"],
40
+ change,
41
+ recommendation
42
+ ]
43
+
44
+ end
45
+
46
+ table.footer << "Recommendations randomly generated"
47
+
48
+ end
@@ -65,6 +65,7 @@ module ConsoleTable
65
65
  @left_margin = options[:left_margin] || 0
66
66
  @right_margin = options[:right_margin] || 0
67
67
  @headings = options[:headings].nil? ? true : options[:headings]
68
+ @ellipse = options[:ellipse] || "..."
68
69
 
69
70
  #Set outline, just the upper and lower lines
70
71
  if @borders
@@ -335,8 +336,10 @@ module ConsoleTable
335
336
  uncolorized = uncolorize(text)
336
337
  if uncolorized.length > length
337
338
 
339
+ ellipsize = false if uncolorize(@ellipse).length >= length
340
+
338
341
  if ellipsize
339
- goal_length = length-3
342
+ goal_length = length-uncolorize(@ellipse).length
340
343
  else
341
344
  goal_length = length
342
345
  end
@@ -367,7 +370,7 @@ module ConsoleTable
367
370
  current_index = current_index + 1
368
371
  end
369
372
 
370
- final_string_parts << "..." if ellipsize
373
+ final_string_parts << @ellipse if ellipsize
371
374
  final_string_parts << "\e[0m" if color_active
372
375
 
373
376
  final_string_parts.join("")
@@ -7,6 +7,9 @@ require 'minitest/autorun'
7
7
  #--
8
8
  # TODO: if you're doing center or right-justification, should it trim from the sides or
9
9
  # from the left, respectively?
10
+ # TODO: "compact" option, which when true makes tables output like they do here, but when
11
+ # compact is FALSE and borders is true, there's an extra space between the | and the text
12
+ # but only in the middle - on the sides the | goes up against the margin
10
13
  #++
11
14
  class ConsoleTableTest < Minitest::Test
12
15
 
@@ -1471,6 +1474,129 @@ Fake:... Fake: 1
1471
1474
  assert_output_equal expected, @mock_out.string
1472
1475
  end
1473
1476
 
1477
+ def test_can_custom_ellipse
1478
+ table_config = [
1479
+ {:key => :col1, :size => 10, :title => "Column 1", :ellipsize=>true},
1480
+ {:key => :col2, :size => 10, :title => "Column 2", :ellipsize=>true, :justify=>:right},
1481
+ ]
1482
+
1483
+ ConsoleTable.define(table_config, :width => 100, :ellipse=>"…", :output=>@mock_out) do |table|
1484
+ table << {
1485
+ :col1 => "Row 1, Column 1",
1486
+ :col2 => "Row 1, Column 2"
1487
+ }
1488
+
1489
+ table << {
1490
+ :col1 => {text: "Row 2, Column 1", :ellipsize=>false},
1491
+ :col2 => {text: "Row 2, Column 1", :ellipsize=>false}
1492
+ }
1493
+ end
1494
+
1495
+ expected=<<-END
1496
+ =====================
1497
+ Column 1 Column 2
1498
+ ---------------------
1499
+ Row 1, Co… Row 1, Co…
1500
+ Row 2, Col Row 2, Col
1501
+ =====================
1502
+ END
1503
+
1504
+ assert_output_equal expected, @mock_out.string
1505
+ end
1506
+
1507
+ def test_can_use_colorized_custom_ellipse_for_some_reason
1508
+ table_config = [
1509
+ {:key => :col1, :size => 10, :title => "Column 1", :ellipsize=>true},
1510
+ {:key => :col2, :size => 10, :title => "Column 2", :ellipsize=>true, :justify=>:right},
1511
+ ]
1512
+
1513
+ ConsoleTable.define(table_config, :width => 100, :ellipse=>"…".red, :output=>@mock_out) do |table|
1514
+ table << {
1515
+ :col1 => "Row 1, Column 1",
1516
+ :col2 => "Row 1, Column 2"
1517
+ }
1518
+
1519
+ table << {
1520
+ :col1 => {text: "Row 2, Column 1", :ellipsize=>false},
1521
+ :col2 => {text: "Row 2, Column 1", :ellipsize=>false}
1522
+ }
1523
+ end
1524
+
1525
+ expected=<<-END
1526
+ =====================
1527
+ Column 1 Column 2
1528
+ ---------------------
1529
+ Row 1, Co… Row 1, Co…
1530
+ Row 2, Col Row 2, Col
1531
+ =====================
1532
+ END
1533
+
1534
+ assert_includes @mock_out.string, "\e[0;31;49m…\e[0m"
1535
+
1536
+ assert_output_equal expected, @mock_out.string
1537
+ end
1538
+
1539
+ def test_can_use_extra_long_ellipse
1540
+ table_config = [
1541
+ {:key => :col1, :size => 10, :title => "Column 1", :ellipsize=>true},
1542
+ {:key => :col2, :size => 10, :title => "Column 2", :ellipsize=>true, :justify=>:right},
1543
+ ]
1544
+
1545
+ ConsoleTable.define(table_config, :width => 100, :ellipse=>" (cont)", :output=>@mock_out) do |table|
1546
+ table << {
1547
+ :col1 => "Row 1, Column 1",
1548
+ :col2 => "Row 1, Column 2"
1549
+ }
1550
+
1551
+ table << {
1552
+ :col1 => {text: "Row 2, Column 1", :ellipsize=>false},
1553
+ :col2 => {text: "Row 2, Column 1", :ellipsize=>false}
1554
+ }
1555
+ end
1556
+
1557
+ expected=<<-END
1558
+ =====================
1559
+ Column 1 Column 2
1560
+ ---------------------
1561
+ Row (cont) Row (cont)
1562
+ Row 2, Col Row 2, Col
1563
+ =====================
1564
+ END
1565
+
1566
+
1567
+ assert_output_equal expected, @mock_out.string
1568
+ end
1569
+
1570
+ def test_ellipse_char_too_long_for_area_is_ignored_entirely
1571
+ table_config = [
1572
+ {:key => :col1, :size => 10, :title => "Column 1", :ellipsize=>true},
1573
+ {:key => :col2, :size => 10, :title => "Column 2", :ellipsize=>true, :justify=>:right},
1574
+ ]
1575
+
1576
+ ConsoleTable.define(table_config, :width => 100, :ellipse=>" (this is a long ellipse)", :output=>@mock_out) do |table|
1577
+ table << {
1578
+ :col1 => "Row 1, Column 1",
1579
+ :col2 => "Row 1, Column 2"
1580
+ }
1581
+
1582
+ table << {
1583
+ :col1 => {text: "Row 2, Column 1", :ellipsize=>false},
1584
+ :col2 => {text: "Row 2, Column 1", :ellipsize=>false}
1585
+ }
1586
+ end
1587
+
1588
+ expected=<<-END
1589
+ =====================
1590
+ Column 1 Column 2
1591
+ ---------------------
1592
+ Row 1, Col Row 1, Col
1593
+ Row 2, Col Row 2, Col
1594
+ =====================
1595
+ END
1596
+
1597
+ assert_output_equal expected, @mock_out.string
1598
+ end
1599
+
1474
1600
  private
1475
1601
  def assert_output_equal(expected, actual)
1476
1602
  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.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rod Hilton
@@ -111,6 +111,8 @@ files:
111
111
  - Rakefile
112
112
  - console_table.gemspec
113
113
  - console_table_screenshot.png
114
+ - examples/purchases.rb
115
+ - examples/stocks.rb
114
116
  - lib/console_table.rb
115
117
  - test/test_console_table.rb
116
118
  homepage: https://github.com/rodhilton/console_table