tabled 0.0.2 → 0.0.3

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
  SHA256:
3
- metadata.gz: a9bea1975c1f4f3cf411a90414b7497ac7a580dd0138ff42cf47f1019c42b21e
4
- data.tar.gz: 6b623b51a521cc9c1e6459420dce36080cfc0382269e4dddf5cd7849a0d55f73
3
+ metadata.gz: 4e5d0c20e1b0c91a2798fe10ae7f2d5da0de98c01bfff7d8af7e2cdc1e5673a2
4
+ data.tar.gz: fa10431595f5bb1dbcd57867cb6e8993424445b30c9d5939e9be523fec215f50
5
5
  SHA512:
6
- metadata.gz: 68fb74f92e12cb02a9b3c565e67fe482d4ee667bfeae7a9864c0ae2a9317ca0834a77edfe6f6757a83a9a14ae87b7f49703e43f314460954a064d2e8c56ce19b
7
- data.tar.gz: 59f967d55a04ef8695b3cfbbbcf06333c0c71a93644a260ffc94259152355e169cfb8d23ef6150ee258617837379ff8811c224b3184aabde920fc142035f6fe2
6
+ metadata.gz: 31f4a75497f308e805bfe282534bdd04fe37d2ddabb299eda69c1170fd353145b2b716d1991db5bcd173703ba4492e292098de242abea4914763421901bb1e50
7
+ data.tar.gz: cc8aee4ae5f3b0e3bfdba62e57e05d03f3fe6381093f370dba6187f0c85811e3a75a5f2363522fbc97f12b5971ac77658bafc402f05205ff2e7175021da9feda
@@ -17,13 +17,25 @@ permissions:
17
17
  contents: read
18
18
 
19
19
  jobs:
20
+ linters:
21
+ runs-on: ubuntu-latest
22
+ strategy:
23
+ matrix:
24
+ ruby-version: ['3.0']
25
+ steps:
26
+ - uses: actions/checkout@v3
27
+ - name: Set up Ruby
28
+ uses: ruby/setup-ruby@2b019609e2b0f1ea1a2bc8ca11cb82ab46ada124
29
+ with:
30
+ ruby-version: ${{ matrix.ruby-version }}
31
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
32
+ - name: Run tests
33
+ run: bundle exec rubocop
20
34
  test:
21
-
22
35
  runs-on: ubuntu-latest
23
36
  strategy:
24
37
  matrix:
25
38
  ruby-version: ['2.6', '2.7', '3.0']
26
-
27
39
  steps:
28
40
  - uses: actions/checkout@v3
29
41
  - name: Set up Ruby
data/.rubocop.yml ADDED
@@ -0,0 +1,7 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ require:
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ NewCops: enable
data/.rubocop_todo.yml ADDED
@@ -0,0 +1,69 @@
1
+ # This configuration was generated by
2
+ # `rubocop --auto-gen-config --auto-gen-only-exclude`
3
+ # on 2022-05-23 07:45:44 UTC using RuboCop version 1.29.1.
4
+ # The point is for the user to remove these configuration records
5
+ # one by one as the offenses are removed from the code base.
6
+ # Note that changes in the inspected code, or installation of new
7
+ # versions of RuboCop, may require this file to be generated again.
8
+
9
+ # Offense count: 1
10
+ # Configuration parameters: Include.
11
+ # Include: **/*.gemspec
12
+ Gemspec/RequiredRubyVersion:
13
+ Exclude:
14
+ - 'tabled.gemspec'
15
+
16
+ # Offense count: 3
17
+ # Configuration parameters: IgnoredMethods, CountRepeatedAttributes, Max.
18
+ Metrics/AbcSize:
19
+ Exclude:
20
+ - 'lib/content_shaper.rb'
21
+ - 'lib/helpers.rb'
22
+
23
+ # Offense count: 3
24
+ # Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods, IgnoredMethods.
25
+ # IgnoredMethods: refine
26
+ Metrics/BlockLength:
27
+ Exclude:
28
+ - '**/*.gemspec'
29
+ - 'spec/framed/tabled_spec.rb'
30
+ - 'spec/non_framed/tabled_spec.rb'
31
+
32
+ # Offense count: 2
33
+ # Configuration parameters: IgnoredMethods, Max.
34
+ Metrics/CyclomaticComplexity:
35
+ Exclude:
36
+ - 'lib/content_shaper.rb'
37
+ - 'lib/helpers.rb'
38
+
39
+ # Offense count: 4
40
+ # Configuration parameters: CountComments, Max, CountAsOne, ExcludedMethods, IgnoredMethods.
41
+ Metrics/MethodLength:
42
+ Exclude:
43
+ - 'lib/content_shaper.rb'
44
+ - 'lib/helpers.rb'
45
+ - 'spec/factories/factory_data.rb'
46
+
47
+ # Offense count: 2
48
+ # Configuration parameters: IgnoredMethods, Max.
49
+ Metrics/PerceivedComplexity:
50
+ Exclude:
51
+ - 'lib/content_shaper.rb'
52
+ - 'lib/helpers.rb'
53
+
54
+ # Offense count: 1
55
+ # Configuration parameters: Max, CountAsOne.
56
+ RSpec/ExampleLength:
57
+ Exclude:
58
+ - 'spec/framed/tabled_spec.rb'
59
+
60
+ # Offense count: 6
61
+ # Configuration parameters: AllowedConstants.
62
+ Style/Documentation:
63
+ Exclude:
64
+ - 'spec/**/*'
65
+ - 'test/**/*'
66
+ - 'lib/content_shaper.rb'
67
+ - 'lib/helpers.rb'
68
+ - 'lib/tabled.rb'
69
+ - 'lib/template.rb'
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ ### Version 0.0.3 (May 23, 2022)
2
+ 1. Printing titles of columns
3
+ 2. Fixing layout (amount of spaces for row separators)
4
+
5
+ ### Version 0.0.2 (May 16, 2022)
6
+ 1. Rendering footers of rows
7
+ 2. Adding optional frame to tables
8
+ 3. Adding row separator
9
+
10
+ ### Version 0.0.1 (May 11, 2022)
11
+ 1. Basic rendering tables to console
data/Gemfile CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  source 'http://rubygems.org'
2
4
 
3
- gemspec
5
+ gemspec
data/Gemfile.lock CHANGED
@@ -1,14 +1,72 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- tabled (0.0.1)
4
+ tabled (0.0.2)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
8
8
  specs:
9
+ addressable (2.4.0)
10
+ ast (2.4.2)
11
+ builder (3.2.4)
9
12
  byebug (11.1.3)
13
+ descendants_tracker (0.0.4)
14
+ thread_safe (~> 0.3, >= 0.3.1)
10
15
  diff-lcs (1.5.0)
11
16
  docile (1.4.0)
17
+ faraday (0.9.2)
18
+ multipart-post (>= 1.2, < 3)
19
+ git (1.11.0)
20
+ rchardet (~> 1.8)
21
+ github_api (0.16.0)
22
+ addressable (~> 2.4.0)
23
+ descendants_tracker (~> 0.0.4)
24
+ faraday (~> 0.8, < 0.10)
25
+ hashie (>= 3.4)
26
+ mime-types (>= 1.16, < 3.0)
27
+ oauth2 (~> 1.0)
28
+ hashie (5.0.0)
29
+ highline (2.0.3)
30
+ jeweler (2.3.9)
31
+ builder
32
+ bundler
33
+ git (>= 1.2.5)
34
+ github_api (~> 0.16.0)
35
+ highline (>= 1.6.15)
36
+ nokogiri (>= 1.5.10)
37
+ psych
38
+ rake
39
+ rdoc
40
+ semver2
41
+ jwt (2.3.0)
42
+ mime-types (2.99.3)
43
+ mini_portile2 (2.8.0)
44
+ multi_json (1.15.0)
45
+ multi_xml (0.6.0)
46
+ multipart-post (2.1.1)
47
+ nokogiri (1.13.6)
48
+ mini_portile2 (~> 2.8.0)
49
+ racc (~> 1.4)
50
+ oauth2 (1.4.8)
51
+ faraday (>= 0.8, < 3.0)
52
+ jwt (>= 1.0, < 3.0)
53
+ multi_json (~> 1.3)
54
+ multi_xml (~> 0.5)
55
+ rack (>= 1.2, < 3)
56
+ parallel (1.22.1)
57
+ parser (3.1.2.0)
58
+ ast (~> 2.4.1)
59
+ psych (4.0.4)
60
+ stringio
61
+ racc (1.6.0)
62
+ rack (2.2.3)
63
+ rainbow (3.1.1)
64
+ rake (13.0.6)
65
+ rchardet (1.8.0)
66
+ rdoc (6.4.0)
67
+ psych (>= 4.0.0)
68
+ regexp_parser (2.4.0)
69
+ rexml (3.2.5)
12
70
  rspec (3.11.0)
13
71
  rspec-core (~> 3.11.0)
14
72
  rspec-expectations (~> 3.11.0)
@@ -22,19 +80,40 @@ GEM
22
80
  diff-lcs (>= 1.2.0, < 2.0)
23
81
  rspec-support (~> 3.11.0)
24
82
  rspec-support (3.11.0)
83
+ rubocop (1.29.1)
84
+ parallel (~> 1.10)
85
+ parser (>= 3.1.0.0)
86
+ rainbow (>= 2.2.2, < 4.0)
87
+ regexp_parser (>= 1.8, < 3.0)
88
+ rexml (>= 3.2.5, < 4.0)
89
+ rubocop-ast (>= 1.17.0, < 2.0)
90
+ ruby-progressbar (~> 1.7)
91
+ unicode-display_width (>= 1.4.0, < 3.0)
92
+ rubocop-ast (1.18.0)
93
+ parser (>= 3.1.1.0)
94
+ rubocop-rspec (2.10.0)
95
+ rubocop (~> 1.19)
96
+ ruby-progressbar (1.11.0)
97
+ semver2 (3.4.2)
25
98
  simplecov (0.21.2)
26
99
  docile (~> 1.1)
27
100
  simplecov-html (~> 0.11)
28
101
  simplecov_json_formatter (~> 0.1)
29
102
  simplecov-html (0.12.3)
30
103
  simplecov_json_formatter (0.1.4)
104
+ stringio (3.0.2)
105
+ thread_safe (0.3.6)
106
+ unicode-display_width (2.1.0)
31
107
 
32
108
  PLATFORMS
33
109
  ruby
34
110
 
35
111
  DEPENDENCIES
36
112
  byebug
113
+ jeweler
37
114
  rspec
115
+ rubocop
116
+ rubocop-rspec
38
117
  simplecov
39
118
  tabled!
40
119
 
data/README.md CHANGED
@@ -1,14 +1,23 @@
1
- [![Gem Version](https://badge.fury.io/rb/tabled.svg)](https://badge.fury.io/rb/tabled) ![CI is pasing for ruby 2.6 - 3.0](https://github.com/rukomoynikov/tabled/actions/workflows/linters.yml/badge.svg)
1
+ [![Gem Version](https://badge.fury.io/rb/tabled.svg)](https://badge.fury.io/rb/tabled) ![CI is pasing for ruby 2.6 - 3.0](https://github.com/rukomoynikov/tabled/actions/workflows/linters.yml/badge.svg) ![Downloads](https://badgen.net/rubygems/dt/tabled)
2
2
 
3
3
 
4
4
  # Description
5
- This library can be used to render your data to a console. It's quite simple and has many features
5
+ Library can be used to render your data to a console. Though it's quite simple, but has many features.
6
6
 
7
7
  # How to use
8
8
  1. Install the gem `bundle add tabled` or `gem install tabled`
9
9
  2. Add to the application `require 'tabled''`
10
10
  3. Pass to the application array of rows. Each row may have any amount of columns and optional footer text.
11
11
 
12
+ ### Params for Tabled instance
13
+ Tabled accepts two params. Data which is prohibited and options.
14
+ Available options:
15
+ 1. `framed` - optional, default is true
16
+ 2. `row_separator` - optional, default is `-`. Can be `nil` if you don't need separate rows.
17
+ 3. `titles` - optional parameter with list of columns titles
18
+
19
+ More examples in [demo.rb](./demo.rb)
20
+
12
21
  ### Simple data structure
13
22
  ```ruby
14
23
  data = [
@@ -17,7 +26,8 @@ data = [
17
26
  ["Alan", "23 years", "Male"],
18
27
  ]
19
28
 
20
- Tabled.new(data).print_to_console
29
+ Tabled.new(data, framed: false,
30
+ row_separator: nil).print_to_console
21
31
  ```
22
32
 
23
33
  Result
@@ -31,25 +41,25 @@ Alan 23 years Male
31
41
  ```ruby
32
42
  data = [
33
43
  ["Helena", "20 years", "Female"],
34
- ["John", "18 years", "Male", { footer: "Legendary assassin John Wick (Keanu Reeves) retired from his violent career after marrying the love of his life." }],
44
+ ["John", "18 years", "Male", { footer: "Legendary assassin John Wick (Keanu Reeves)." }],
35
45
  ["Alan", "23 years", "Male"],
36
46
  ]
37
47
 
38
- Tabled.new(data).print_to_console
48
+ Tabled.new(data, row_separator: nil).print_to_console
39
49
  ```
40
50
 
41
51
  Result
42
52
  ```shell
43
- Helena 20 years Female
44
-
45
- John 18 years Male
46
- Legendary assassin John Wick.
47
-
48
- Alan 23 years Male
53
+ ----------------------------------------------------------------
54
+ | Helena 20 years Female |
55
+ | John 18 years Male |
56
+ | Legendary assassin John Wick (Keanu Reeves). |
57
+ | Alan 23 years Male |
58
+ ----------------------------------------------------------------
49
59
  ```
50
60
 
51
61
  # Contributing
52
- 1. Fork it ( http://github.com/rodhilton/console_table/fork )
62
+ 1. Fork it ( http://github.com/rukomoynikov/tabled/fork )
53
63
  2. Create your feature branch (git checkout -b my-new-feature)
54
64
  3. Commit your changes (git commit -am 'Add some feature')
55
65
  4. Push to the branch (git push origin my-new-feature)
data/demo.rb CHANGED
@@ -1,130 +1,186 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative './lib/tabled'
2
4
 
3
5
  STAR_WARS_CHARACTERS = [
4
6
  {
5
- "name": "Luke Skywalker",
6
- "height": "172",
7
- "mass": "77",
8
- "hair_color": "blond",
9
- "skin_color": "fair",
10
- "eye_color": "blue",
11
- "birth_year": "19BBY",
12
- "gender": "male",
7
+ name: 'Luke Skywalker',
8
+ height: '172',
9
+ mass: '77',
10
+ hair_color: 'blond',
11
+ skin_color: 'fair',
12
+ eye_color: 'blue',
13
+ birth_year: '19BBY',
14
+ gender: 'male'
13
15
  },
14
16
  {
15
- "name": "C-3PO",
16
- "height": "167",
17
- "mass": "75",
18
- "hair_color": "n/a",
19
- "skin_color": "gold",
20
- "eye_color": "yellow",
21
- "birth_year": "112BBY",
22
- "gender": "n/a",
17
+ name: 'C-3PO',
18
+ height: '167',
19
+ mass: '75',
20
+ hair_color: 'n/a',
21
+ skin_color: 'gold',
22
+ eye_color: 'yellow',
23
+ birth_year: '112BBY',
24
+ gender: 'n/a'
23
25
  },
24
26
  {
25
- "name": "R2-D2",
26
- "height": "96",
27
- "mass": "32",
28
- "hair_color": "n/a",
29
- "skin_color": "white, blue",
30
- "eye_color": "red",
31
- "birth_year": "33BBY",
32
- "gender": "n/a",
27
+ name: 'R2-D2',
28
+ height: '96',
29
+ mass: '32',
30
+ hair_color: 'n/a',
31
+ skin_color: 'white, blue',
32
+ eye_color: 'red',
33
+ birth_year: '33BBY',
34
+ gender: 'n/a'
33
35
  },
34
36
  {
35
- "name": "Darth Vader",
36
- "height": "202",
37
- "mass": "136",
38
- "hair_color": "none",
39
- "skin_color": "white",
40
- "eye_color": "yellow",
41
- "birth_year": "41.9BBY",
42
- "gender": "male",
37
+ name: 'Darth Vader',
38
+ height: '202',
39
+ mass: '136',
40
+ hair_color: 'none',
41
+ skin_color: 'white',
42
+ eye_color: 'yellow',
43
+ birth_year: '41.9BBY',
44
+ gender: 'male'
43
45
  },
44
46
  {
45
- "name": "Leia Organa",
46
- "height": "150",
47
- "mass": "49",
48
- "hair_color": "brown",
49
- "skin_color": "light",
50
- "eye_color": "brown",
51
- "birth_year": "19BBY",
52
- "gender": "female",
47
+ name: 'Leia Organa',
48
+ height: '150',
49
+ mass: '49',
50
+ hair_color: 'brown',
51
+ skin_color: 'light',
52
+ eye_color: 'brown',
53
+ birth_year: '19BBY',
54
+ gender: 'female'
53
55
  },
54
56
  {
55
- "name": "Owen Lars",
56
- "height": "178",
57
- "mass": "120",
58
- "hair_color": "brown, grey",
59
- "skin_color": "light",
60
- "eye_color": "blue",
61
- "birth_year": "52BBY",
62
- "gender": "male",
57
+ name: 'Owen Lars',
58
+ height: '178',
59
+ mass: '120',
60
+ hair_color: 'brown, grey',
61
+ skin_color: 'light',
62
+ eye_color: 'blue',
63
+ birth_year: '52BBY',
64
+ gender: 'male'
63
65
  },
64
66
  {
65
- "name": "Beru Whitesun lars",
66
- "height": "165",
67
- "mass": "75",
68
- "hair_color": "brown",
69
- "skin_color": "light",
70
- "eye_color": "blue",
71
- "birth_year": "47BBY",
72
- "gender": "female",
67
+ name: 'Beru Whitesun lars',
68
+ height: '165',
69
+ mass: '75',
70
+ hair_color: 'brown',
71
+ skin_color: 'light',
72
+ eye_color: 'blue',
73
+ birth_year: '47BBY',
74
+ gender: 'female'
73
75
  },
74
76
  {
75
- "name": "R5-D4",
76
- "height": "97",
77
- "mass": "32",
78
- "hair_color": "n/a",
79
- "skin_color": "white, red",
80
- "eye_color": "red",
81
- "birth_year": "unknown",
82
- "gender": "n/a",
77
+ name: 'R5-D4',
78
+ height: '97',
79
+ mass: '32',
80
+ hair_color: 'n/a',
81
+ skin_color: 'white, red',
82
+ eye_color: 'red',
83
+ birth_year: 'unknown',
84
+ gender: 'n/a'
83
85
  },
84
86
  {
85
- "name": "Biggs Darklighter",
86
- "height": "183",
87
- "mass": "84",
88
- "hair_color": "black",
89
- "skin_color": "light",
90
- "eye_color": "brown",
91
- "birth_year": "24BBY",
92
- "gender": "male",
87
+ name: 'Biggs Darklighter',
88
+ height: '183',
89
+ mass: '84',
90
+ hair_color: 'black',
91
+ skin_color: 'light',
92
+ eye_color: 'brown',
93
+ birth_year: '24BBY',
94
+ gender: 'male'
93
95
  },
94
96
  {
95
- "name": "Obi-Wan Kenobi",
96
- "height": "182",
97
- "mass": "77",
98
- "hair_color": "auburn, white",
99
- "skin_color": "fair",
100
- "eye_color": "blue-gray",
101
- "birth_year": "57BBY",
102
- "gender": "male",
97
+ name: 'Obi-Wan Kenobi',
98
+ height: '182',
99
+ mass: '77',
100
+ hair_color: 'auburn, white',
101
+ skin_color: 'fair',
102
+ eye_color: 'blue-gray',
103
+ birth_year: '57BBY',
104
+ gender: 'male'
103
105
  }
104
- ]
106
+ ].freeze
105
107
 
106
108
  print "Printing without any params: \n"
107
109
 
108
- data = STAR_WARS_CHARACTERS.map { |character|
110
+ data = STAR_WARS_CHARACTERS.map do |character|
109
111
  [character[:name], character[:height], character[:gender]]
110
- }
112
+ end
111
113
 
112
114
  Tabled.new(data).print_to_console
113
115
 
116
+ # ---------------------------------
117
+ # | Luke Skywalker 172 male |
118
+ # ---------------------------------
119
+ # | C-3PO 167 n/a |
120
+ # ---------------------------------
121
+ # | R2-D2 96 n/a |
122
+ # ---------------------------------
123
+ # | Darth Vader 202 male |
124
+ # ---------------------------------
125
+ # | Leia Organa 150 female |
126
+ # ---------------------------------
127
+ # | Owen Lars 178 male |
128
+ # ---------------------------------
129
+ # | Beru Whitesun lars 165 female |
130
+ # ---------------------------------
131
+ # | R5-D4 97 n/a |
132
+ # ---------------------------------
133
+ # | Biggs Darklighter 183 male |
134
+ # ---------------------------------
135
+ # | Obi-Wan Kenobi 182 male |
136
+ # ---------------------------------
137
+
114
138
  print "\n\n############################\n\n"
115
139
  print "Printing without frame: \n"
116
140
 
117
- data = STAR_WARS_CHARACTERS.map { |character|
141
+ data = STAR_WARS_CHARACTERS.map do |character|
118
142
  [character[:name], character[:height], character[:gender]]
119
- }
143
+ end
120
144
 
121
145
  Tabled.new(data, framed: false).print_to_console
122
146
 
147
+ # Luke Skywalker 172 male
148
+ # ---------------------------------
149
+ # C-3PO 167 n/a
150
+ # ---------------------------------
151
+ # R2-D2 96 n/a
152
+ # ---------------------------------
153
+ # Darth Vader 202 male
154
+ # ---------------------------------
155
+ # Leia Organa 150 female
156
+ # ---------------------------------
157
+ # Owen Lars 178 male
158
+ # ---------------------------------
159
+ # Beru Whitesun lars 165 female
160
+ # ---------------------------------
161
+ # R5-D4 97 n/a
162
+ # ---------------------------------
163
+ # Biggs Darklighter 183 male
164
+ # ---------------------------------
165
+ # Obi-Wan Kenobi 182 male
166
+ # ---------------------------------
167
+
123
168
  print "\n\n############################\n\n"
124
169
  print "Printing without frame: and row separator\n"
125
170
 
126
- data = STAR_WARS_CHARACTERS.map { |character|
127
- [character[:name], character[:height], character[:gender]]
128
- }
171
+ data = STAR_WARS_CHARACTERS.map do |character|
172
+ [character[:name], character[:height], character[:gender], { footer: 'One very long footer! Be carefull' }]
173
+ end
174
+
175
+ Tabled.new(data, titles: %w[Name Height Gender]).print_to_console
129
176
 
130
- Tabled.new(data, framed: false, row_separator: nil).print_to_console
177
+ # Luke Skywalker 172 male
178
+ # C-3PO 167 n/a
179
+ # R2-D2 96 n/a
180
+ # Darth Vader 202 male
181
+ # Leia Organa 150 female
182
+ # Owen Lars 178 male
183
+ # Beru Whitesun lars 165 female
184
+ # R5-D4 97 n/a
185
+ # Biggs Darklighter 183 male
186
+ # Obi-Wan Kenobi 182 male
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Tabled
2
4
  class ContentShaper
3
5
  attr_accessor :data, :options, :columns_width
@@ -9,25 +11,27 @@ class Tabled
9
11
  end
10
12
 
11
13
  def shape
12
- content =
13
- data
14
- .inject([]) { |enumerator, row|
15
- enumerator << Tabled::Template::Row.render(row, columns_width, options[:framed])
16
- enumerator << Tabled::Template::RowFooter.render(row, columns_width, options[:framed])
14
+ content = []
17
15
 
18
- # Row separator
19
- unless options[:row_separator].nil?
20
- enumerator << options[:row_separator].to_s * row_length
21
- end
16
+ unless options[:titles].empty?
17
+ content << Tabled::Template::Titles.render(options[:titles], columns_width, options[:framed])
18
+ content << (options[:row_separator].to_s * row_length) unless options[:row_separator].nil?
19
+ end
22
20
 
23
- enumerator
24
- }
25
- .compact
21
+ content.concat(
22
+ data
23
+ .each_with_object([]) do |row, enumerator|
24
+ enumerator << Tabled::Template::Row.render(row, columns_width, options[:framed])
25
+ enumerator << Tabled::Template::RowFooter.render(row, columns_width, options[:framed])
26
26
 
27
- content = add_left_and_right_borders(content)
28
- content = add_top_bottom_borders(content)
27
+ # Row separator
28
+ enumerator << (options[:row_separator].to_s * row_length) unless options[:row_separator].nil?
29
+ end
30
+ .compact
31
+ )
29
32
 
30
- content
33
+ content = add_left_and_right_borders(content)
34
+ add_top_bottom_borders(content)
31
35
  end
32
36
 
33
37
  private
@@ -37,19 +41,17 @@ class Tabled
37
41
  end
38
42
 
39
43
  def add_left_and_right_borders(content)
40
- content.inject([]) { |enumerator, row|
44
+ content.each_with_object([]) do |row, enumerator|
41
45
  # For a row separator all symbols are the same
42
- row_is_separator = row.split('').uniq.size == 1
43
- if row_is_separator && !options[:row_separator].nil?
44
- enumerator << (options[:row_separator] * 2) + row + options[:row_separator]
45
- elsif !options[:framed]
46
- enumerator << row
47
- else
48
- enumerator << ('| ' if options[:framed]) + row + ('|' if options[:framed])
49
- end
50
-
51
- enumerator
52
- }
46
+ row_is_separator = row.chars.uniq.size == 1
47
+ enumerator << if row_is_separator && !options[:row_separator].nil? && options[:framed]
48
+ (options[:row_separator] * 2) + row + options[:row_separator]
49
+ elsif !options[:framed]
50
+ row
51
+ else
52
+ ('| ' if options[:framed]) + row + ('|' if options[:framed])
53
+ end
54
+ end
53
55
  end
54
56
 
55
57
  def add_top_bottom_borders(content)
@@ -65,4 +67,4 @@ class Tabled
65
67
  end
66
68
  end
67
69
  end
68
- end
70
+ end
data/lib/helpers.rb CHANGED
@@ -1,31 +1,47 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Tabled
2
4
  class Helpers
3
5
  # Calculates columns size
4
- def self.calculate_columns_width(data)
6
+ def self.calculate_columns_width(data:, options:)
5
7
  columns_width = []
6
8
 
7
9
  data.each do |row|
8
- row_without_params = row[0..-2]
9
- row_without_params.each_with_index { |column, index|
10
- is_column_last = row_without_params.count == index + 1
11
- if is_column_last
12
- possible_new_value = [row.last.fetch(:footer, '').to_s.size, column.to_s.size].sort.last
13
- columns_width[index] = possible_new_value if possible_new_value > (columns_width[index] || 0)
14
- else
15
- columns_width[index] = column.to_s.size if column.to_s.size > (columns_width[index] || 0)
16
- end
17
- }
10
+ # [0..-2] Due to reason last item is always hash with row params
11
+ row[0..-2].each_with_index do |column, index|
12
+ columns_width[index] = column.to_s.size if column.to_s.size > (columns_width[index] || 0)
13
+ end
14
+ end
15
+
16
+ # If titles longer than data use titles column width
17
+ columns_width.each_with_index do |_column_size, index|
18
+ title_size = options[:titles].fetch(index, '').size
19
+
20
+ columns_width[index] = title_size > columns_width[index] ? title_size : columns_width[index]
18
21
  end
19
22
 
20
- columns_width.map { |column_width| column_width + 1 }
23
+ # Add 1 space in the end of each column
24
+ columns_width = columns_width.map { |column_width| column_width + 1 }
25
+ # Space in the last column is extra when table isn't framed
26
+ columns_width[-1] = columns_width[-1] - 1 unless options[:framed]
27
+
28
+ # Add to the last column footer spaces if needed
29
+ longest_footer_size = # longest footers
30
+ data.map do |row|
31
+ row.last.fetch(:footer, '')
32
+ end.max.to_s.size + 1
33
+ last_col_spaces = longest_footer_size - columns_width.sum # spaces required to the end
34
+ columns_width[-1] = columns_width.last + last_col_spaces if last_col_spaces.positive?
35
+
36
+ columns_width
21
37
  end
22
38
 
23
39
  # Add hash as a last element of the row
24
40
  def self.convert_to_required_structure(data)
25
- data.map { |row|
41
+ data.map do |row|
26
42
  row << {} unless row.last.is_a?(Hash)
27
43
  row
28
- }
44
+ end
29
45
  end
30
46
  end
31
- end
47
+ end
data/lib/tabled.rb CHANGED
@@ -1,21 +1,22 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'byebug'
2
4
  require_relative './template'
3
5
  require_relative './helpers'
4
6
  require_relative './content_shaper'
5
7
 
6
8
  class Tabled
7
- DEFAULT_OPTIONS = { framed: true, row_separator: '-' }
9
+ DEFAULT_OPTIONS = { framed: true, row_separator: '-', titles: [] }.freeze
8
10
  attr_accessor :data, :columns_width, :content, :options
9
11
 
10
12
  def initialize(data, **options)
11
13
  @options = DEFAULT_OPTIONS.merge(options)
12
14
  @data = Tabled::Helpers.convert_to_required_structure(data)
13
- @columns_width = Tabled::Helpers.calculate_columns_width(data)
14
- @content = Tabled::ContentShaper.new(data, @columns_width, @options).shape()
15
+ @columns_width = Tabled::Helpers.calculate_columns_width(data: data, options: @options)
16
+ @content = Tabled::ContentShaper.new(data, @columns_width, @options).shape
15
17
  end
16
18
 
17
-
18
19
  def print_to_console
19
20
  print content.join("\n")
20
21
  end
21
- end
22
+ end
data/lib/template.rb CHANGED
@@ -1,22 +1,37 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class Tabled
2
4
  class Template
3
5
  class Row
4
- def self.render(row, columns_width, is_framed)
5
- row[0..-2].map.with_index { |column, index|
6
+ # | Luke Skywalker 172 male |
7
+ def self.render(row, columns_width, _is_framed)
8
+ row[0..-2].map.with_index do |column, index|
9
+ spaces = ' ' * (columns_width[index] - column.to_s.size)
10
+ column.to_s + spaces
11
+ end.join
12
+ end
13
+ end
14
+
15
+ class Titles
16
+ # ------------------------------------
17
+ # | Name Height Gender |
18
+ # ------------------------------------
19
+ def self.render(row, columns_width, _is_framed)
20
+ row.map.with_index do |column, index|
6
21
  spaces = ' ' * (columns_width[index] - column.to_s.size)
7
22
  column.to_s + spaces
8
- }.join('')
23
+ end.join
9
24
  end
10
25
  end
11
26
 
12
27
  class RowFooter
13
- def self.render(row, columns_width, is_framed)
28
+ def self.render(row, columns_width, _is_framed)
14
29
  return nil unless row.last.fetch(:footer, false)
15
30
 
16
31
  footer = row.last.fetch(:footer)
17
- required_spaces = columns_width.sum() - footer.size
32
+ required_spaces = columns_width.sum - footer.size
18
33
  footer + (' ' * required_spaces)
19
34
  end
20
35
  end
21
36
  end
22
- end
37
+ end
data/tabled.gemspec CHANGED
@@ -1,19 +1,25 @@
1
+ # frozen_string_literal: true
2
+
1
3
  Gem::Specification.new do |s|
2
4
  s.name = 'tabled'
3
- s.version = '0.0.2'
4
- s.summary = "Library for rendering pretty tables in console"
5
- s.description = "This library can be used to render your data to a console. It's quite simple and has many features"
6
- s.authors = ["Max Rukomoynikov"]
5
+ s.version = '0.0.3'
6
+ s.summary = 'Library for rendering pretty tables in console'
7
+ s.description = "Library can be used to render your data to a console. Though it's quite simple, but has many features."
8
+ s.authors = ['Max Rukomoynikov']
7
9
  s.email = 'rukomoynikov@gmail.com'
8
10
  s.files =
9
11
  Dir.chdir(File.expand_path(__dir__)) do
10
12
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
11
13
  end
12
14
  s.homepage = 'https://rubygems.org/gems/tabled'
13
- s.metadata = { "source_code_uri" => "https://github.com/Rukomoynikov/tabled" }
14
- s.license = 'MIT'
15
+ s.metadata = { 'source_code_uri' => 'https://github.com/Rukomoynikov/tabled',
16
+ 'rubygems_mfa_required' => 'true' }
17
+ s.license = 'MIT'
15
18
 
16
- s.add_development_dependency "byebug"
17
- s.add_development_dependency "rspec"
18
- s.add_development_dependency "simplecov"
19
- end
19
+ s.add_development_dependency 'byebug'
20
+ s.add_development_dependency 'jeweler'
21
+ s.add_development_dependency 'rspec'
22
+ s.add_development_dependency 'rubocop'
23
+ s.add_development_dependency 'rubocop-rspec'
24
+ s.add_development_dependency 'simplecov'
25
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tabled
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
  - Max Rukomoynikov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-05-16 00:00:00.000000000 Z
11
+ date: 2022-05-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: jeweler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: rspec
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -38,6 +52,34 @@ dependencies:
38
52
  - - ">="
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
41
83
  - !ruby/object:Gem::Dependency
42
84
  name: simplecov
43
85
  requirement: !ruby/object:Gem::Requirement
@@ -52,8 +94,8 @@ dependencies:
52
94
  - - ">="
53
95
  - !ruby/object:Gem::Version
54
96
  version: '0'
55
- description: This library can be used to render your data to a console. It's quite
56
- simple and has many features
97
+ description: Library can be used to render your data to a console. Though it's quite
98
+ simple, but has many features.
57
99
  email: rukomoynikov@gmail.com
58
100
  executables: []
59
101
  extensions: []
@@ -62,6 +104,9 @@ files:
62
104
  - ".github/workflows/linters.yml"
63
105
  - ".gitignore"
64
106
  - ".rspec"
107
+ - ".rubocop.yml"
108
+ - ".rubocop_todo.yml"
109
+ - CHANGELOG.md
65
110
  - Gemfile
66
111
  - Gemfile.lock
67
112
  - README.md
@@ -76,6 +121,7 @@ licenses:
76
121
  - MIT
77
122
  metadata:
78
123
  source_code_uri: https://github.com/Rukomoynikov/tabled
124
+ rubygems_mfa_required: 'true'
79
125
  post_install_message:
80
126
  rdoc_options: []
81
127
  require_paths: