table_format 0.0.2 → 0.0.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
  SHA256:
3
- metadata.gz: 4a86c63ac0f5f0dc4e3b9a03deb6be638b38a27775452cc7471bbc60fb68ca72
4
- data.tar.gz: 3560e586267ddd3d5f6718ba623008b5bcebc5430f9f5b165d03d6553481bcd2
3
+ metadata.gz: 43377dc26c9c8bcce5e79f94b1b7904a257715333e05766c764897ffc802338b
4
+ data.tar.gz: d60e49566d487bdfb6337e26ae1929a280b97c63f8d575e786a092a0546d80a0
5
5
  SHA512:
6
- metadata.gz: 7ffbc2bb7690dbaf1e785fee2801cd6326da3add0b14133b03cabc77f221943e223cd240457ed2c3993673671068e4c903be28191a0e2b28173147d669672976
7
- data.tar.gz: 4772e4975a09975ea1a953ea7302e119ce2da3539cd19e38b88c6cb39819cc11a11c632fff820fad0409bf90649ce0ec02ea92c4c02ffdfcb8a80f3b73f3c1a6
6
+ metadata.gz: cb34514fff9cbe378e5b5769ebab9236711f25ca3fd6996b8e57030fe339bb72f29723a19f737b1b1d314f20bedbcf75ce64de086a29a5ba076fde7483016832
7
+ data.tar.gz: eea73f8464ac8888fcdf29df78af6f92ae69a84c43b89dc78ad458731cbd42349f4357ebc2194e26ff08c3aa16f3be208198e4854eecbb113125f6922ae219e8
@@ -0,0 +1,11 @@
1
+ $LOAD_PATH << '../lib'
2
+ require 'table_format'
3
+
4
+ tp [
5
+ {id: 1, name: "foo\nbar" },
6
+ ]
7
+ # >> |----+----------|
8
+ # >> | id | name |
9
+ # >> |----+----------|
10
+ # >> | 1 | foo\nbar |
11
+ # >> |----+----------|
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH << '../lib'
2
+ require 'table_format'
3
+
4
+ TableFormat.default_options.update(truncate: 2)
5
+
6
+ tp [
7
+ {id: 1, name: "0123456789" },
8
+ ]
9
+ # >> |----+-------|
10
+ # >> | id | name |
11
+ # >> |----+-------|
12
+ # >> | 1 | 01... |
13
+ # >> |----+-------|
data/lib/table_format.rb CHANGED
@@ -8,7 +8,7 @@ else
8
8
  if defined?(ActiveSupport)
9
9
  ActiveSupport.on_load(:active_record) do
10
10
  include TableFormat::ActiveRecord
11
- ::ActiveRecord::Result.include TableFormat::ActiveRecordResult
11
+ ::ActiveRecord::Result.include(TableFormat::ActiveRecordResult)
12
12
 
13
13
  ::ActiveRecord::Relation.class_eval do
14
14
  def to_t(**options)
@@ -6,7 +6,7 @@ require 'kconv'
6
6
  module TableFormat
7
7
  mattr_accessor :default_options do
8
8
  {
9
- markdown: false,
9
+ markdown: false, # Same as {intersection: '|', :cover: false}
10
10
  header: nil,
11
11
  cover: true,
12
12
  vertical: '|',
@@ -14,6 +14,7 @@ module TableFormat
14
14
  intersection_both: '|',
15
15
  horizon: '-',
16
16
  padding: ' ',
17
+ truncate: 256,
17
18
  in_code: Kconv::UTF8,
18
19
  }
19
20
  end
@@ -74,6 +75,22 @@ module TableFormat
74
75
  @column_names = all_columns
75
76
  end
76
77
  @table_rows = @rows.collect { |e| e.values_at(*all_columns) }
78
+
79
+ @table_rows = @table_rows.collect do |values|
80
+ values.collect do |value|
81
+ if value
82
+ value = value.to_s
83
+ value = value.gsub(/\R/, "\\n")
84
+
85
+ if @options[:truncate]
86
+ if value.size > @options[:truncate]
87
+ value = value[0, @options[:truncate]] + "..."
88
+ end
89
+ end
90
+ end
91
+ value
92
+ end
93
+ end
77
94
  end
78
95
 
79
96
  def all_columns
@@ -1,3 +1,3 @@
1
1
  module TableFormat
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -65,6 +65,22 @@ EOT
65
65
  EOT
66
66
  end
67
67
 
68
+ it 'Convert line breaks to \n' do
69
+ TableFormat.generate(["a\nb"]).should == <<~'EOT'
70
+ |------|
71
+ | a\nb |
72
+ |------|
73
+ EOT
74
+ end
75
+
76
+ it 'truncate' do
77
+ TableFormat.generate(["0123"], truncate: 2).should == <<~EOT
78
+ |-------|
79
+ | 01... |
80
+ |-------|
81
+ EOT
82
+ end
83
+
68
84
  describe 'various to_t' do
69
85
  it 'hash array' do
70
86
  [{a: 1}].to_t.should == <<~EOT
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: table_format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - akicho8
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-28 00:00:00.000000000 Z
11
+ date: 2018-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -110,6 +110,8 @@ files:
110
110
  - examples/0120_default_options.rb
111
111
  - examples/0130_mongoid.rb
112
112
  - examples/0140_markdown_format.rb
113
+ - examples/0150_line_break.rb
114
+ - examples/0160_truncate.rb
113
115
  - lib/table_format.rb
114
116
  - lib/table_format/core_ext.rb
115
117
  - lib/table_format/generator.rb