typed_print 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ded9b786c7fc0596377055e783a299fe9eab5558d0d375d978f8186ab72c840
4
- data.tar.gz: d6433125b154f6b2deaaf7764b2b02919d72d4e25e53edc1a2af379265289c96
3
+ metadata.gz: d9efbcc2dccb48448275cea750dea3f3e8682d669916f9968fa27ce9ea84860a
4
+ data.tar.gz: 5b00aac3cd4941932a298285ed7931ea7a62cef5eb2667d045c7201b3860bf27
5
5
  SHA512:
6
- metadata.gz: 8d7b5bd554c0e68e5d8846103e2020db279a96f3ce7d075281492a42570f7d055dd58c38a15bfe8d3be8b6af67a1f30c6c3d81d476c5d080d533049ad62a8561
7
- data.tar.gz: 27cdf2be6875f4a47f666c5716db1b50b1ffa3136d3ba548a8d8d6f8a123be07ae3f73d8a92c1a6031f75b3fd03011490aea81508bb5140678fbb09faba9b820
6
+ metadata.gz: 1a8b3bbc69003ba7711849e746e71eb2cfce4c017842e8e6104c500b71830a415a03234716d6837ef9d77e4ae336231a41f78399f95f15e26a961847e48cd46a
7
+ data.tar.gz: c3f40b2ef369fcf2106dba9cf85792981ecd901a503b6904ef584f5f04f0783b910de90836506df91a97616618884f2134d648a102cd8fe817534c28953cf9cf
data/CHANGELOG.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # Changelog
2
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
+
3
11
  ## [0.3.0] - 2026-04-27
4
12
 
5
13
  ### Added
data/README.md CHANGED
@@ -21,6 +21,7 @@ Beautiful, aligned table output for Ruby hashes and objects with zero dependenci
21
21
  - 📝 Preserves original column order
22
22
  - 📄 Markdown table output (v0.2.0+)
23
23
  - 🌈 Optional color output via `pastel` gem (v0.3.0+)
24
+ - 📤 CSV export with Excel-compatible encoding (v0.4.0+)
24
25
 
25
26
  ## Installation
26
27
 
@@ -153,6 +154,17 @@ Product B 49.99 false Limited edition
153
154
 
154
155
  Same options as `print`.
155
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
+
156
168
  ### Color Output (v0.3.0+)
157
169
 
158
170
  Color support is optional and requires the `pastel` gem. Add it to your Gemfile:
@@ -174,6 +186,32 @@ TypedPrint.print(data, colors: { name: :cyan, score: :green, active: :yellow })
174
186
 
175
187
  Both `:plain` and `:markdown` formats support color. If `pastel` is not installed, color options are silently ignored and output is plain text.
176
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
+
177
215
  ## Development
178
216
 
179
217
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests.
@@ -27,6 +27,19 @@ module TypedPrint
27
27
  end
28
28
  end
29
29
 
30
+ def to_csv(col_sep: ",")
31
+ require "csv"
32
+ return "" if @data.empty?
33
+
34
+ header_row = @headers.map { |h| header_label(h) }
35
+ rows = @data.map { |row| @headers.map { |h| format_value(row[h]) } }
36
+
37
+ CSV.generate(col_sep: col_sep) do |csv|
38
+ csv << header_row
39
+ rows.each { |row| csv << row }
40
+ end
41
+ end
42
+
30
43
  def render_plain
31
44
  return "" if @data.empty?
32
45
 
@@ -150,6 +163,8 @@ module TypedPrint
150
163
  end
151
164
 
152
165
  def determine_headers
166
+ return [] if @data.empty?
167
+
153
168
  all_keys = @data.flat_map(&:keys).uniq
154
169
 
155
170
  if @only_columns
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TypedPrint
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/typed_print.rb CHANGED
@@ -13,4 +13,15 @@ module TypedPrint
13
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,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: typed_print
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ender Ahmet Yurt