typed_print 0.1.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 +7 -0
- data/CHANGELOG.md +13 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE.txt +21 -0
- data/README.md +159 -0
- data/Rakefile +12 -0
- data/lib/typed_print/table.rb +104 -0
- data/lib/typed_print/version.rb +5 -0
- data/lib/typed_print.rb +19 -0
- data/sig/typed_print.rbs +4 -0
- metadata +55 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 0b589c4ef3b3bb0bbc3629c7e6a2a74de7b74a7124bceb46aadc66a7fe957de0
|
|
4
|
+
data.tar.gz: 557218da71a928e0ed694c03e07bce59c343226642dcdc1f9231290051d02aaf
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 28efd87ec5f95353c09ae1c9954ee52f843d3031b0228597cbe9a62e13e88eea41bdb87dbd010ca541e0579af6c40cce4fa1bc8e8a90724480ec57e2b3770bb9
|
|
7
|
+
data.tar.gz: f9e4a8604633020344fd0b41926eb5b99e9af31075dd7bca0cdcc3ef79871ff0c57643b9a0f24214280ef7a8c84a4ed5ec36a6bae30d36914f0c576b223d5d68
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
## [0.1.0] - 2026-04-21
|
|
2
|
+
|
|
3
|
+
### Added
|
|
4
|
+
- Initial release
|
|
5
|
+
- Basic table formatting for Ruby hashes
|
|
6
|
+
- Column alignment options (left, right, center)
|
|
7
|
+
- Column filtering with `:only` option
|
|
8
|
+
- Custom headers with `:headers` option
|
|
9
|
+
- Smart type formatting (booleans, nil, strings)
|
|
10
|
+
- Preserves original column order
|
|
11
|
+
- `TypedPrint.print` method for printing to stdout
|
|
12
|
+
- `TypedPrint.table` method for returning formatted string
|
|
13
|
+
- Zero dependencies
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Code of Conduct
|
|
2
|
+
|
|
3
|
+
"typed_print" follows [The Ruby Community Conduct Guideline](https://www.ruby-lang.org/en/conduct) in all "collaborative space", which is defined as community communications channels (such as mailing lists, submitted patches, commit comments, etc.):
|
|
4
|
+
|
|
5
|
+
* Participants will be tolerant of opposing views.
|
|
6
|
+
* Participants must ensure that their language and actions are free of personal attacks and disparaging personal remarks.
|
|
7
|
+
* When interpreting the words and actions of others, participants should always assume good intentions.
|
|
8
|
+
* Behaviour which can be reasonably considered harassment will not be tolerated.
|
|
9
|
+
|
|
10
|
+
If you have any concerns about behaviour within this project, please contact us at ["enderyurt@gmail.com"](mailto:"enderyurt@gmail.com").
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ender Ahmet Yurt
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
# TypedPrint
|
|
2
|
+
|
|
3
|
+
Beautiful, aligned table output for Ruby hashes and objects with zero dependencies.
|
|
4
|
+
|
|
5
|
+
[](https://badge.fury.io/rb/typed_print)
|
|
6
|
+
[](https://github.com/enderahmetyurt/typed_print/actions/workflows/main.yml)
|
|
7
|
+
[](https://www.ruby-lang.org/)
|
|
8
|
+
[](https://opensource.org/licenses/MIT)
|
|
9
|
+
[](https://rubygems.org/gems/typed_print)
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 🚀 Zero dependencies
|
|
14
|
+
- 📊 Automatic column width calculation
|
|
15
|
+
- 🎯 Smart type formatting (booleans, nil, strings)
|
|
16
|
+
- 📐 Column alignment (left, right, center)
|
|
17
|
+
- 🎨 Custom column headers
|
|
18
|
+
- 🔍 Column filtering
|
|
19
|
+
- 📝 Preserves original column order
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
Add this line to your application's Gemfile:
|
|
24
|
+
|
|
25
|
+
```ruby
|
|
26
|
+
gem 'typed_print'
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Or install it yourself:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
gem install typed_print
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage
|
|
36
|
+
|
|
37
|
+
### Basic Usage
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
require 'typed_print'
|
|
41
|
+
|
|
42
|
+
data = [
|
|
43
|
+
{ name: "Alice", score: 100, active: true },
|
|
44
|
+
{ name: "Bob", score: 42, active: false }
|
|
45
|
+
]
|
|
46
|
+
|
|
47
|
+
TypedPrint.print(data)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
Output:
|
|
51
|
+
```
|
|
52
|
+
Name Score Active
|
|
53
|
+
------+------+-------
|
|
54
|
+
Alice 100 true
|
|
55
|
+
Bob 42 false
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Column Alignment
|
|
59
|
+
|
|
60
|
+
Right-align specific columns:
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
TypedPrint.print(data, align: { score: :right })
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
Output:
|
|
67
|
+
```
|
|
68
|
+
Name Score Active
|
|
69
|
+
------+------+-------
|
|
70
|
+
Alice 100 true
|
|
71
|
+
Bob 42 false
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Filter Columns
|
|
75
|
+
|
|
76
|
+
Show only specific columns:
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
TypedPrint.print(data, only: [:name, :score])
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Output:
|
|
83
|
+
```
|
|
84
|
+
Name Score
|
|
85
|
+
------+------
|
|
86
|
+
Alice 100
|
|
87
|
+
Bob 42
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Custom Headers
|
|
91
|
+
|
|
92
|
+
Rename columns for display:
|
|
93
|
+
|
|
94
|
+
```ruby
|
|
95
|
+
TypedPrint.print(data, headers: { name: "Username", score: "Points", active: "Status" })
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Output:
|
|
99
|
+
```
|
|
100
|
+
Username Points Status
|
|
101
|
+
---------+------+-------
|
|
102
|
+
Alice 100 true
|
|
103
|
+
Bob 42 false
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### Return String Instead of Printing
|
|
107
|
+
|
|
108
|
+
Use `table` method to get the formatted string:
|
|
109
|
+
|
|
110
|
+
```ruby
|
|
111
|
+
table_string = TypedPrint.table(data)
|
|
112
|
+
puts table_string.upcase
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Working with Different Data Types
|
|
116
|
+
|
|
117
|
+
```ruby
|
|
118
|
+
mixed_data = [
|
|
119
|
+
{ name: "Product A", price: 29.99, in_stock: true, notes: nil },
|
|
120
|
+
{ name: "Product B", price: 49.99, in_stock: false, notes: "Limited edition" }
|
|
121
|
+
]
|
|
122
|
+
|
|
123
|
+
TypedPrint.print(mixed_data)
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Output:
|
|
127
|
+
```
|
|
128
|
+
Name Price In_stock Notes
|
|
129
|
+
----------+-------+---------+-------------
|
|
130
|
+
Product A 29.99 true
|
|
131
|
+
Product B 49.99 false Limited edition
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## API Reference
|
|
135
|
+
|
|
136
|
+
`TypedPrint.print(data, options)` Prints the formatted table to stdout and returns `nil`.
|
|
137
|
+
|
|
138
|
+
**Options:**
|
|
139
|
+
- `align: Hash` - Column alignment (`:left`, `:right`, `:center`), defaults to `:left`
|
|
140
|
+
- `only: Array` - Array of column symbols to display
|
|
141
|
+
- `headers: Hash` - Custom headers for columns
|
|
142
|
+
|
|
143
|
+
`TypedPrint.table(data, options)` returns the formatted table as a string.
|
|
144
|
+
|
|
145
|
+
Same options as `print`.
|
|
146
|
+
|
|
147
|
+
## Development
|
|
148
|
+
|
|
149
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
|
|
150
|
+
|
|
151
|
+
To install this gem onto your local machine, run `bundle exec rake install`.
|
|
152
|
+
|
|
153
|
+
## Contributing
|
|
154
|
+
|
|
155
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/yourusername/typed_print.
|
|
156
|
+
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module TypedPrint
|
|
4
|
+
class Table
|
|
5
|
+
def initialize(data, alignments = {}, only_columns = nil, custom_headers = {})
|
|
6
|
+
@data = data
|
|
7
|
+
@alignments = alignments
|
|
8
|
+
@only_columns = only_columns&.map(&:to_sym)
|
|
9
|
+
@custom_headers = custom_headers.transform_keys(&:to_sym)
|
|
10
|
+
|
|
11
|
+
@headers = determine_headers
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def render
|
|
15
|
+
return "" if @data.empty?
|
|
16
|
+
|
|
17
|
+
# Build rows
|
|
18
|
+
rows = @data.map { |row| @headers.map { |h| format_value(row[h]) } }
|
|
19
|
+
|
|
20
|
+
# Build header strings (with custom headers or capitalized defaults)
|
|
21
|
+
header_strings = @headers.map do |h|
|
|
22
|
+
if @custom_headers[h]
|
|
23
|
+
@custom_headers[h]
|
|
24
|
+
else
|
|
25
|
+
# Capitalize each word in the header
|
|
26
|
+
h.to_s.split('_').map(&:capitalize).join(' ')
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Calculate column widths
|
|
31
|
+
column_widths = header_strings.map(&:length)
|
|
32
|
+
rows.each do |row|
|
|
33
|
+
row.each_with_index do |cell, i|
|
|
34
|
+
column_widths[i] = [column_widths[i], cell.length].max
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Build output
|
|
39
|
+
output = []
|
|
40
|
+
output << format_row(header_strings, column_widths, :center)
|
|
41
|
+
output << separator(column_widths)
|
|
42
|
+
rows.each do |row|
|
|
43
|
+
output << format_row(row, column_widths)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
output.join("\n")
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def format_value(value)
|
|
52
|
+
case value
|
|
53
|
+
when true then "true"
|
|
54
|
+
when false then "false"
|
|
55
|
+
when nil then ""
|
|
56
|
+
else value.to_s
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def determine_headers
|
|
61
|
+
all_keys = @data.flat_map(&:keys).uniq
|
|
62
|
+
|
|
63
|
+
if @only_columns
|
|
64
|
+
# Only include specified columns, in the order specified
|
|
65
|
+
@only_columns.select { |key| all_keys.include?(key) }
|
|
66
|
+
else
|
|
67
|
+
# Preserve the order from the first hash, but ensure all keys are included
|
|
68
|
+
first_item_keys = @data.first.keys
|
|
69
|
+
# Then add any additional keys from other items that weren't in the first
|
|
70
|
+
first_item_keys + (all_keys - first_item_keys)
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def format_row(cells, widths, alignment_override = nil)
|
|
75
|
+
cells.each_with_index.map do |cell, i|
|
|
76
|
+
width = widths[i]
|
|
77
|
+
|
|
78
|
+
# Determine alignment - need to use the header key, not the cell value
|
|
79
|
+
header_key = @headers[i]
|
|
80
|
+
|
|
81
|
+
align = if alignment_override
|
|
82
|
+
alignment_override
|
|
83
|
+
elsif @alignments[header_key]
|
|
84
|
+
@alignments[header_key]
|
|
85
|
+
else
|
|
86
|
+
:left
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
case align
|
|
90
|
+
when :right
|
|
91
|
+
cell.rjust(width)
|
|
92
|
+
when :center
|
|
93
|
+
cell.center(width)
|
|
94
|
+
else # :left
|
|
95
|
+
cell.ljust(width)
|
|
96
|
+
end
|
|
97
|
+
end.join(" ")
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def separator(widths)
|
|
101
|
+
widths.map { |w| "-" * w }.join("-+-")
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
data/lib/typed_print.rb
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "typed_print/version"
|
|
4
|
+
require "typed_print/table"
|
|
5
|
+
|
|
6
|
+
require "typed_print/version"
|
|
7
|
+
require "typed_print/table"
|
|
8
|
+
|
|
9
|
+
module TypedPrint
|
|
10
|
+
def self.print(data, align: {}, only: nil, headers: {})
|
|
11
|
+
puts table(data, align: align, only: only, headers: headers)
|
|
12
|
+
nil
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.table(data, align: {}, only: nil, headers: {})
|
|
16
|
+
table_obj = TypedPrint::Table.new(data, align, only, headers)
|
|
17
|
+
table_obj.render
|
|
18
|
+
end
|
|
19
|
+
end
|
data/sig/typed_print.rbs
ADDED
metadata
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: typed_print
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Ender Ahmet Yurt
|
|
8
|
+
bindir: exe
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
description: TypedPrint provides zero-dependency, beautifully formatted table output
|
|
13
|
+
for Ruby data structures with automatic column sizing, alignment options, custom
|
|
14
|
+
headers, and column filtering.
|
|
15
|
+
email:
|
|
16
|
+
- enderyurt@gmail.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- CHANGELOG.md
|
|
22
|
+
- CODE_OF_CONDUCT.md
|
|
23
|
+
- LICENSE.txt
|
|
24
|
+
- README.md
|
|
25
|
+
- Rakefile
|
|
26
|
+
- lib/typed_print.rb
|
|
27
|
+
- lib/typed_print/table.rb
|
|
28
|
+
- lib/typed_print/version.rb
|
|
29
|
+
- sig/typed_print.rbs
|
|
30
|
+
homepage: https://github.com/enderahmetyurt/typed_print
|
|
31
|
+
licenses:
|
|
32
|
+
- MIT
|
|
33
|
+
metadata:
|
|
34
|
+
allowed_push_host: https://rubygems.org
|
|
35
|
+
homepage_uri: https://github.com/enderahmetyurt/typed_print
|
|
36
|
+
source_code_uri: https://github.com/enderahmetyurt/typed_print
|
|
37
|
+
changelog_uri: https://github.com/enderahmetyurt/typed_print/blob/main/CHANGELOG.md
|
|
38
|
+
rdoc_options: []
|
|
39
|
+
require_paths:
|
|
40
|
+
- lib
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 2.6.0
|
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: '0'
|
|
51
|
+
requirements: []
|
|
52
|
+
rubygems_version: 3.6.9
|
|
53
|
+
specification_version: 4
|
|
54
|
+
summary: Beautiful, aligned table output for Ruby hashes and objects
|
|
55
|
+
test_files: []
|