nazar 0.1.3 → 1.1.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
  SHA256:
3
- metadata.gz: ca22ec5fbbb7a1b310bf6ce3c13f3b7141ba3ebbb6148ad2ee38f7d597c709d5
4
- data.tar.gz: cc3b88da51bd75718661913f23d8aae323c759ffef8ab1d43d7469b471056216
3
+ metadata.gz: 2689adb3080ba628ef27ab5ce71804352ccb02b2a90dbf363a50373210f7b2bd
4
+ data.tar.gz: e8a4f1954eb5d67a62e1f54d4a9983f7d2cb034903bd24d5a7d2dfed626d0808
5
5
  SHA512:
6
- metadata.gz: 3fcd672ea8ab0537ead3bfb865bc297383739b4b48596b51b7929b49fa8e95d85a0e610ab405b8aaca3ff6180347553a71a1444218486e87f2289084f15be084
7
- data.tar.gz: e2eea43c25abe63d516122c5fc927e8d4c5d68b1727a2f3c0e1a5569998819174b027199aa4ae7e33ccc4e93d63adef1053ae753b73b2741c04b09633a4624b8
6
+ metadata.gz: ee1752541ac4356428bce4298bb5a6ff011b83943784ed0727adeec0c82b9763465944324dd8672d5cfb27e0a28e05e44f2dc796a9a28d5459086804b5c1a7c4
7
+ data.tar.gz: 7eb125d122b3c424251eb6ef76554d5737d24509659d12c1414cab0ad48fb59f790b63a0c8cf264273b10f3786efb7b1397db2549a3ef4dc8af6963ef4e7e62c
checksums.yaml.gz.sig CHANGED
Binary file
data/.rubocop.yml CHANGED
@@ -12,4 +12,6 @@ Metrics/BlockLength:
12
12
  Layout/CaseIndentation:
13
13
  EnforcedStyle: end
14
14
  Layout/EndAlignment:
15
- EnforcedStyleAlignWith: variable
15
+ EnforcedStyleAlignWith: variable
16
+ Layout/FirstArrayElementIndentation:
17
+ EnforcedStyle: consistent
data/README.md CHANGED
@@ -57,13 +57,13 @@ group :development, :test do
57
57
  gem 'nazar', require: false
58
58
  end
59
59
  ```
60
- Then, you have to call `Nazar.enable!`. If you're using Rails, you might want to add this snippet to `config/application.rb`:
60
+ Then, you have to call `Nazar.enable!`. If you're using Rails, you might want to add this snippet to `config/application.rb`:
61
61
 
62
62
  ```ruby
63
63
  console do
64
64
  require 'nazar'
65
65
 
66
- Nazar.enable! # See configuration section for more options and
66
+ Nazar.enable! # See configuration section for more options and
67
67
  # Opt-in setup section if you don't want to enable it for every item
68
68
  end
69
69
  ```
@@ -73,15 +73,15 @@ Otherwise, call it in `bin/console` or any other script that launches your REPL.
73
73
 
74
74
  This is my recommended way to install this gem - that way it works automatically in all your projects, even in standalone `irb` or `pry` sessions.
75
75
 
76
- - **Create global gems path**
77
-
76
+ - **Create global gems path**
77
+
78
78
  `mkdir -p ~/.gem/ruby/global` - You're welcome to use any other path for your global gemset
79
79
  - **Install nazar in your global gemset**
80
-
80
+
81
81
  `gem install nazar -i ~/.gem/ruby/global`
82
82
  - **Load Nazar in your REPL rc file**
83
83
  Add following snippet to your `.irbrc` or `.pryrc`:
84
-
84
+
85
85
  ```ruby
86
86
  global_gemset_path = File.expand_path('~/.gem/ruby/global/gems')
87
87
 
@@ -91,7 +91,7 @@ if Dir.exist?(global_gemset_path)
91
91
  $LOAD_PATH.unshift(*global_gems_path)
92
92
  end
93
93
 
94
- %w[nazar].each do |gem|
94
+ %w[nazar].each do |gem| # add more gems you want to load globally
95
95
  begin
96
96
  require gem
97
97
  rescue LoadError
@@ -101,16 +101,92 @@ end
101
101
  Nazar.enable! if defined?(Nazar)
102
102
  ```
103
103
 
104
+ ## Layouts
105
+ `Nazar.config.formatter.layout` can be set to either `:horizontal`, `:vertical` or `:auto`.
106
+
107
+ `:auto` uses horizontal layout if it fits in your console size, otherwise it falls back to `:vertical` layout.
108
+
109
+ Here's a preview of both layouts:
110
+
111
+ #### Horizontal
112
+ ```ruby
113
+ ┏━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━┓
114
+ ┃ id │ email │ name │ description │ created_at │ updated_at │ active │ last_login_at ┃
115
+ ┣═══════╪══════════════════════════════════════╪══════════════════════╪═══════════════════════════════════════════════════════════════════════╪═════════════════════════╪═════════════════════════╪════════╪═════════════════════════┫
116
+ ┃ 1 │ delma@goldner.co │ Mrs. Charis Brown │ Quasi atque ea. Beatae corporis quia. Eveniet velit ad. │ 2021-09-01 05:42:24 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
117
+ ┃ 2 │ markpfeffer@wunschbraun.info │ Ms. Sherice Champlin │ Omnis ut illum. Non velit mollitia. Expedita facilis dignissimos. │ 2021-09-10 07:27:00 UTC │ 2021-09-20 09:36:29 UTC │ ✓ │ 2021-09-19 16:25:32 UTC ┃
118
+ ┃ 3 │ nathansimonis@kunde.org │ Florentino Gleason │ Non recusandae eos. Et voluptates iusto. Commodi est in. │ 2021-09-07 18:24:45 UTC │ 2021-09-20 09:36:29 UTC │ ✗ │ ∅ ┃
119
+ ┃ 4 │ edwardowilkinson@walkerschiller.info │ Judie Boehm │ Ut ea eum. Tempore voluptates praesentium. Animi sapiente distinctio. │ 2021-09-05 02:11:36 UTC │ 2021-09-20 09:36:29 UTC │ ✓ │ 2021-09-19 20:17:24 UTC ┃
120
+ ┃ 5 │ isaiahebert@brauncarroll.net │ Mari Ullrich │ Aperiam quas voluptas. Autem alias quia. Aut perspiciatis eos. │ 2021-09-09 05:23:03 UTC │ 2021-09-20 09:36:29 UTC │ ∅ │ ∅ ┃
121
+ ┠───────┼──────────────────────────────────────┴──────────────────────┴───────────────────────────────────────────────────────────────────────┴─────────────────────────┴─────────────────────────┴────────┴─────────────────────────┨
122
+ ┃ Total │ 5 ┃
123
+ ┗━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
124
+ ```
125
+
126
+ ### Vertical
127
+ ```ruby
128
+ User.first(5)
129
+
130
+ ┏━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
131
+ ┃ id │ 1 ┃
132
+ ┃ email │ delma@goldner.co ┃
133
+ ┃ name │ Mrs. Charis Brown ┃
134
+ ┃ description │ Quasi atque ea. Beatae corporis quia. Eveniet velit ad. ┃
135
+ ┃ created_at │ 2021-09-01 05:42:24 UTC ┃
136
+ ┃ updated_at │ 2021-09-20 09:36:29 UTC ┃
137
+ ┃ active │ ✗ ┃
138
+ ┃ last_login_at │ ∅ ┃
139
+ ┠───────────────┼───────────────────────────────────────────────────────────────────────┨
140
+ ┃ id │ 2 ┃
141
+ ┃ email │ markpfeffer@wunschbraun.info ┃
142
+ ┃ name │ Ms. Sherice Champlin ┃
143
+ ┃ description │ Omnis ut illum. Non velit mollitia. Expedita facilis dignissimos. ┃
144
+ ┃ created_at │ 2021-09-10 07:27:00 UTC ┃
145
+ ┃ updated_at │ 2021-09-20 09:36:29 UTC ┃
146
+ ┃ active │ ✓ ┃
147
+ ┃ last_login_at │ 2021-09-19 16:25:32 UTC ┃
148
+ ┠───────────────┼───────────────────────────────────────────────────────────────────────┨
149
+ ┃ id │ 3 ┃
150
+ ┃ email │ nathansimonis@kunde.org ┃
151
+ ┃ name │ Florentino Gleason ┃
152
+ ┃ description │ Non recusandae eos. Et voluptates iusto. Commodi est in. ┃
153
+ ┃ created_at │ 2021-09-07 18:24:45 UTC ┃
154
+ ┃ updated_at │ 2021-09-20 09:36:29 UTC ┃
155
+ ┃ active │ ✗ ┃
156
+ ┃ last_login_at │ ∅ ┃
157
+ ┠───────────────┼───────────────────────────────────────────────────────────────────────┨
158
+ ┃ id │ 4 ┃
159
+ ┃ email │ edwardowilkinson@walkerschiller.info ┃
160
+ ┃ name │ Judie Boehm ┃
161
+ ┃ description │ Ut ea eum. Tempore voluptates praesentium. Animi sapiente distinctio. ┃
162
+ ┃ created_at │ 2021-09-05 02:11:36 UTC ┃
163
+ ┃ updated_at │ 2021-09-20 09:36:29 UTC ┃
164
+ ┃ active │ ✓ ┃
165
+ ┃ last_login_at │ 2021-09-19 20:17:24 UTC ┃
166
+ ┠───────────────┼───────────────────────────────────────────────────────────────────────┨
167
+ ┃ id │ 5 ┃
168
+ ┃ email │ isaiahebert@brauncarroll.net ┃
169
+ ┃ name │ Mari Ullrich ┃
170
+ ┃ description │ Aperiam quas voluptas. Autem alias quia. Aut perspiciatis eos. ┃
171
+ ┃ created_at │ 2021-09-09 05:23:03 UTC ┃
172
+ ┃ updated_at │ 2021-09-20 09:36:29 UTC ┃
173
+ ┃ active │ ∅ ┃
174
+ ┃ last_login_at │ ∅ ┃
175
+ ┠───────────────┼───────────────────────────────────────────────────────────────────────┨
176
+ ┃ Total │ 5 ┃
177
+ ┗━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
178
+ ```
179
+
104
180
  ## Shorthand method
105
181
 
106
- Nazar defines top level `__(data)` method, that can be useful for two things:
182
+ Nazar defines top level `__(data, **options)` method, that can be useful for two things:
107
183
 
108
184
  ### Inspecting (array of) Hashes/Structs
109
185
 
110
186
  Nazar by design won't enhance output for any collection that does not consist of supported (eg. ActiveRecord/Sequel) items. If you have a data that is structurally compatible (ie: all items consists of the same keys), and want to enahance the output, you can simply use `#__` method:
111
187
 
112
188
  ```ruby
113
- __ [{foo: :bar, test: 123},{foo: :baz, test: 456}]
189
+ __([{foo: :bar, test: 123},{foo: :baz, test: 456}])
114
190
  ┏━━━━━━━┯━━━━━━┓
115
191
  ┃ foo │ test ┃
116
192
  ┣═══════╪══════┫
@@ -123,6 +199,21 @@ __ [{foo: :bar, test: 123},{foo: :baz, test: 456}]
123
199
 
124
200
  It works with Structs and Hashes (or, more specifically - with any object that reponds to `#keys` and `#values`)
125
201
 
202
+ You can also enforce layout and/or pagination:
203
+
204
+ ```ruby
205
+ __([{foo: :bar, test: 123},{foo: :baz, test: 456}], layout: :vertical)
206
+ ┏━━━━━━━┯━━━━━━┓
207
+ ┃ foo │ :bar ┃
208
+ ┃ test │ 123 ┃
209
+ ┠───────┼──────┨
210
+ ┃ foo │ :baz ┃
211
+ ┃ test │ 456 ┃
212
+ ┠───────┼──────┨
213
+ ┃ Total │ 2 ┃
214
+ ┗━━━━━━━┷━━━━━━┛
215
+ ```
216
+
126
217
  ### Opt-in setup
127
218
 
128
219
  If you use `Nazar.load!` instead of `Nazar.enable!`, it would not enhance output for every element in the console. Instead, you have to call `#__` for each item that you want to enhance output
@@ -134,7 +225,6 @@ User.all # Returns default output
134
225
  __ User.all # Returns output enhanced by Nazar
135
226
  ```
136
227
 
137
-
138
228
  ## Configuration
139
229
 
140
230
  By default, Nazar improves output for ActiveRecord (both collections and a single item) and for CSV Tables. You can configure that by calling `#enable!` with optional argument:
@@ -146,8 +236,11 @@ You can also configure behaviour of Nazar:
146
236
  | Config | Default | Description |
147
237
  | ------ | ------- | ----------- |
148
238
  | `Nazar.config.colors.enabled` | true | Determines whether the output should be colorised or not |
149
- | `Nazar.config.formatter.nil` | `∅` | Sets character printed if the value was nil |
239
+ | `Nazar.config.formatter.nil` | `∅` | Sets character printed if the value was nil |
240
+ | `Nazar.config.formatter.layout` | `:auto` | Determines which layout should be used. See <a href="#layouts">Layouts</a> for more details |
241
+ | `Nazar.config.formatter.paginate` | `true` | Determines if results too long/wide should be printed directly, or paginated with `less`. Horizontal layout is always paginated.
150
242
  | `Nazar.config.formatter.boolean` | ['✓', '✗'] | First item in array is a character for `true`, second for `false` |
243
+ | `Nazar.config.formatter.resize` | `true` | Determines if the table should always be resized to take full width |
151
244
  | `Nazar.config.enable_shorthand_method` | true | Determines if shorthand method should be defined. See <a href="#opt-in-setup">Opt-in setup</a> for more details |
152
245
 
153
246
  ## Contributing
data/certs/krzyzak.pem CHANGED
@@ -1,27 +1,27 @@
1
1
  -----BEGIN CERTIFICATE-----
2
2
  MIIEgjCCAuqgAwIBAgIBATANBgkqhkiG9w0BAQsFADA1MTMwMQYDVQQDDCptaWNo
3
- YWwua3J6eXphbm93c2tpX2dpdGh1Yi9EQz1nbWFpbC9EQz1jb20wHhcNMjEwOTE3
4
- MTYyMDMyWhcNMjIwOTE3MTYyMDMyWjA1MTMwMQYDVQQDDCptaWNoYWwua3J6eXph
3
+ YWwua3J6eXphbm93c2tpX2dpdGh1Yi9EQz1nbWFpbC9EQz1jb20wHhcNMjIwOTE5
4
+ MTQwNDQyWhcNMjMwOTE5MTQwNDQyWjA1MTMwMQYDVQQDDCptaWNoYWwua3J6eXph
5
5
  bm93c2tpX2dpdGh1Yi9EQz1nbWFpbC9EQz1jb20wggGiMA0GCSqGSIb3DQEBAQUA
6
- A4IBjwAwggGKAoIBgQDEWgTDpbt17SytlzKcnFAeiNeL95n0/kwX6ubyXItTDKlG
7
- Tw4KuDrqp2rcEZZhbqVLfJX3NgAFpme6fC1sg6KfG6b5w6g+b9NBw4q1+gw1LS0J
8
- SITmgZJrMOqsX6YLeMWK5+guFuj8dhHb18IbLgS6q/+cLHQvfWMz9m0Nz6bh7p1D
9
- ydK1TaRk9QcSd9c0RoSQ8psqPoHMzqZ28+UceDMrpPPqzJXNfusibUvF/lETribm
10
- pKEiwybVh5IouMcWs5gplukkreJyjnCX19aFk3yPlQZaNp1Uly9qgO5Q5Z4gmD4M
11
- 24sZ5KxNrc7LCMC6D5KnkSPRo+nj7biIfAii6wrN7cdSValR3yJd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QqbqeW82BxjXMPu4HVBy
25
+ WXVb638bzH4OYBIYuGcw50jqBK1SyDx4exzThr6KxSiGwhAKj2xFqy51MIQjfUN0
26
+ TL5B7Buq
27
27
  -----END CERTIFICATE-----
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nazar
4
+ class BaseTable
5
+ def initialize(headers, cells)
6
+ @headers = headers
7
+ @cells = cells
8
+ end
9
+
10
+ def render
11
+ table.render
12
+ end
13
+
14
+ def add_summary(value)
15
+ table << :separator
16
+ table << summary_row(value)
17
+ end
18
+
19
+ private
20
+
21
+ attr_reader :headers, :cells
22
+
23
+ def table
24
+ @table ||= TTY::Table.new(header: headers, rows: cells)
25
+ end
26
+
27
+ def resize
28
+ Nazar.config.formatter.resize
29
+ end
30
+
31
+ def summary_row(value)
32
+ [pastel.bold('Total'), value]
33
+ end
34
+
35
+ def pastel
36
+ @pastel ||= Pastel.new(enabled: Nazar.config.colors.enabled)
37
+ end
38
+ end
39
+ end
@@ -27,7 +27,7 @@ module Nazar
27
27
  end
28
28
 
29
29
  def self.valid?(data)
30
- item = data.first
30
+ item = data&.first
31
31
  compatible = item.respond_to?(:keys) && item.respond_to?(:values)
32
32
 
33
33
  data.is_a?(Enumerable) && (item.is_a?(Struct) || compatible)
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nazar
4
+ class HorizontalTable < BaseTable
5
+ def render
6
+ table.render(:unicode, border: { separator: :each_row }, multiline: true, resize: resize)
7
+ end
8
+
9
+ private
10
+
11
+ def summary_row(value)
12
+ super.fill(nil, 3, table.columns_size - 3)
13
+ end
14
+ end
15
+ end
@@ -2,13 +2,19 @@
2
2
 
3
3
  module Nazar
4
4
  class Renderer
5
- def initialize(data, use_generic_formatter: false)
5
+ def initialize(data, layout:, paginate:, use_generic_formatter: false)
6
6
  @data = data
7
7
  @use_generic_formatter = use_generic_formatter
8
+ @layout = layout
9
+ @paginate = paginate
8
10
  end
9
11
 
10
12
  def render
11
- pager.page(view.render)
13
+ if paginate || view.horizontal?
14
+ pager.page(view.render)
15
+ else
16
+ puts view.render
17
+ end
12
18
  nil
13
19
  end
14
20
 
@@ -22,10 +28,10 @@ module Nazar
22
28
 
23
29
  private
24
30
 
25
- attr_reader :data
31
+ attr_reader :data, :use_generic_formatter, :layout, :paginate
26
32
 
27
33
  def view
28
- @view ||= View.new(data, use_generic_formatter: @use_generic_formatter)
34
+ @view ||= View.new(data, use_generic_formatter: use_generic_formatter, layout: layout)
29
35
  end
30
36
  end
31
37
  end
data/lib/nazar/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nazar
4
- VERSION = '0.1.3'
4
+ VERSION = '1.1.0'
5
5
  end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Nazar
4
+ class VerticalTable < BaseTable
5
+ class VerticalBorder < TTY::Table::Border
6
+ def_border do
7
+ top '─'
8
+ top_mid '─'
9
+ top_left '─'
10
+ top_right '─'
11
+ bottom '─'
12
+ bottom_mid '─'
13
+ bottom_left '─'
14
+ bottom_right '─'
15
+ mid '─'
16
+ mid_mid '─'
17
+ mid_left '─'
18
+ mid_right '─'
19
+ left ''
20
+ center ''
21
+ right ''
22
+ end
23
+ end
24
+
25
+ def render
26
+ table.render_with(VerticalBorder, multiline: true, resize: resize) do |renderer|
27
+ renderer.border.style = :green if Nazar.config.colors.enabled
28
+ renderer.border.separator = separators_around_each_item
29
+ end
30
+ end
31
+
32
+ private_constant :VerticalBorder
33
+
34
+ private
35
+
36
+ def table
37
+ @table ||= TTY::Table.new(rows: cells)
38
+ end
39
+
40
+ def cells
41
+ @cells.flat_map do |cell|
42
+ cell.map.with_index do |item, index|
43
+ ["#{headers[index]} ", item]
44
+ end
45
+ end
46
+ end
47
+
48
+ def separators_around_each_item
49
+ (0...cells.size).select do |i|
50
+ (i % headers.size).zero? || ((i + 1) % headers.size).zero?
51
+ end
52
+ end
53
+ end
54
+ end
data/lib/nazar/view.rb CHANGED
@@ -6,27 +6,56 @@ module Nazar
6
6
 
7
7
  def_delegators :formatter, :headers, :cells, :summary
8
8
 
9
- def initialize(data, use_generic_formatter: false)
9
+ def initialize(data, layout:, use_generic_formatter: false)
10
10
  @data = data
11
11
  @use_generic_formatter = use_generic_formatter
12
+ @layout = layout
12
13
  end
13
14
 
14
15
  def render
15
16
  return unless supported_data?
16
17
 
17
- table.tap do
18
- add_summary if summary
19
- end
18
+ table.add_summary(summary) if summary
19
+
20
+ table.render
20
21
  end
21
22
 
22
23
  def supported_data?
23
24
  !!formatter_klass && formatter.valid?
24
25
  end
25
26
 
27
+ def layout
28
+ case @layout
29
+ when :auto
30
+ determine_layout
31
+ when :vertical
32
+ :vertical
33
+ when :horizontal
34
+ :horizontal
35
+ else
36
+ raise ArgumentError, "Unknown layout: #{Nazar.config.formatter.layout}"
37
+ end
38
+ end
39
+
40
+ def horizontal?
41
+ layout == :horizontal
42
+ end
43
+
26
44
  private
27
45
 
28
46
  attr_reader :data, :use_generic_formatter
29
47
 
48
+ def determine_layout
49
+ table_width > TTY::Screen.width ? :vertical : :horizontal
50
+ end
51
+
52
+ def table_width
53
+ [
54
+ headers.map(&:size).sum,
55
+ *cells.map { |row| row.map(&:size).sum }
56
+ ].max
57
+ end
58
+
30
59
  def formatters
31
60
  @formatters ||= Nazar.formatters.tap do |formatters|
32
61
  formatters << Nazar::Formatter::Generic if use_generic_formatter
@@ -41,21 +70,8 @@ module Nazar
41
70
  @formatter ||= formatter_klass.new(data)
42
71
  end
43
72
 
44
- def add_summary
45
- table.add_separator
46
- table.add_row [pastel.bold('Total'), { value: summary, colspan: headers.size - 1 }]
47
- end
48
-
49
73
  def table
50
- @table ||= Terminal::Table.new(
51
- headings: headers,
52
- rows: cells,
53
- style: { border: :unicode_thick_edge }
54
- )
55
- end
56
-
57
- def pastel
58
- @pastel ||= Pastel.new(enabled: Nazar.config.colors.enabled)
74
+ @table ||= horizontal? ? HorizontalTable.new(headers, cells) : VerticalTable.new(headers, cells)
59
75
  end
60
76
  end
61
77
  end
data/lib/nazar.rb CHANGED
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'dry-configurable'
4
- require 'terminal-table'
4
+ require 'tty-table'
5
5
  require 'pastel'
6
6
  require 'tty-pager'
7
7
 
8
8
  require 'nazar/version'
9
+ require 'nazar/base_table'
10
+ require 'nazar/horizontal_table'
11
+ require 'nazar/vertical_table'
9
12
  require 'nazar/cell_formatter'
10
13
  require 'nazar/headers_formatter'
11
14
  require 'nazar/renderer'
@@ -19,6 +22,9 @@ module Nazar # rubocop:disable Metrics/ModuleLength
19
22
  setting :formatter do
20
23
  setting :nil, default: '∅'
21
24
  setting :boolean, default: ['✓', '✗']
25
+ setting :layout, default: :auto
26
+ setting :paginate, default: true
27
+ setting :resize, default: true
22
28
  end
23
29
 
24
30
  setting :colors do
@@ -80,7 +86,8 @@ module Nazar # rubocop:disable Metrics/ModuleLength
80
86
  return unless defined?(Pry)
81
87
 
82
88
  proc do |output, value, instance|
83
- renderer = Nazar::Renderer.new(value)
89
+ renderer = Nazar::Renderer.new(value, layout: Nazar.config.formatter.layout,
90
+ paginate: Nazar.config.formatter.paginate)
84
91
  renderer.valid? ? renderer.render : @__original_pry_print.call(output, value, instance)
85
92
  end
86
93
  end
@@ -113,7 +120,8 @@ module Nazar # rubocop:disable Metrics/ModuleLength
113
120
  ::IRB::Irb.class_eval do
114
121
  alias_method :__original_output_value__, :output_value
115
122
  def output_value(omit = false) # rubocop:disable Style/OptionalBooleanParameter
116
- renderer = Nazar::Renderer.new(@context.last_value)
123
+ renderer = Nazar::Renderer.new(@context.last_value, layout: Nazar.config.formatter.layout,
124
+ paginate: Nazar.config.formatter.paginate)
117
125
  renderer.valid? ? renderer.render : __original_output_value__(omit)
118
126
  end
119
127
  end
@@ -137,8 +145,8 @@ module Nazar # rubocop:disable Metrics/ModuleLength
137
145
  @defined_shorthand_method = true
138
146
 
139
147
  Object.class_eval do
140
- def __(item)
141
- Nazar::Renderer.new(item, use_generic_formatter: true).render
148
+ def __(item, layout: Nazar.config.formatter.layout, paginate: Nazar.config.formatter.paginate)
149
+ Nazar::Renderer.new(item, use_generic_formatter: true, layout: layout, paginate: paginate).render
142
150
  end
143
151
  end
144
152
  end
data/nazar.gemspec CHANGED
@@ -14,6 +14,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
14
14
  spec.required_ruby_version = Gem::Requirement.new('>= 2.7.0')
15
15
 
16
16
  spec.metadata['homepage_uri'] = spec.homepage
17
+ spec.metadata['rubygems_mfa_required'] = 'true'
17
18
 
18
19
  # Specify which files should be added to the gem when it is released.
19
20
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
@@ -35,6 +36,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
35
36
 
36
37
  spec.add_runtime_dependency 'dry-configurable', '~> 0.12'
37
38
  spec.add_runtime_dependency 'pastel', '~> 0.8'
38
- spec.add_runtime_dependency 'terminal-table', '~> 3.0'
39
39
  spec.add_runtime_dependency 'tty-pager', '~> 0.14'
40
+ spec.add_runtime_dependency 'tty-screen', '~> 0.8'
41
+ spec.add_runtime_dependency 'tty-table', '~> 0.12'
40
42
  end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nazar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michał Krzyżanowski
@@ -11,32 +11,32 @@ cert_chain:
11
11
  - |
12
12
  -----BEGIN CERTIFICATE-----
13
13
  MIIEgjCCAuqgAwIBAgIBATANBgkqhkiG9w0BAQsFADA1MTMwMQYDVQQDDCptaWNo
14
- YWwua3J6eXphbm93c2tpX2dpdGh1Yi9EQz1nbWFpbC9EQz1jb20wHhcNMjEwOTE3
15
- MTYyMDMyWhcNMjIwOTE3MTYyMDMyW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yygZGPksZrgcbdho1lBaZTt0+eqTVq
35
- JWaxXNTngvP87JEn1IlU2S+n2huSBtSd+h3UaubUMDUanTewtjfp1HOpGdN5qTsS
36
- FlwFUDGjWAe8lcsjyGp1dox0FK91TAHdZW1op8LYnOcO2DM8Mgzu4Gp7mibATEnx
37
- ooN2pwmH
29
+ AAOCAYEAfmM8QzJAfeZkmHonfleoiIIFNSeG84wK/MWBGKr0VMIalhNHpR6DQbao
30
+ 5kAQh7PMWjPr0n3ZHUT4QU7lqp1/eiErEXtKAExnxRefzWSHNm0WjXT/dIyFUbYQ
31
+ rRnKtabxRNWtRIrK3GecPBDtAVTRtYQQkCaSwbwE37sHJhntS/Dvnl8pw2THq7ef
32
+ XbhPDcqmP+yWRCszn9Hzda19+8VA6Sn+fIwevXMUgvb3VHqLYcIO2amHtnCeM8Q5
33
+ caYBi+Tx9EDFh04EE4rbUGX25RcLO43j3ShLLqfW6QHFUtJIKS7WhgBVoEGvNA35
34
+ 2KAZEiV7+ByRFKndeJmFtom0GMNBYPQW4l96aCrQJZyVhWSWGQZornxbH+hylJEp
35
+ on5yD3vJbVQXzwm0Z4I1IH5xc4YbEepCQRK6u/cT6NuiQqbqeW82BxjXMPu4HVBy
36
+ WXVb638bzH4OYBIYuGcw50jqBK1SyDx4exzThr6KxSiGwhAKj2xFqy51MIQjfUN0
37
+ TL5B7Buq
38
38
  -----END CERTIFICATE-----
39
- date: 2021-10-06 00:00:00.000000000 Z
39
+ date: 2022-10-12 00:00:00.000000000 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: activerecord
@@ -143,33 +143,47 @@ dependencies:
143
143
  - !ruby/object:Gem::Version
144
144
  version: '0.8'
145
145
  - !ruby/object:Gem::Dependency
146
- name: terminal-table
146
+ name: tty-pager
147
147
  requirement: !ruby/object:Gem::Requirement
148
148
  requirements:
149
149
  - - "~>"
150
150
  - !ruby/object:Gem::Version
151
- version: '3.0'
151
+ version: '0.14'
152
152
  type: :runtime
153
153
  prerelease: false
154
154
  version_requirements: !ruby/object:Gem::Requirement
155
155
  requirements:
156
156
  - - "~>"
157
157
  - !ruby/object:Gem::Version
158
- version: '3.0'
158
+ version: '0.14'
159
159
  - !ruby/object:Gem::Dependency
160
- name: tty-pager
160
+ name: tty-screen
161
161
  requirement: !ruby/object:Gem::Requirement
162
162
  requirements:
163
163
  - - "~>"
164
164
  - !ruby/object:Gem::Version
165
- version: '0.14'
165
+ version: '0.8'
166
166
  type: :runtime
167
167
  prerelease: false
168
168
  version_requirements: !ruby/object:Gem::Requirement
169
169
  requirements:
170
170
  - - "~>"
171
171
  - !ruby/object:Gem::Version
172
- version: '0.14'
172
+ version: '0.8'
173
+ - !ruby/object:Gem::Dependency
174
+ name: tty-table
175
+ requirement: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - "~>"
178
+ - !ruby/object:Gem::Version
179
+ version: '0.12'
180
+ type: :runtime
181
+ prerelease: false
182
+ version_requirements: !ruby/object:Gem::Requirement
183
+ requirements:
184
+ - - "~>"
185
+ - !ruby/object:Gem::Version
186
+ version: '0.12'
173
187
  description:
174
188
  email:
175
189
  - michal.krzyzanowski+github@gmail.com
@@ -189,6 +203,7 @@ files:
189
203
  - bin/setup
190
204
  - certs/krzyzak.pem
191
205
  - lib/nazar.rb
206
+ - lib/nazar/base_table.rb
192
207
  - lib/nazar/cell_formatter.rb
193
208
  - lib/nazar/formatter.rb
194
209
  - lib/nazar/formatter/active_record_collection.rb
@@ -200,8 +215,10 @@ files:
200
215
  - lib/nazar/formatter/sequel_interface.rb
201
216
  - lib/nazar/formatter/sequel_item.rb
202
217
  - lib/nazar/headers_formatter.rb
218
+ - lib/nazar/horizontal_table.rb
203
219
  - lib/nazar/renderer.rb
204
220
  - lib/nazar/version.rb
221
+ - lib/nazar/vertical_table.rb
205
222
  - lib/nazar/view.rb
206
223
  - nazar.gemspec
207
224
  homepage: https://github.com/krzyzak/nazar
@@ -209,6 +226,7 @@ licenses:
209
226
  - MIT
210
227
  metadata:
211
228
  homepage_uri: https://github.com/krzyzak/nazar
229
+ rubygems_mfa_required: 'true'
212
230
  post_install_message:
213
231
  rdoc_options: []
214
232
  require_paths:
metadata.gz.sig CHANGED
Binary file