cli-format 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d8aac542c4c1f36ffb6df02868633539c1a3f3fc7f5bf5c1d42cd22eb8925a20
4
+ data.tar.gz: bf568ed13e7f0e3e6ffdf6331f6983236ec1fe1a8cb963a3fef4b65a1a16b6a6
5
+ SHA512:
6
+ metadata.gz: 25570ef38bfef3dc4ae4d91cb14b79173c37e8da0b444a69b3c1ebc36d2ef2285e976c7e96faddac074474fef84633f8c6953d427384087bbcac84807e217a67
7
+ data.tar.gz: e0623dd69a6f1bf1c7a988ed4d5cff25cecdbf83c5dfece00ee0680659443205d34805eef6236432c129ea69668538083d135ae5c6b1667229f2901b8f6e0608
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.6
7
+ before_install: gem install bundler -v 2.0.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in cli_format.gemspec
4
+ gemspec
@@ -0,0 +1,53 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ cli-format (0.1.0)
5
+ activesupport
6
+ text-table
7
+ zeitwerk
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ activesupport (6.0.2.1)
13
+ concurrent-ruby (~> 1.0, >= 1.0.2)
14
+ i18n (>= 0.7, < 2)
15
+ minitest (~> 5.1)
16
+ tzinfo (~> 1.1)
17
+ zeitwerk (~> 2.2)
18
+ concurrent-ruby (1.1.5)
19
+ diff-lcs (1.3)
20
+ i18n (1.8.2)
21
+ concurrent-ruby (~> 1.0)
22
+ minitest (5.14.0)
23
+ rake (10.5.0)
24
+ rspec (3.9.0)
25
+ rspec-core (~> 3.9.0)
26
+ rspec-expectations (~> 3.9.0)
27
+ rspec-mocks (~> 3.9.0)
28
+ rspec-core (3.9.1)
29
+ rspec-support (~> 3.9.1)
30
+ rspec-expectations (3.9.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.9.0)
33
+ rspec-mocks (3.9.1)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.9.0)
36
+ rspec-support (3.9.2)
37
+ text-table (1.2.4)
38
+ thread_safe (0.3.6)
39
+ tzinfo (1.2.6)
40
+ thread_safe (~> 0.1)
41
+ zeitwerk (2.2.2)
42
+
43
+ PLATFORMS
44
+ ruby
45
+
46
+ DEPENDENCIES
47
+ bundler (~> 2.0)
48
+ cli-format!
49
+ rake (~> 10.0)
50
+ rspec (~> 3.0)
51
+
52
+ BUNDLED WITH
53
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Tung Nguyen
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.
@@ -0,0 +1,94 @@
1
+ # cli-format
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'cli-format'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ require "cli-format"
19
+
20
+ options = {header: true, format: "table"}
21
+ header = ["Name", "Food", "Color"]
22
+ data = [
23
+ ["Tung", "Apple", "Yellow"],
24
+ ["Bob", "Pear", "Blue"],
25
+ ]
26
+
27
+ presenter = CliFormat::Presenter.new(options)
28
+ presenter.header = header
29
+ data.each do |row|
30
+ presenter.rows << row
31
+ end
32
+ presenter.show
33
+ ```
34
+
35
+ Format table shows:
36
+
37
+ +------+-------+--------+
38
+ | Name | Food | Color |
39
+ +------+-------+--------+
40
+ | Tung | Apple | Yellow |
41
+ | Bob | Pear | Blue |
42
+ +------+-------+--------+
43
+
44
+ Format csv shows:
45
+
46
+ Name,Food,Color
47
+ Tung,Apple,Yellow
48
+ Bob,Pear,Blue
49
+
50
+ Format json shows:
51
+
52
+ ```json
53
+ {
54
+ "header": [
55
+ "Name",
56
+ "Food",
57
+ "Color"
58
+ ],
59
+ "data": [
60
+ [
61
+ "Tung",
62
+ "Apple",
63
+ "Yellow"
64
+ ],
65
+ [
66
+ "Bob",
67
+ "Pear",
68
+ "Blue"
69
+ ]
70
+ ]
71
+ }
72
+ ```
73
+
74
+ ## Text
75
+
76
+ If you would like to grab just the text and not it shown, use the `text` method.
77
+
78
+ ```ruby
79
+ presenter.text
80
+ ```
81
+
82
+ ## Development
83
+
84
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
85
+
86
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cli_format.
91
+
92
+ ## License
93
+
94
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "cli_format"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,33 @@
1
+ lib = File.expand_path("lib", __dir__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "cli_format/version"
4
+
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"]
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"
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+
17
+ # Specify which files should be added to the gem when it is released.
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
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "activesupport"
27
+ spec.add_dependency "text-table"
28
+ spec.add_dependency "zeitwerk"
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"
33
+ end
@@ -0,0 +1 @@
1
+ require_relative "cli_format"
@@ -0,0 +1,13 @@
1
+ require "active_support/core_ext/string"
2
+ require "cli_format/version"
3
+ require "cli_format/autoloader"
4
+ CliFormat::Autoloader.setup
5
+
6
+ module CliFormat
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+
10
+ def self.formats
11
+ %w[csv table tab json]
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ require "zeitwerk"
2
+
3
+ module CliFormat
4
+ class Autoloader
5
+ class Inflector < Zeitwerk::Inflector
6
+ def camelize(basename, _abspath)
7
+ map = { cli: "CLI", version: "VERSION" }
8
+ map[basename.to_sym] || super
9
+ end
10
+ end
11
+
12
+ class << self
13
+ def setup
14
+ loader = Zeitwerk::Loader.new
15
+ loader.inflector = Inflector.new
16
+ loader.push_dir(File.dirname(__dir__)) # lib
17
+ loader.ignore("#{File.dirname(__dir__)}/cli-format.rb")
18
+ loader.setup
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,20 @@
1
+ module CliFormat
2
+ class Presenter
3
+ attr_accessor :header, :rows
4
+ def initialize(options)
5
+ @options = options
6
+ @rows = []
7
+ end
8
+
9
+ def show
10
+ presenter_class = "CliFormat::Presenter::#{format.classify}".constantize
11
+ presenter = presenter_class.new(@options, @header, @rows)
12
+ presenter.show
13
+ end
14
+
15
+ # Formats: tabs, markdown, json, csv, table, etc
16
+ def format
17
+ @options[:format] || ENV['CLI_FORMAT'] || "table"
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,12 @@
1
+ class CliFormat::Presenter
2
+ class Base
3
+ def initialize(options, header, rows)
4
+ @options, @header, @rows = options, header, rows
5
+ @buffer = []
6
+ end
7
+
8
+ def show
9
+ puts text
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,14 @@
1
+ require 'csv'
2
+
3
+ class CliFormat::Presenter
4
+ class Csv < Base
5
+ def text
6
+ CSV.generate do |csv|
7
+ csv << @header if @header
8
+ @rows.each do |row|
9
+ csv << row
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,13 @@
1
+ require "json"
2
+
3
+ class CliFormat::Presenter
4
+ class Json < Base
5
+ def text
6
+ json_data = {
7
+ header: @header,
8
+ data: @rows
9
+ }
10
+ JSON.pretty_generate(json_data)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,11 @@
1
+ class CliFormat::Presenter
2
+ class Tab < Base
3
+ def text
4
+ @buffer << @header.join("\t") if @header
5
+ @rows.each do |row|
6
+ @buffer << row.join("\t")
7
+ end
8
+ @buffer.join("\n")
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,12 @@
1
+ require 'text-table'
2
+
3
+ class CliFormat::Presenter
4
+ class Table < Base
5
+ def text
6
+ table = Text::Table.new
7
+ table.head = @header if @header
8
+ table.rows = @rows
9
+ table
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module CliFormat
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cli-format
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tung Nguyen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: text-table
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: zeitwerk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ description:
98
+ email:
99
+ - tongueroo@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - Gemfile
108
+ - Gemfile.lock
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - cli-format.gemspec
115
+ - lib/cli-format.rb
116
+ - lib/cli_format.rb
117
+ - lib/cli_format/autoloader.rb
118
+ - lib/cli_format/presenter.rb
119
+ - lib/cli_format/presenter/base.rb
120
+ - lib/cli_format/presenter/csv.rb
121
+ - lib/cli_format/presenter/json.rb
122
+ - lib/cli_format/presenter/tab.rb
123
+ - lib/cli_format/presenter/table.rb
124
+ - lib/cli_format/version.rb
125
+ homepage: https://github.com/tongueroo/cli-format
126
+ licenses:
127
+ - MIT
128
+ metadata:
129
+ homepage_uri: https://github.com/tongueroo/cli-format
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubygems_version: 3.1.2
146
+ signing_key:
147
+ specification_version: 4
148
+ summary: 'Format cli output in different ways: tab, table, csv, json, etc'
149
+ test_files: []