typed_print 0.1.0 → 0.2.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: 0b589c4ef3b3bb0bbc3629c7e6a2a74de7b74a7124bceb46aadc66a7fe957de0
4
- data.tar.gz: 557218da71a928e0ed694c03e07bce59c343226642dcdc1f9231290051d02aaf
3
+ metadata.gz: 85e9b5563417e0d0cb32b45b946ec26f3697a220802c6e012595faecc3d74275
4
+ data.tar.gz: b40e48244c2020e69b5f25c3049468f7431df3b7eb6a60d6a35080ace995cc63
5
5
  SHA512:
6
- metadata.gz: 28efd87ec5f95353c09ae1c9954ee52f843d3031b0228597cbe9a62e13e88eea41bdb87dbd010ca541e0579af6c40cce4fa1bc8e8a90724480ec57e2b3770bb9
7
- data.tar.gz: f9e4a8604633020344fd0b41926eb5b99e9af31075dd7bca0cdcc3ef79871ff0c57643b9a0f24214280ef7a8c84a4ed5ec36a6bae30d36914f0c576b223d5d68
6
+ metadata.gz: f4d9957cd9d3647ee46761fca7ecfc70093958719730d56f39dd10442392dc29d70a6779bed68ff60a45eb1890780d28c4162defec8b2107e90a758b6d224350
7
+ data.tar.gz: c18f2fa2dcebc25b150a914afa7795ce07434aa3b43dd3ad3d0abad64c3ce9e3574ed08a3667e214b33bff7cd1977d0eacdd98f4674de2e4265ddef2e4c2efc0
@@ -11,7 +11,15 @@ module TypedPrint
11
11
  @headers = determine_headers
12
12
  end
13
13
 
14
- def render
14
+ def render(format = :plain)
15
+ if format == :markdown
16
+ render_markdown
17
+ else
18
+ render_plain
19
+ end
20
+ end
21
+
22
+ def render_plain
15
23
  return "" if @data.empty?
16
24
 
17
25
  # Build rows
@@ -22,7 +30,6 @@ module TypedPrint
22
30
  if @custom_headers[h]
23
31
  @custom_headers[h]
24
32
  else
25
- # Capitalize each word in the header
26
33
  h.to_s.split('_').map(&:capitalize).join(' ')
27
34
  end
28
35
  end
@@ -46,6 +53,43 @@ module TypedPrint
46
53
  output.join("\n")
47
54
  end
48
55
 
56
+ def render_markdown
57
+ return "" if @data.empty?
58
+
59
+ # Build rows
60
+ rows = @data.map { |row| @headers.map { |h| format_value(row[h]) } }
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
70
+
71
+ # Calculate column widths for consistent spacing
72
+ column_widths = header_strings.map(&:length)
73
+ rows.each do |row|
74
+ row.each_with_index do |cell, i|
75
+ column_widths[i] = [column_widths[i], cell.length].max
76
+ end
77
+ end
78
+
79
+ # Build markdown table
80
+ output = []
81
+ # Header row
82
+ output << "| " + header_strings.each_with_index.map { |h, i| h.ljust(column_widths[i]) }.join(" | ") + " |"
83
+ # Separator row
84
+ output << "|" + column_widths.map { |w| "-" * (w + 2) }.join("|") + "|"
85
+ # Data rows
86
+ rows.each do |row|
87
+ output << "| " + row.each_with_index.map { |cell, i| cell.ljust(column_widths[i]) }.join(" | ") + " |"
88
+ end
89
+
90
+ output.join("\n")
91
+ end
92
+
49
93
  private
50
94
 
51
95
  def format_value(value)
@@ -61,12 +105,9 @@ module TypedPrint
61
105
  all_keys = @data.flat_map(&:keys).uniq
62
106
 
63
107
  if @only_columns
64
- # Only include specified columns, in the order specified
65
108
  @only_columns.select { |key| all_keys.include?(key) }
66
109
  else
67
- # Preserve the order from the first hash, but ensure all keys are included
68
110
  first_item_keys = @data.first.keys
69
- # Then add any additional keys from other items that weren't in the first
70
111
  first_item_keys + (all_keys - first_item_keys)
71
112
  end
72
113
  end
@@ -74,8 +115,6 @@ module TypedPrint
74
115
  def format_row(cells, widths, alignment_override = nil)
75
116
  cells.each_with_index.map do |cell, i|
76
117
  width = widths[i]
77
-
78
- # Determine alignment - need to use the header key, not the cell value
79
118
  header_key = @headers[i]
80
119
 
81
120
  align = if alignment_override
@@ -91,7 +130,7 @@ module TypedPrint
91
130
  cell.rjust(width)
92
131
  when :center
93
132
  cell.center(width)
94
- else # :left
133
+ else
95
134
  cell.ljust(width)
96
135
  end
97
136
  end.join(" ")
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TypedPrint
4
- VERSION = "0.1.0"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/typed_print.rb CHANGED
@@ -3,17 +3,14 @@
3
3
  require "typed_print/version"
4
4
  require "typed_print/table"
5
5
 
6
- require "typed_print/version"
7
- require "typed_print/table"
8
-
9
6
  module TypedPrint
10
- def self.print(data, align: {}, only: nil, headers: {})
11
- puts table(data, align: align, only: only, headers: headers)
7
+ def self.print(data, align: {}, only: nil, headers: {}, format: :plain)
8
+ puts table(data, align: align, only: only, headers: headers, format: format)
12
9
  nil
13
10
  end
14
11
 
15
- def self.table(data, align: {}, only: nil, headers: {})
12
+ def self.table(data, align: {}, only: nil, headers: {}, format: :plain)
16
13
  table_obj = TypedPrint::Table.new(data, align, only, headers)
17
- table_obj.render
14
+ table_obj.render(format)
18
15
  end
19
16
  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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ender Ahmet Yurt
@@ -49,7 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
49
  - !ruby/object:Gem::Version
50
50
  version: '0'
51
51
  requirements: []
52
- rubygems_version: 3.6.9
52
+ rubygems_version: 4.0.10
53
53
  specification_version: 4
54
54
  summary: Beautiful, aligned table output for Ruby hashes and objects
55
55
  test_files: []