cli-format 0.4.0 → 0.6.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: '082a3544f56a8fe91e0250769c433422dc4c02a045dca8e9f7e374ca954e4aab'
4
- data.tar.gz: '09d4753e88d4055d96e3c0028ea15a61764c0f65dc8699671cbb174abeaff9e7'
3
+ metadata.gz: ed37b92279060591308aba623808d5ea5e3dbe5f536bd2558dc4a64d0929488f
4
+ data.tar.gz: 73b6ae66721753de030a17fa035773d227ec25d27c02fba45ac550e322853f59
5
5
  SHA512:
6
- metadata.gz: e557258c906638acfa29d8b7b459b5f423bf1a0c5bc71fa80a4cedf518d45214157f210c03f2c1d9119c1d40a85c58d270ea8acdeb85055f212fc17328478270
7
- data.tar.gz: f4c036785d8a0590985dbd8ec2a5c1aa73f258645e4adf364d3a79f6b0b75e43fc6ac705ce710e2aed97ce06f4e57010df5b04aa67b0426411617f4c2b5596f9
6
+ metadata.gz: 36d90336bc7a83a91376d1710add6cddbf5c8969612d933493c49e969da7df696c22b5258283aa53f06f1a543cc64a6bbec7452a6ad089d38b137a9eb104d572
7
+ data.tar.gz: ca3aecae2d366a4c05f2deca19873562b94c28f945fdd9ac7a8085754a4d58fbb1a89d153bf409181b24dbc34394a15086c748fed850fbac885bd20338eb7a26
data/CHANGELOG.md CHANGED
@@ -3,6 +3,16 @@
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.6.0] - 2024-04-15
7
+ - [#5](https://github.com/boltops-tools/cli-format/pull/5) More formats
8
+ - dotenv format
9
+ - empty_message support
10
+ - info format
11
+ - update deps
12
+
13
+ ## [0.5.0] - 2023-12-02
14
+ - make options optional
15
+
6
16
  ## [0.4.0] - 2023-10-04
7
17
  - [#4](https://github.com/boltops-tools/cli-format/pull/4) markdown format support
8
18
 
data/cli-format.gemspec CHANGED
@@ -3,31 +3,31 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
  require "cli_format/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
- spec.name = "cli-format"
7
- spec.version = CliFormat::VERSION
8
- spec.authors = ["Tung Nguyen"]
9
- spec.email = ["tongueroo@gmail.com"]
6
+ spec.name = "cli-format"
7
+ spec.version = CliFormat::VERSION
8
+ spec.authors = ["Tung Nguyen"]
9
+ spec.email = ["tongueroo@gmail.com"]
10
10
 
11
- spec.summary = "Format cli output in different ways: tab, table, csv, json, etc"
12
- spec.homepage = "https://github.com/tongueroo/cli-format"
13
- spec.license = "MIT"
11
+ spec.summary = "Format cli output in different ways: tab, table, csv, json, etc"
12
+ spec.homepage = "https://github.com/tongueroo/cli-format"
13
+ spec.license = "MIT"
14
14
 
15
15
  spec.metadata["homepage_uri"] = spec.homepage
16
16
 
17
17
  # Specify which files should be added to the gem when it is released.
18
18
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
20
20
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
21
  end
22
- spec.bindir = "exe"
23
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
24
  spec.require_paths = ["lib"]
25
25
 
26
26
  spec.add_dependency "activesupport"
27
27
  spec.add_dependency "text-table"
28
28
  spec.add_dependency "zeitwerk"
29
29
 
30
- spec.add_development_dependency "bundler", "~> 2.0"
31
- spec.add_development_dependency "rake", "~> 10.0"
32
- spec.add_development_dependency "rspec", "~> 3.0"
30
+ spec.add_development_dependency "bundler", ">= 2.0"
31
+ spec.add_development_dependency "rake", ">= 12.3.3"
32
+ spec.add_development_dependency "rspec", ">= 3.0"
33
33
  end
@@ -1,12 +1,19 @@
1
1
  class CliFormat::Presenter
2
2
  class Base
3
+ attr_accessor :header, :rows
4
+ attr_writer :empty_message
3
5
  def initialize(options, header, rows)
4
6
  @options, @header, @rows = options, header, rows
7
+ @empty_message = "No items found"
5
8
  @buffer = []
6
9
  end
7
10
 
8
11
  def show
9
- puts text
12
+ if @rows.empty?
13
+ puts @empty_message
14
+ else
15
+ puts text
16
+ end
10
17
  end
11
18
  end
12
19
  end
@@ -0,0 +1,11 @@
1
+ class CliFormat::Presenter
2
+ class Dotenv < Base
3
+ def text
4
+ @buffer << @header.join("=") if @header
5
+ @rows.each do |row|
6
+ @buffer << row.join("=")
7
+ end
8
+ @buffer.join("\n")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ class CliFormat::Presenter
2
+ class Info < Base
3
+ # This format probably works best for 2 columns, though it can
4
+ # support an arbitrary number of columns.
5
+ # It produces aligned text output. Example:
6
+ #
7
+ # Name demo
8
+ # Description demo-dev-ci
9
+ # Source GITHUB
10
+ #
11
+ def text
12
+ column_widths = []
13
+ @rows.each do |row|
14
+ next if skip?(row)
15
+ row.each_with_index do |cell, i|
16
+ column_widths[i] = [column_widths[i].to_i, cell.to_s.length].max
17
+ end
18
+ end
19
+
20
+ spacing = @options[:spacing] || " "
21
+ lines = @rows.map do |row|
22
+ items = []
23
+ next if skip?(row)
24
+ row.each_with_index do |cell, i|
25
+ items << cell.to_s.ljust(column_widths[i]) unless cell.nil?
26
+ end
27
+ items.join(spacing)
28
+ end
29
+ lines.compact.join("\n")
30
+ end
31
+
32
+ # special rule to skip when second column is blank
33
+ def skip?(row)
34
+ column_size == 2 && row[1].blank?
35
+ end
36
+
37
+ def column_size
38
+ @rows.first.size
39
+ end
40
+ end
41
+ end
@@ -1,4 +1,4 @@
1
- require 'text-table'
1
+ require "text-table"
2
2
 
3
3
  class CliFormat::Presenter
4
4
  class Table < Base
@@ -1,22 +1,34 @@
1
1
  module CliFormat
2
2
  class Presenter
3
3
  attr_accessor :header, :rows
4
- def initialize(options)
4
+ def initialize(options = {})
5
5
  @options = options
6
6
  @rows = []
7
7
  end
8
8
 
9
- delegate :text, :show, to: :presenter
9
+ delegate :text,
10
+ :empty_message,
11
+ :empty_message=,
12
+ :header,
13
+ :header=,
14
+ :show,
15
+ to: :strategy
10
16
 
11
- def presenter
12
- return @presenter if @presenter
13
- presenter_class = "CliFormat::Presenter::#{format.camelize}".constantize
14
- @presenter = presenter_class.new(@options, @header, @rows)
17
+ def strategy
18
+ return @strategy if @strategy
19
+ strategy_class = begin
20
+ "CliFormat::Presenter::#{format.camelize}".constantize
21
+ rescue NameError => e
22
+ default = CliFormat.default_format
23
+ puts "WARN: format not found: #{format}. Using default format: #{default}"
24
+ "CliFormat::Presenter::#{default.camelize}".constantize
25
+ end
26
+ @strategy = strategy_class.new(@options, @header, @rows)
15
27
  end
16
28
 
17
29
  # Formats: tabs, markdown, json, csv, table, etc
18
30
  def format
19
- @options[:format] || ENV['CLI_FORMAT'] || CliFormat.default_format # table
31
+ @options[:format] || ENV["CLI_FORMAT"] || CliFormat.default_format # table
20
32
  end
21
33
  end
22
34
  end
@@ -1,3 +1,3 @@
1
1
  module CliFormat
2
- VERSION = "0.4.0"
2
+ VERSION = "0.6.0"
3
3
  end
data/lib/cli_format.rb CHANGED
@@ -9,7 +9,17 @@ module CliFormat
9
9
  # Your code goes here...
10
10
 
11
11
  def self.formats
12
- %w[csv equal json markdown space tab table]
12
+ %w[
13
+ csv
14
+ dotenv
15
+ equal
16
+ info
17
+ json
18
+ markdown
19
+ space
20
+ tab
21
+ table
22
+ ]
13
23
  end
14
24
 
15
25
  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.4.0
4
+ version: 0.6.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: 2023-10-04 00:00:00.000000000 Z
11
+ date: 2024-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -56,42 +56,42 @@ dependencies:
56
56
  name: bundler
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '2.0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '2.0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - "~>"
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
- version: '10.0'
75
+ version: 12.3.3
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - "~>"
80
+ - - ">="
81
81
  - !ruby/object:Gem::Version
82
- version: '10.0'
82
+ version: 12.3.3
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rspec
85
85
  requirement: !ruby/object:Gem::Requirement
86
86
  requirements:
87
- - - "~>"
87
+ - - ">="
88
88
  - !ruby/object:Gem::Version
89
89
  version: '3.0'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
- - - "~>"
94
+ - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '3.0'
97
97
  description:
@@ -118,7 +118,9 @@ files:
118
118
  - lib/cli_format/presenter.rb
119
119
  - lib/cli_format/presenter/base.rb
120
120
  - lib/cli_format/presenter/csv.rb
121
+ - lib/cli_format/presenter/dotenv.rb
121
122
  - lib/cli_format/presenter/equal.rb
123
+ - lib/cli_format/presenter/info.rb
122
124
  - lib/cli_format/presenter/json.rb
123
125
  - lib/cli_format/presenter/markdown.rb
124
126
  - lib/cli_format/presenter/space.rb
@@ -145,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
145
147
  - !ruby/object:Gem::Version
146
148
  version: '0'
147
149
  requirements: []
148
- rubygems_version: 3.4.17
150
+ rubygems_version: 3.4.19
149
151
  signing_key:
150
152
  specification_version: 4
151
153
  summary: 'Format cli output in different ways: tab, table, csv, json, etc'