nazar 1.0.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data/.rubocop.yml +3 -1
- data/README.md +3 -1
- data/lib/nazar/base_table.rb +39 -0
- data/lib/nazar/horizontal_table.rb +21 -0
- data/lib/nazar/version.rb +1 -1
- data/lib/nazar/vertical_table.rb +101 -0
- data/lib/nazar/view.rb +6 -46
- data/lib/nazar.rb +10 -1
- data/nazar.gemspec +1 -1
- data.tar.gz.sig +0 -0
- metadata +14 -11
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 29342461f59fd102beeb9253deb00b6f54086d855a2a62a11c1e108933f72600
|
4
|
+
data.tar.gz: 758f662b5a103a244beea03963ebb79d658ae6e076874cba2cadea66c14869df
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0fcbdca62c72558c2ae5278efc603dbe30ef4a08ea7966a198a4f53ce58227cec71b36bdf5acc37954b47f7c379e460947c6ab8e12c804b38e74b08610aa1d9
|
7
|
+
data.tar.gz: 07e02b611b902f7736ff1936b80b285da36e3fcacfc1492832eaf1b9ae307eaaa274d12f407d7cbbac4abc22a250a723943b449955dcb764c393b0a8d3650803
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -241,7 +241,9 @@ You can also configure behaviour of Nazar:
|
|
241
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.
|
242
242
|
| `Nazar.config.formatter.boolean` | ['✓', '✗'] | First item in array is a character for `true`, second for `false` |
|
243
243
|
| `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 |
|
244
|
-
|
244
|
+
| `Nazar.config.table.resize` | `true` | Determines if the table should always be resized to take full width |
|
245
|
+
| `Nazar.config.table.min_width` | `nil` | Sets minimum width. By default uses the longest header length |
|
246
|
+
| `Nazar.config.table.max_width` | `nil` | Determines maximum table width. If `resize` is set to true, uses terminal width by default, otherwise uses content length |
|
245
247
|
## Contributing
|
246
248
|
|
247
249
|
Bug reports and pull requests are welcome on GitHub at https://github.com/krzyzak/nazar.
|
@@ -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.table.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
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Nazar
|
4
|
+
class HorizontalTable < BaseTable
|
5
|
+
def render
|
6
|
+
table.render(:unicode, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
private
|
10
|
+
|
11
|
+
def summary_row(value)
|
12
|
+
super.fill(nil, 3, table.columns_size - 3)
|
13
|
+
end
|
14
|
+
|
15
|
+
def options
|
16
|
+
{ border: { separator: :each_row }, multiline: true, resize: resize }.tap do |hash|
|
17
|
+
hash[:width] = Nazar.config.table.max_width if Nazar.config.table.max_width
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/nazar/version.rb
CHANGED
@@ -0,0 +1,101 @@
|
|
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, options) 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 options
|
41
|
+
{
|
42
|
+
multiline: true,
|
43
|
+
resize: resize,
|
44
|
+
column_widths: column_widths,
|
45
|
+
width: max_width
|
46
|
+
}
|
47
|
+
end
|
48
|
+
|
49
|
+
def cells
|
50
|
+
@cells.flat_map do |cell|
|
51
|
+
cell.map.with_index do |item, index|
|
52
|
+
["#{headers[index]} ", item]
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def separators_around_each_item
|
58
|
+
(0...cells.size).select do |i|
|
59
|
+
(i % headers.size).zero? || ((i + 1) % headers.size).zero?
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def column_widths
|
64
|
+
[headers_width, values_width]
|
65
|
+
end
|
66
|
+
|
67
|
+
def max_width
|
68
|
+
Nazar.config.table.max_width || default_width
|
69
|
+
end
|
70
|
+
|
71
|
+
def default_width
|
72
|
+
Nazar.config.table.resize ? TTY::Screen.width : natural_width + padding
|
73
|
+
end
|
74
|
+
|
75
|
+
def natural_width
|
76
|
+
content_size = headers_width + content_width
|
77
|
+
|
78
|
+
[content_size, TTY::Screen.width].min
|
79
|
+
end
|
80
|
+
|
81
|
+
def content_width
|
82
|
+
cells.flatten.map(&:size).max
|
83
|
+
end
|
84
|
+
|
85
|
+
def headers_width
|
86
|
+
[headers.map(&:size).max + 1, Nazar.config.table.min_width].compact.min
|
87
|
+
end
|
88
|
+
|
89
|
+
def values_width
|
90
|
+
if Nazar.config.table.resize
|
91
|
+
TTY::Screen.width - headers_width
|
92
|
+
else
|
93
|
+
[content_width, TTY::Screen.width - headers_width].min
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def padding
|
98
|
+
3
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
data/lib/nazar/view.rb
CHANGED
@@ -4,7 +4,7 @@ module Nazar
|
|
4
4
|
class View
|
5
5
|
extend Forwardable
|
6
6
|
|
7
|
-
def_delegators :formatter, :headers, :summary
|
7
|
+
def_delegators :formatter, :headers, :cells, :summary
|
8
8
|
|
9
9
|
def initialize(data, layout:, use_generic_formatter: false)
|
10
10
|
@data = data
|
@@ -15,9 +15,9 @@ module Nazar
|
|
15
15
|
def render
|
16
16
|
return unless supported_data?
|
17
17
|
|
18
|
-
table.
|
19
|
-
|
20
|
-
|
18
|
+
table.add_summary(summary) if summary
|
19
|
+
|
20
|
+
table.render
|
21
21
|
end
|
22
22
|
|
23
23
|
def supported_data?
|
@@ -52,7 +52,7 @@ module Nazar
|
|
52
52
|
def table_width
|
53
53
|
[
|
54
54
|
headers.map(&:size).sum,
|
55
|
-
|
55
|
+
*cells.map { |row| row.map(&:size).sum }
|
56
56
|
].max
|
57
57
|
end
|
58
58
|
|
@@ -70,48 +70,8 @@ module Nazar
|
|
70
70
|
@formatter ||= formatter_klass.new(data)
|
71
71
|
end
|
72
72
|
|
73
|
-
def add_summary
|
74
|
-
colspan = horizontal? ? headers.size - 1 : 1
|
75
|
-
|
76
|
-
table.add_separator
|
77
|
-
table.add_row [pastel.bold('Total'), { value: summary, colspan: colspan }]
|
78
|
-
end
|
79
|
-
|
80
73
|
def table
|
81
|
-
@table ||=
|
82
|
-
headings: display_headers,
|
83
|
-
rows: display_cells,
|
84
|
-
style: { border: :unicode_thick_edge }
|
85
|
-
)
|
86
|
-
end
|
87
|
-
|
88
|
-
def display_headers
|
89
|
-
horizontal? ? formatter.headers : []
|
90
|
-
end
|
91
|
-
|
92
|
-
def display_cells
|
93
|
-
horizontal? ? horizontal_cells : vertical_cells
|
94
|
-
end
|
95
|
-
|
96
|
-
def horizontal_cells
|
97
|
-
formatter.cells
|
98
|
-
end
|
99
|
-
|
100
|
-
def vertical_cells
|
101
|
-
data = horizontal_cells.inject([]) do |output, items|
|
102
|
-
items.each.with_index do |item, index|
|
103
|
-
output << [headers[index], item]
|
104
|
-
end
|
105
|
-
|
106
|
-
output << :separator
|
107
|
-
end
|
108
|
-
|
109
|
-
data.pop # remove last separator
|
110
|
-
data
|
111
|
-
end
|
112
|
-
|
113
|
-
def pastel
|
114
|
-
@pastel ||= Pastel.new(enabled: Nazar.config.colors.enabled)
|
74
|
+
@table ||= horizontal? ? HorizontalTable.new(headers, cells) : VerticalTable.new(headers, cells)
|
115
75
|
end
|
116
76
|
end
|
117
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 '
|
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'
|
@@ -23,6 +26,12 @@ module Nazar # rubocop:disable Metrics/ModuleLength
|
|
23
26
|
setting :paginate, default: true
|
24
27
|
end
|
25
28
|
|
29
|
+
setting :table do
|
30
|
+
setting :resize, default: true
|
31
|
+
setting :min_width, default: nil
|
32
|
+
setting :max_width, default: nil
|
33
|
+
end
|
34
|
+
|
26
35
|
setting :colors do
|
27
36
|
setting :enabled, default: ENV.fetch('ENABLE_TTY_COLORS') { TTY::Color.color? ? 'true' : 'false' } == 'true'
|
28
37
|
end
|
data/nazar.gemspec
CHANGED
@@ -36,7 +36,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
|
|
36
36
|
|
37
37
|
spec.add_runtime_dependency 'dry-configurable', '~> 0.12'
|
38
38
|
spec.add_runtime_dependency 'pastel', '~> 0.8'
|
39
|
-
spec.add_runtime_dependency 'terminal-table', '~> 3.0'
|
40
39
|
spec.add_runtime_dependency 'tty-pager', '~> 0.14'
|
41
40
|
spec.add_runtime_dependency 'tty-screen', '~> 0.8'
|
41
|
+
spec.add_runtime_dependency 'tty-table', '~> 0.12'
|
42
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: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michał Krzyżanowski
|
@@ -36,7 +36,7 @@ cert_chain:
|
|
36
36
|
WXVb638bzH4OYBIYuGcw50jqBK1SyDx4exzThr6KxSiGwhAKj2xFqy51MIQjfUN0
|
37
37
|
TL5B7Buq
|
38
38
|
-----END CERTIFICATE-----
|
39
|
-
date: 2022-
|
39
|
+
date: 2022-10-12 00:00:00.000000000 Z
|
40
40
|
dependencies:
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: activerecord
|
@@ -143,47 +143,47 @@ dependencies:
|
|
143
143
|
- !ruby/object:Gem::Version
|
144
144
|
version: '0.8'
|
145
145
|
- !ruby/object:Gem::Dependency
|
146
|
-
name:
|
146
|
+
name: tty-pager
|
147
147
|
requirement: !ruby/object:Gem::Requirement
|
148
148
|
requirements:
|
149
149
|
- - "~>"
|
150
150
|
- !ruby/object:Gem::Version
|
151
|
-
version: '
|
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: '
|
158
|
+
version: '0.14'
|
159
159
|
- !ruby/object:Gem::Dependency
|
160
|
-
name: tty-
|
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.
|
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.
|
172
|
+
version: '0.8'
|
173
173
|
- !ruby/object:Gem::Dependency
|
174
|
-
name: tty-
|
174
|
+
name: tty-table
|
175
175
|
requirement: !ruby/object:Gem::Requirement
|
176
176
|
requirements:
|
177
177
|
- - "~>"
|
178
178
|
- !ruby/object:Gem::Version
|
179
|
-
version: '0.
|
179
|
+
version: '0.12'
|
180
180
|
type: :runtime
|
181
181
|
prerelease: false
|
182
182
|
version_requirements: !ruby/object:Gem::Requirement
|
183
183
|
requirements:
|
184
184
|
- - "~>"
|
185
185
|
- !ruby/object:Gem::Version
|
186
|
-
version: '0.
|
186
|
+
version: '0.12'
|
187
187
|
description:
|
188
188
|
email:
|
189
189
|
- michal.krzyzanowski+github@gmail.com
|
@@ -203,6 +203,7 @@ files:
|
|
203
203
|
- bin/setup
|
204
204
|
- certs/krzyzak.pem
|
205
205
|
- lib/nazar.rb
|
206
|
+
- lib/nazar/base_table.rb
|
206
207
|
- lib/nazar/cell_formatter.rb
|
207
208
|
- lib/nazar/formatter.rb
|
208
209
|
- lib/nazar/formatter/active_record_collection.rb
|
@@ -214,8 +215,10 @@ files:
|
|
214
215
|
- lib/nazar/formatter/sequel_interface.rb
|
215
216
|
- lib/nazar/formatter/sequel_item.rb
|
216
217
|
- lib/nazar/headers_formatter.rb
|
218
|
+
- lib/nazar/horizontal_table.rb
|
217
219
|
- lib/nazar/renderer.rb
|
218
220
|
- lib/nazar/version.rb
|
221
|
+
- lib/nazar/vertical_table.rb
|
219
222
|
- lib/nazar/view.rb
|
220
223
|
- nazar.gemspec
|
221
224
|
homepage: https://github.com/krzyzak/nazar
|
metadata.gz.sig
CHANGED
Binary file
|