typed_print 0.2.0 → 0.4.0
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 +4 -4
- data/CHANGELOG.md +30 -4
- data/README.md +94 -26
- data/assets/demo.gif +0 -0
- data/assets/demo.rb +38 -0
- data/assets/demo.tape +14 -0
- data/lib/typed_print/table.rb +114 -42
- data/lib/typed_print/version.rb +1 -1
- data/lib/typed_print.rb +15 -4
- metadata +19 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d9efbcc2dccb48448275cea750dea3f3e8682d669916f9968fa27ce9ea84860a
|
|
4
|
+
data.tar.gz: 5b00aac3cd4941932a298285ed7931ea7a62cef5eb2667d045c7201b3860bf27
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1a8b3bbc69003ba7711849e746e71eb2cfce4c017842e8e6104c500b71830a415a03234716d6837ef9d77e4ae336231a41f78399f95f15e26a961847e48cd46a
|
|
7
|
+
data.tar.gz: c3f40b2ef369fcf2106dba9cf85792981ecd901a503b6904ef584f5f04f0783b910de90836506df91a97616618884f2134d648a102cd8fe817534c28953cf9cf
|
data/CHANGELOG.md
CHANGED
|
@@ -1,13 +1,39 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## [0.4.0] - 2026-05-02
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- `TypedPrint.to_csv(data, only:, headers:, delimiter: ",")` — returns CSV string
|
|
7
|
+
- `TypedPrint.save(data, path, only:, headers:, delimiter: ",")` — writes CSV file with UTF-8 BOM (Excel-compatible)
|
|
8
|
+
- Custom delimiter support (`,` or `;` or any character)
|
|
9
|
+
- Fixed edge case: empty data no longer raises on `determine_headers`
|
|
10
|
+
|
|
11
|
+
## [0.3.0] - 2026-04-27
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
- Optional color support via `pastel` gem (runtime optional, dev dependency)
|
|
15
|
+
- `color: true` for automatic type-based coloring (headers cyan, numbers green, booleans green/red, nil gray)
|
|
16
|
+
- `colors: { col: :color }` for manual per-column color mapping
|
|
17
|
+
- Color support for both `:plain` and `:markdown` formats
|
|
18
|
+
|
|
19
|
+
## [0.2.0] - 2026-04-22
|
|
20
|
+
|
|
21
|
+
### Added
|
|
22
|
+
- Markdown table output with `format: :markdown` option
|
|
23
|
+
- New `TypedPrint.table` method returns string without printing
|
|
24
|
+
- Support for both plain and markdown formats
|
|
25
|
+
|
|
26
|
+
### Changed
|
|
27
|
+
- Internal refactoring to support multiple output formats
|
|
28
|
+
|
|
1
29
|
## [0.1.0] - 2026-04-21
|
|
2
30
|
|
|
3
31
|
### Added
|
|
4
32
|
- Initial release
|
|
5
33
|
- Basic table formatting for Ruby hashes
|
|
6
34
|
- Column alignment options (left, right, center)
|
|
7
|
-
- Column filtering with
|
|
8
|
-
- Custom headers with
|
|
35
|
+
- Column filtering with `only:` option
|
|
36
|
+
- Custom headers with `headers:` option
|
|
9
37
|
- Smart type formatting (booleans, nil, strings)
|
|
10
38
|
- Preserves original column order
|
|
11
|
-
- `TypedPrint.print` method for printing to stdout
|
|
12
|
-
- `TypedPrint.table` method for returning formatted string
|
|
13
39
|
- Zero dependencies
|
data/README.md
CHANGED
|
@@ -8,29 +8,30 @@ Beautiful, aligned table output for Ruby hashes and objects with zero dependenci
|
|
|
8
8
|
[](https://opensource.org/licenses/MIT)
|
|
9
9
|
[](https://rubygems.org/gems/typed_print)
|
|
10
10
|
|
|
11
|
+

|
|
12
|
+
|
|
11
13
|
## Features
|
|
12
14
|
|
|
13
|
-
- 🚀 Zero dependencies
|
|
15
|
+
- 🚀 Zero runtime dependencies
|
|
14
16
|
- 📊 Automatic column width calculation
|
|
15
17
|
- 🎯 Smart type formatting (booleans, nil, strings)
|
|
16
18
|
- 📐 Column alignment (left, right, center)
|
|
17
19
|
- 🎨 Custom column headers
|
|
18
20
|
- 🔍 Column filtering
|
|
19
21
|
- 📝 Preserves original column order
|
|
22
|
+
- 📄 Markdown table output (v0.2.0+)
|
|
23
|
+
- 🌈 Optional color output via `pastel` gem (v0.3.0+)
|
|
24
|
+
- 📤 CSV export with Excel-compatible encoding (v0.4.0+)
|
|
20
25
|
|
|
21
26
|
## Installation
|
|
22
27
|
|
|
23
28
|
Add this line to your application's Gemfile:
|
|
24
29
|
|
|
25
|
-
|
|
26
|
-
gem 'typed_print'
|
|
27
|
-
```
|
|
30
|
+
`gem 'typed_print'`
|
|
28
31
|
|
|
29
32
|
Or install it yourself:
|
|
30
33
|
|
|
31
|
-
|
|
32
|
-
gem install typed_print
|
|
33
|
-
```
|
|
34
|
+
`gem install typed_print`
|
|
34
35
|
|
|
35
36
|
## Usage
|
|
36
37
|
|
|
@@ -48,72 +49,78 @@ TypedPrint.print(data)
|
|
|
48
49
|
```
|
|
49
50
|
|
|
50
51
|
Output:
|
|
52
|
+
|
|
51
53
|
```
|
|
52
|
-
|
|
54
|
+
Name Score Active
|
|
53
55
|
------+------+-------
|
|
54
56
|
Alice 100 true
|
|
55
57
|
Bob 42 false
|
|
56
58
|
```
|
|
57
59
|
|
|
58
|
-
###
|
|
60
|
+
### Markdown Format (NEW in v0.2.0)
|
|
61
|
+
```ruby
|
|
62
|
+
TypedPrint.print(data, format: :markdown)
|
|
63
|
+
```
|
|
59
64
|
|
|
60
|
-
|
|
65
|
+
Output:
|
|
66
|
+
```
|
|
67
|
+
| Name | Score | Active |
|
|
68
|
+
|-------|-------|--------|
|
|
69
|
+
| Alice | 100 | true |
|
|
70
|
+
| Bob | 42 | false |
|
|
71
|
+
```
|
|
61
72
|
|
|
73
|
+
### Column Alignment
|
|
62
74
|
```ruby
|
|
63
75
|
TypedPrint.print(data, align: { score: :right })
|
|
64
76
|
```
|
|
65
77
|
|
|
66
78
|
Output:
|
|
67
79
|
```
|
|
68
|
-
|
|
80
|
+
Name Score Active
|
|
69
81
|
------+------+-------
|
|
70
82
|
Alice 100 true
|
|
71
|
-
Bob 42 false
|
|
83
|
+
Bob 42 false
|
|
72
84
|
```
|
|
73
85
|
|
|
74
86
|
### Filter Columns
|
|
75
87
|
|
|
76
|
-
Show only specific columns:
|
|
77
|
-
|
|
78
88
|
```ruby
|
|
79
89
|
TypedPrint.print(data, only: [:name, :score])
|
|
80
90
|
```
|
|
81
91
|
|
|
82
92
|
Output:
|
|
83
93
|
```
|
|
84
|
-
|
|
94
|
+
Name Score
|
|
85
95
|
------+------
|
|
86
96
|
Alice 100
|
|
87
97
|
Bob 42
|
|
88
98
|
```
|
|
89
99
|
|
|
90
100
|
### Custom Headers
|
|
91
|
-
|
|
92
|
-
Rename columns for display:
|
|
93
|
-
|
|
94
101
|
```ruby
|
|
95
102
|
TypedPrint.print(data, headers: { name: "Username", score: "Points", active: "Status" })
|
|
96
103
|
```
|
|
97
104
|
|
|
98
105
|
Output:
|
|
99
106
|
```
|
|
100
|
-
|
|
107
|
+
Username Points Status
|
|
101
108
|
---------+------+-------
|
|
102
109
|
Alice 100 true
|
|
103
110
|
Bob 42 false
|
|
104
111
|
```
|
|
105
112
|
|
|
106
113
|
### Return String Instead of Printing
|
|
107
|
-
|
|
108
|
-
Use `table` method to get the formatted string:
|
|
109
|
-
|
|
110
114
|
```ruby
|
|
111
115
|
table_string = TypedPrint.table(data)
|
|
112
116
|
puts table_string.upcase
|
|
113
|
-
```
|
|
114
117
|
|
|
115
|
-
|
|
118
|
+
# Markdown format
|
|
119
|
+
markdown_string = TypedPrint.table(data, format: :markdown)
|
|
120
|
+
File.write("table.md", markdown_string)
|
|
121
|
+
````
|
|
116
122
|
|
|
123
|
+
### Working with Different Data Types
|
|
117
124
|
```ruby
|
|
118
125
|
mixed_data = [
|
|
119
126
|
{ name: "Product A", price: 29.99, in_stock: true, notes: nil },
|
|
@@ -125,25 +132,86 @@ TypedPrint.print(mixed_data)
|
|
|
125
132
|
|
|
126
133
|
Output:
|
|
127
134
|
```
|
|
128
|
-
|
|
135
|
+
Name Price In_stock Notes
|
|
129
136
|
----------+-------+---------+-------------
|
|
130
137
|
Product A 29.99 true
|
|
131
138
|
Product B 49.99 false Limited edition
|
|
132
139
|
```
|
|
133
140
|
|
|
134
141
|
## API Reference
|
|
135
|
-
|
|
136
142
|
`TypedPrint.print(data, options)` Prints the formatted table to stdout and returns `nil`.
|
|
137
143
|
|
|
144
|
+
|
|
138
145
|
**Options:**
|
|
139
146
|
- `align: Hash` - Column alignment (`:left`, `:right`, `:center`), defaults to `:left`
|
|
140
147
|
- `only: Array` - Array of column symbols to display
|
|
141
148
|
- `headers: Hash` - Custom headers for columns
|
|
149
|
+
- `format: Symbol` - Output format (`:plain` or `:markdown`), defaults to `:plain`
|
|
150
|
+
- `color: Boolean` - Auto color by type (headers cyan, numbers/true green, false red, nil gray), requires `pastel` gem
|
|
151
|
+
- `colors: Hash` - Manual per-column color map (e.g. `{ name: :cyan, score: :green }`), requires `pastel` gem
|
|
142
152
|
|
|
143
153
|
`TypedPrint.table(data, options)` returns the formatted table as a string.
|
|
144
154
|
|
|
145
155
|
Same options as `print`.
|
|
146
156
|
|
|
157
|
+
`TypedPrint.to_csv(data, options)` returns data as a CSV string.
|
|
158
|
+
|
|
159
|
+
**Options:**
|
|
160
|
+
- `only: Array` - Columns to include
|
|
161
|
+
- `headers: Hash` - Custom column headers
|
|
162
|
+
- `delimiter: String` - Column separator (default: `","`)
|
|
163
|
+
|
|
164
|
+
`TypedPrint.save(data, path, options)` writes a CSV file with UTF-8 BOM and returns `nil`.
|
|
165
|
+
|
|
166
|
+
Same options as `to_csv`.
|
|
167
|
+
|
|
168
|
+
### Color Output (v0.3.0+)
|
|
169
|
+
|
|
170
|
+
Color support is optional and requires the `pastel` gem. Add it to your Gemfile:
|
|
171
|
+
|
|
172
|
+
```ruby
|
|
173
|
+
gem 'pastel'
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
**Automatic coloring by type:**
|
|
177
|
+
```ruby
|
|
178
|
+
TypedPrint.print(data, color: true)
|
|
179
|
+
# Headers → cyan, Integer/Float/true → green, false → red, nil → gray
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
**Manual per-column colors:**
|
|
183
|
+
```ruby
|
|
184
|
+
TypedPrint.print(data, colors: { name: :cyan, score: :green, active: :yellow })
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Both `:plain` and `:markdown` formats support color. If `pastel` is not installed, color options are silently ignored and output is plain text.
|
|
188
|
+
|
|
189
|
+
### CSV Export (v0.4.0+)
|
|
190
|
+
|
|
191
|
+
No extra dependencies — uses Ruby's stdlib `csv`.
|
|
192
|
+
|
|
193
|
+
**Return CSV string:**
|
|
194
|
+
```ruby
|
|
195
|
+
csv = TypedPrint.to_csv(data)
|
|
196
|
+
# => "Name,Score,Active\nAlice,100,true\nBob,42,false\n"
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
**Save to file (Excel-compatible UTF-8 BOM):**
|
|
200
|
+
```ruby
|
|
201
|
+
TypedPrint.save(data, "output.csv")
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
**Custom delimiter:**
|
|
205
|
+
```ruby
|
|
206
|
+
TypedPrint.to_csv(data, delimiter: ";")
|
|
207
|
+
TypedPrint.save(data, "output.csv", delimiter: ";")
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
**Filter columns and custom headers:**
|
|
211
|
+
```ruby
|
|
212
|
+
TypedPrint.to_csv(data, only: [:name, :score], headers: { score: "Points" })
|
|
213
|
+
```
|
|
214
|
+
|
|
147
215
|
## Development
|
|
148
216
|
|
|
149
217
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
|
data/assets/demo.gif
ADDED
|
Binary file
|
data/assets/demo.rb
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
|
|
4
|
+
require "typed_print"
|
|
5
|
+
|
|
6
|
+
data = [
|
|
7
|
+
{ name: "Alice", score: 98, active: true },
|
|
8
|
+
{ name: "Bob", score: 74, active: false },
|
|
9
|
+
{ name: "Charlie", score: 61, active: true },
|
|
10
|
+
{ name: "Diana", score: 42, active: false }
|
|
11
|
+
]
|
|
12
|
+
|
|
13
|
+
puts "# Basic table"
|
|
14
|
+
TypedPrint.print(data)
|
|
15
|
+
sleep 2
|
|
16
|
+
|
|
17
|
+
puts "\n# Right-align score column"
|
|
18
|
+
TypedPrint.print(data, align: { score: :right })
|
|
19
|
+
sleep 2
|
|
20
|
+
|
|
21
|
+
puts "\n# Filter columns"
|
|
22
|
+
TypedPrint.print(data, only: [:name, :score])
|
|
23
|
+
sleep 2
|
|
24
|
+
|
|
25
|
+
puts "\n# Custom headers"
|
|
26
|
+
TypedPrint.print(data, headers: { name: "Player", score: "Points", active: "Status" })
|
|
27
|
+
sleep 2
|
|
28
|
+
|
|
29
|
+
puts "\n# Markdown format"
|
|
30
|
+
TypedPrint.print(data, format: :markdown)
|
|
31
|
+
sleep 2
|
|
32
|
+
|
|
33
|
+
puts "\n# Automatic colors"
|
|
34
|
+
TypedPrint.print(data, color: true)
|
|
35
|
+
sleep 2
|
|
36
|
+
|
|
37
|
+
puts "\n# Manual colors"
|
|
38
|
+
TypedPrint.print(data, colors: { name: :cyan, score: :green, active: :yellow })
|
data/assets/demo.tape
ADDED
data/lib/typed_print/table.rb
CHANGED
|
@@ -1,12 +1,20 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
begin
|
|
4
|
+
require "pastel"
|
|
5
|
+
rescue LoadError
|
|
6
|
+
# Pastel not installed; color output silently disabled
|
|
7
|
+
end
|
|
8
|
+
|
|
3
9
|
module TypedPrint
|
|
4
10
|
class Table
|
|
5
|
-
def initialize(data, alignments = {}, only_columns = nil, custom_headers = {})
|
|
11
|
+
def initialize(data, alignments = {}, only_columns = nil, custom_headers = {}, color: false, colors: nil)
|
|
6
12
|
@data = data
|
|
7
13
|
@alignments = alignments
|
|
8
14
|
@only_columns = only_columns&.map(&:to_sym)
|
|
9
15
|
@custom_headers = custom_headers.transform_keys(&:to_sym)
|
|
16
|
+
@color_auto = color
|
|
17
|
+
@colors = colors&.transform_keys(&:to_sym)
|
|
10
18
|
|
|
11
19
|
@headers = determine_headers
|
|
12
20
|
end
|
|
@@ -19,35 +27,46 @@ module TypedPrint
|
|
|
19
27
|
end
|
|
20
28
|
end
|
|
21
29
|
|
|
22
|
-
def
|
|
30
|
+
def to_csv(col_sep: ",")
|
|
31
|
+
require "csv"
|
|
23
32
|
return "" if @data.empty?
|
|
24
33
|
|
|
25
|
-
|
|
34
|
+
header_row = @headers.map { |h| header_label(h) }
|
|
26
35
|
rows = @data.map { |row| @headers.map { |h| format_value(row[h]) } }
|
|
27
36
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
@custom_headers[h]
|
|
32
|
-
else
|
|
33
|
-
h.to_s.split('_').map(&:capitalize).join(' ')
|
|
34
|
-
end
|
|
37
|
+
CSV.generate(col_sep: col_sep) do |csv|
|
|
38
|
+
csv << header_row
|
|
39
|
+
rows.each { |row| csv << row }
|
|
35
40
|
end
|
|
41
|
+
end
|
|
36
42
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
43
|
+
def render_plain
|
|
44
|
+
return "" if @data.empty?
|
|
45
|
+
|
|
46
|
+
rows_plain = @data.map { |row| @headers.map { |h| format_value(row[h]) } }
|
|
47
|
+
header_strings_plain = @headers.map { |h| header_label(h) }
|
|
48
|
+
|
|
49
|
+
column_widths = header_strings_plain.map(&:length)
|
|
50
|
+
rows_plain.each do |row|
|
|
40
51
|
row.each_with_index do |cell, i|
|
|
41
52
|
column_widths[i] = [column_widths[i], cell.length].max
|
|
42
53
|
end
|
|
43
54
|
end
|
|
44
55
|
|
|
45
|
-
|
|
56
|
+
if color_mode?
|
|
57
|
+
header_color = @color_auto ? :cyan : nil
|
|
58
|
+
header_strings_display = header_strings_plain.map { |h| colorize(h, header_color) }
|
|
59
|
+
rows_display = @data.map { |row| @headers.map { |h| colorize_cell(h, row[h]) } }
|
|
60
|
+
else
|
|
61
|
+
header_strings_display = header_strings_plain
|
|
62
|
+
rows_display = rows_plain
|
|
63
|
+
end
|
|
64
|
+
|
|
46
65
|
output = []
|
|
47
|
-
output << format_row(
|
|
66
|
+
output << format_row(header_strings_display, column_widths, :center, header_strings_plain)
|
|
48
67
|
output << separator(column_widths)
|
|
49
|
-
|
|
50
|
-
output << format_row(
|
|
68
|
+
rows_plain.each_with_index do |plain_row, ri|
|
|
69
|
+
output << format_row(rows_display[ri], column_widths, nil, plain_row)
|
|
51
70
|
end
|
|
52
71
|
|
|
53
72
|
output.join("\n")
|
|
@@ -56,35 +75,34 @@ module TypedPrint
|
|
|
56
75
|
def render_markdown
|
|
57
76
|
return "" if @data.empty?
|
|
58
77
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
# Build header strings
|
|
63
|
-
header_strings = @headers.map do |h|
|
|
64
|
-
if @custom_headers[h]
|
|
65
|
-
@custom_headers[h]
|
|
66
|
-
else
|
|
67
|
-
h.to_s.split('_').map(&:capitalize).join(' ')
|
|
68
|
-
end
|
|
69
|
-
end
|
|
78
|
+
rows_plain = @data.map { |row| @headers.map { |h| format_value(row[h]) } }
|
|
79
|
+
header_strings_plain = @headers.map { |h| header_label(h) }
|
|
70
80
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
rows.each do |row|
|
|
81
|
+
column_widths = header_strings_plain.map(&:length)
|
|
82
|
+
rows_plain.each do |row|
|
|
74
83
|
row.each_with_index do |cell, i|
|
|
75
84
|
column_widths[i] = [column_widths[i], cell.length].max
|
|
76
85
|
end
|
|
77
86
|
end
|
|
78
87
|
|
|
79
|
-
|
|
88
|
+
if color_mode?
|
|
89
|
+
header_color = @color_auto ? :cyan : nil
|
|
90
|
+
header_strings_display = header_strings_plain.map { |h| colorize(h, header_color) }
|
|
91
|
+
rows_display = @data.map { |row| @headers.map { |h| colorize_cell(h, row[h]) } }
|
|
92
|
+
else
|
|
93
|
+
header_strings_display = header_strings_plain
|
|
94
|
+
rows_display = rows_plain
|
|
95
|
+
end
|
|
96
|
+
|
|
80
97
|
output = []
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
98
|
+
output << "| " + header_strings_display.each_with_index.map { |h, i|
|
|
99
|
+
pad_right(h, header_strings_plain[i], column_widths[i])
|
|
100
|
+
}.join(" | ") + " |"
|
|
84
101
|
output << "|" + column_widths.map { |w| "-" * (w + 2) }.join("|") + "|"
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
102
|
+
rows_plain.each_with_index do |plain_row, ri|
|
|
103
|
+
output << "| " + rows_display[ri].each_with_index.map { |cell, i|
|
|
104
|
+
pad_right(cell, plain_row[i], column_widths[i])
|
|
105
|
+
}.join(" | ") + " |"
|
|
88
106
|
end
|
|
89
107
|
|
|
90
108
|
output.join("\n")
|
|
@@ -101,7 +119,52 @@ module TypedPrint
|
|
|
101
119
|
end
|
|
102
120
|
end
|
|
103
121
|
|
|
122
|
+
def header_label(h)
|
|
123
|
+
if @custom_headers[h]
|
|
124
|
+
@custom_headers[h]
|
|
125
|
+
else
|
|
126
|
+
h.to_s.split("_").map(&:capitalize).join(" ")
|
|
127
|
+
end
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def color_mode?
|
|
131
|
+
@color_auto || (@colors && !@colors.empty?)
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def colorize(text, color)
|
|
135
|
+
return text unless color && defined?(Pastel)
|
|
136
|
+
@pastel ||= Pastel.new
|
|
137
|
+
@pastel.send(color, text)
|
|
138
|
+
rescue
|
|
139
|
+
text
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def colorize_cell(header_key, original_value)
|
|
143
|
+
plain = format_value(original_value)
|
|
144
|
+
color = cell_color(header_key, original_value)
|
|
145
|
+
colorize(plain, color)
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def cell_color(header_key, value)
|
|
149
|
+
if @colors
|
|
150
|
+
@colors[header_key]
|
|
151
|
+
elsif @color_auto
|
|
152
|
+
auto_color(value)
|
|
153
|
+
end
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
def auto_color(value)
|
|
157
|
+
case value
|
|
158
|
+
when Integer, Float then :green
|
|
159
|
+
when true then :green
|
|
160
|
+
when false then :red
|
|
161
|
+
when nil then :bright_black
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
104
165
|
def determine_headers
|
|
166
|
+
return [] if @data.empty?
|
|
167
|
+
|
|
105
168
|
all_keys = @data.flat_map(&:keys).uniq
|
|
106
169
|
|
|
107
170
|
if @only_columns
|
|
@@ -112,8 +175,9 @@ module TypedPrint
|
|
|
112
175
|
end
|
|
113
176
|
end
|
|
114
177
|
|
|
115
|
-
def format_row(cells, widths, alignment_override = nil)
|
|
178
|
+
def format_row(cells, widths, alignment_override = nil, plain_cells = nil)
|
|
116
179
|
cells.each_with_index.map do |cell, i|
|
|
180
|
+
plain_cell = plain_cells ? plain_cells[i] : cell
|
|
117
181
|
width = widths[i]
|
|
118
182
|
header_key = @headers[i]
|
|
119
183
|
|
|
@@ -125,17 +189,25 @@ module TypedPrint
|
|
|
125
189
|
:left
|
|
126
190
|
end
|
|
127
191
|
|
|
192
|
+
padding = width - plain_cell.length
|
|
193
|
+
|
|
128
194
|
case align
|
|
129
195
|
when :right
|
|
130
|
-
cell
|
|
196
|
+
" " * padding + cell
|
|
131
197
|
when :center
|
|
132
|
-
|
|
198
|
+
left_pad = padding / 2
|
|
199
|
+
right_pad = padding - left_pad
|
|
200
|
+
" " * left_pad + cell + " " * right_pad
|
|
133
201
|
else
|
|
134
|
-
cell
|
|
202
|
+
cell + " " * padding
|
|
135
203
|
end
|
|
136
204
|
end.join(" ")
|
|
137
205
|
end
|
|
138
206
|
|
|
207
|
+
def pad_right(colored_text, plain_text, width)
|
|
208
|
+
colored_text + " " * (width - plain_text.length)
|
|
209
|
+
end
|
|
210
|
+
|
|
139
211
|
def separator(widths)
|
|
140
212
|
widths.map { |w| "-" * w }.join("-+-")
|
|
141
213
|
end
|
data/lib/typed_print/version.rb
CHANGED
data/lib/typed_print.rb
CHANGED
|
@@ -4,13 +4,24 @@ require "typed_print/version"
|
|
|
4
4
|
require "typed_print/table"
|
|
5
5
|
|
|
6
6
|
module TypedPrint
|
|
7
|
-
def self.print(data, align: {}, only: nil, headers: {}, format: :plain)
|
|
8
|
-
puts table(data, align: align, only: only, headers: headers, format: format)
|
|
7
|
+
def self.print(data, align: {}, only: nil, headers: {}, format: :plain, color: false, colors: nil)
|
|
8
|
+
puts table(data, align: align, only: only, headers: headers, format: format, color: color, colors: colors)
|
|
9
9
|
nil
|
|
10
10
|
end
|
|
11
11
|
|
|
12
|
-
def self.table(data, align: {}, only: nil, headers: {}, format: :plain)
|
|
13
|
-
table_obj = TypedPrint::Table.new(data, align, only, headers)
|
|
12
|
+
def self.table(data, align: {}, only: nil, headers: {}, format: :plain, color: false, colors: nil)
|
|
13
|
+
table_obj = TypedPrint::Table.new(data, align, only, headers, color: color, colors: colors)
|
|
14
14
|
table_obj.render(format)
|
|
15
15
|
end
|
|
16
|
+
|
|
17
|
+
def self.to_csv(data, only: nil, headers: {}, delimiter: ",")
|
|
18
|
+
table_obj = TypedPrint::Table.new(data, {}, only, headers)
|
|
19
|
+
table_obj.to_csv(col_sep: delimiter)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.save(data, path, only: nil, headers: {}, delimiter: ",")
|
|
23
|
+
csv = to_csv(data, only: only, headers: headers, delimiter: delimiter)
|
|
24
|
+
File.write(path, "\xEF\xBB\xBF" + csv, encoding: "UTF-8")
|
|
25
|
+
nil
|
|
26
|
+
end
|
|
16
27
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,28 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: typed_print
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ender Ahmet Yurt
|
|
8
8
|
bindir: exe
|
|
9
9
|
cert_chain: []
|
|
10
10
|
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
-
dependencies:
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: pastel
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :development
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
12
26
|
description: TypedPrint provides zero-dependency, beautifully formatted table output
|
|
13
27
|
for Ruby data structures with automatic column sizing, alignment options, custom
|
|
14
28
|
headers, and column filtering.
|
|
@@ -23,6 +37,9 @@ files:
|
|
|
23
37
|
- LICENSE.txt
|
|
24
38
|
- README.md
|
|
25
39
|
- Rakefile
|
|
40
|
+
- assets/demo.gif
|
|
41
|
+
- assets/demo.rb
|
|
42
|
+
- assets/demo.tape
|
|
26
43
|
- lib/typed_print.rb
|
|
27
44
|
- lib/typed_print/table.rb
|
|
28
45
|
- lib/typed_print/version.rb
|