cli-format 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4095cc6e627450d530471f19d29bf399c067f308120d31cfac0f176545b648b
4
- data.tar.gz: 614a3d6e47e35bbeeb69a84dfa43c6e6c306e031a4d580a89f72e6ddbba63920
3
+ metadata.gz: cd4f2a9ec7f572ab34075cf59ef687bee6a44f74b9ca6f87d063441893279612
4
+ data.tar.gz: acad0b9dd32e86eae9abd34f7eb076494bb0a4186847dd6b67356d6e3682b0da
5
5
  SHA512:
6
- metadata.gz: 7595f81c809bcc0532906626f3dbbb7c5beed07a8dbfba8b6fc5301772bcea2a93355397f8847f773337bce67106317da9182e58199be8184b67599462912851
7
- data.tar.gz: d314e0f6eee35570425f1be3032c406700f047465ddc03ebcf3cbf09c5c7cde1f6e8ac974fae891c523b72e86d887f2f98842c491bda4018f1bfe407a011dba1
6
+ metadata.gz: 302a90c9c48a58b1d62ba9f65a89dd2c40e66881249bfb1a3d0bcb27d70701192f79248fb6bba3c1749c71730dd804f26200e3152c22da6a7612bb7889735590
7
+ data.tar.gz: bf4b5cd497d34bfb7a94fae537b377ce225f9a69da2731b52c03c54ffc1ee505bc52dc28e6e70aecd1eb60f994a2392f568889989e5fc0be40ef23dd138eead6
data/CHANGELOG.md CHANGED
@@ -3,6 +3,9 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  This project *loosely* adheres to [Semantic Versioning](http://semver.org/), even before v1.0.
5
5
 
6
+ ## [0.3.0] - 2023-09-26
7
+ - [#3](https://github.com/boltops-tools/cli-format/pull/3) space format
8
+
6
9
  ## [0.2.2] - 2022-02-15
7
10
  - [#2](https://github.com/boltops-tools/cli-format/pull/2) add equal format
8
11
 
@@ -0,0 +1,35 @@
1
+ class CliFormat::Presenter
2
+ class Space < Base
3
+ def text
4
+ max_widths = calculate_max_widths
5
+
6
+ @buffer << format_row(@header, max_widths) if @header
7
+ @rows.each do |row|
8
+ @buffer << format_row(row, max_widths)
9
+ end
10
+
11
+ @buffer.join("\n")
12
+ end
13
+
14
+ def calculate_max_widths
15
+ all_rows = [@header, *@rows].compact
16
+ max_widths = Array.new(all_rows[0].size, 0)
17
+
18
+ all_rows.each do |row|
19
+ row.each_with_index do |col, idx|
20
+ max_widths[idx] = [max_widths[idx], col.to_s.length].max
21
+ end
22
+ end
23
+
24
+ max_widths
25
+ end
26
+
27
+ def format_row(row, max_widths)
28
+ formatted_row = row.each_with_index.map do |col, idx|
29
+ idx == row.size - 1 ? col.to_s : col.to_s.ljust(max_widths[idx])
30
+ end
31
+
32
+ formatted_row.join(" ")
33
+ end
34
+ end
35
+ end
@@ -6,10 +6,12 @@ module CliFormat
6
6
  @rows = []
7
7
  end
8
8
 
9
- def show
9
+ delegate :text, :show, to: :presenter
10
+
11
+ def presenter
12
+ return @presenter if @presenter
10
13
  presenter_class = "CliFormat::Presenter::#{format.camelize}".constantize
11
- presenter = presenter_class.new(@options, @header, @rows)
12
- presenter.show
14
+ @presenter = presenter_class.new(@options, @header, @rows)
13
15
  end
14
16
 
15
17
  # Formats: tabs, markdown, json, csv, table, etc
@@ -1,3 +1,3 @@
1
1
  module CliFormat
2
- VERSION = "0.2.2"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/cli_format.rb CHANGED
@@ -9,7 +9,7 @@ module CliFormat
9
9
  # Your code goes here...
10
10
 
11
11
  def self.formats
12
- %w[csv equal table tab json]
12
+ %w[csv equal json space tab table]
13
13
  end
14
14
 
15
15
  cattr_accessor :default_format
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli-format
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tung Nguyen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-15 00:00:00.000000000 Z
11
+ date: 2023-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -120,6 +120,7 @@ files:
120
120
  - lib/cli_format/presenter/csv.rb
121
121
  - lib/cli_format/presenter/equal.rb
122
122
  - lib/cli_format/presenter/json.rb
123
+ - lib/cli_format/presenter/space.rb
123
124
  - lib/cli_format/presenter/tab.rb
124
125
  - lib/cli_format/presenter/table.rb
125
126
  - lib/cli_format/version.rb
@@ -143,7 +144,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
143
144
  - !ruby/object:Gem::Version
144
145
  version: '0'
145
146
  requirements: []
146
- rubygems_version: 3.2.32
147
+ rubygems_version: 3.4.17
147
148
  signing_key:
148
149
  specification_version: 4
149
150
  summary: 'Format cli output in different ways: tab, table, csv, json, etc'