so-pretty 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +15 -0
- data/bin/so-pretty +6 -0
- data/bin/sp +6 -0
- data/lib/so-pretty.rb +153 -0
- data/lib/so-pretty/version.rb +3 -0
- data/so-pretty.gemspec +52 -0
- metadata +93 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ebcbc2fa619e3b9d8456f1173803b48896bb0244
|
4
|
+
data.tar.gz: 9156649bc47e57c81af01f411813c1f14b6e16b0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 213252e82e13d776c79c19b2c4c2a4c2a451adce5aaaf9eeac2a0547b5f161443464a3b3fa3a142cdc684c3ac3dcae21e364af56eca5043d8e6d9b6003800e2d
|
7
|
+
data.tar.gz: a17ad9329dd6218fb7ec7bfa5e6c23f1d834e73368bbbfc39a5a173fe2809f3decd04c09a70f745b27e560f2f95a39e66750945e67edf9c4f2997b8a53d54471
|
data/README.md
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
## Usage
|
2
|
+
|
3
|
+
```
|
4
|
+
Usage: sp [options]
|
5
|
+
-i, --input [instream] Input stream, one of: '-', 'STDIN', or a file name
|
6
|
+
-o, --output [outstream] Output stream, one of: '-', 'STDOUT', 'STDERR' or a file name
|
7
|
+
-f, --format [format] Output format, one of: 'yaml', 'json', 'ruby', 'minjson'
|
8
|
+
```
|
9
|
+
|
10
|
+
## TODO
|
11
|
+
|
12
|
+
- add formats
|
13
|
+
* csv
|
14
|
+
* toml
|
15
|
+
* some specs would be nice
|
data/bin/so-pretty
ADDED
data/bin/sp
ADDED
data/lib/so-pretty.rb
ADDED
@@ -0,0 +1,153 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'json'
|
5
|
+
require 'zlib'
|
6
|
+
require 'yaml'
|
7
|
+
require 'optparse'
|
8
|
+
|
9
|
+
begin
|
10
|
+
require 'awesome_print'
|
11
|
+
rescue LoadError => e
|
12
|
+
STDERR.puts "awesome_print is not installed. Install it to get better printing from the :ruby format. `gem install awesome_print`"
|
13
|
+
end
|
14
|
+
|
15
|
+
module SoPretty
|
16
|
+
class Base
|
17
|
+
def initialize( options = {} )
|
18
|
+
@instream = to_stream( :input, options[:instream] )
|
19
|
+
@outstream = to_stream( :output, options[:outstream] )
|
20
|
+
@outformat = options[:format]
|
21
|
+
end
|
22
|
+
|
23
|
+
def prettify
|
24
|
+
if @outformat == :yaml
|
25
|
+
@outstream.puts YAML::dump( parsed )
|
26
|
+
return
|
27
|
+
end
|
28
|
+
|
29
|
+
if @outformat == :ruby
|
30
|
+
if defined?( AwesomePrint )
|
31
|
+
@outstream.puts parsed.ai( plain: true, index: false )
|
32
|
+
else
|
33
|
+
@outstream.puts parsed.inspect
|
34
|
+
end
|
35
|
+
|
36
|
+
return
|
37
|
+
end
|
38
|
+
|
39
|
+
if @outformat == :minjson
|
40
|
+
@outstream.puts JSON.dump( parsed )
|
41
|
+
return
|
42
|
+
end
|
43
|
+
|
44
|
+
@outstream.puts JSON.pretty_generate( parsed )
|
45
|
+
ensure
|
46
|
+
close_streams
|
47
|
+
end
|
48
|
+
|
49
|
+
def parsed
|
50
|
+
return @parsed if @parsed
|
51
|
+
|
52
|
+
begin
|
53
|
+
data = JSON.load( string )
|
54
|
+
return @parsed = data
|
55
|
+
rescue JSON::ParserError => e
|
56
|
+
end
|
57
|
+
|
58
|
+
begin
|
59
|
+
data = YAML::load( string )
|
60
|
+
return @parsed = data
|
61
|
+
rescue Psych::SyntaxError => e
|
62
|
+
end
|
63
|
+
|
64
|
+
begin
|
65
|
+
data = Kernel.eval( string )
|
66
|
+
return @parsed = data
|
67
|
+
rescue StandardError => e
|
68
|
+
end
|
69
|
+
|
70
|
+
raise ArgumentError, "Input was unparsable as JSON or YAML."
|
71
|
+
end
|
72
|
+
|
73
|
+
def string
|
74
|
+
return @string if @string
|
75
|
+
|
76
|
+
str = @instream.read
|
77
|
+
|
78
|
+
zstream = Zlib::Inflate.new
|
79
|
+
begin
|
80
|
+
buf = zstream.inflate(str)
|
81
|
+
zstream.finish
|
82
|
+
zstream.close
|
83
|
+
|
84
|
+
return @string = buf
|
85
|
+
rescue Exception => e
|
86
|
+
end
|
87
|
+
|
88
|
+
return @string = str
|
89
|
+
end
|
90
|
+
|
91
|
+
def close_streams
|
92
|
+
@instream.flush
|
93
|
+
@instream.close
|
94
|
+
|
95
|
+
@outstream.flush
|
96
|
+
@outstream.close
|
97
|
+
end
|
98
|
+
|
99
|
+
def self.from_cli( argv )
|
100
|
+
options = {
|
101
|
+
:instream => "-",
|
102
|
+
:outstream => "-",
|
103
|
+
:format => :yaml,
|
104
|
+
}
|
105
|
+
|
106
|
+
parser = OptionParser.new do |opts|
|
107
|
+
opts.banner = "Usage: sp [options]"
|
108
|
+
|
109
|
+
opts.on( "-i", "--input [instream]", "Input stream, one of: '-', 'STDIN', or a file name" ) do |instream|
|
110
|
+
options[:instream] = instream
|
111
|
+
end
|
112
|
+
|
113
|
+
opts.on( "-o", "--output [outstream]", "Output stream, one of: '-', 'STDOUT', 'STDERR' or a file name" ) do |outstream|
|
114
|
+
options[:outstream] = outstream
|
115
|
+
end
|
116
|
+
|
117
|
+
opts.on( "-f", "--format [format]", "Output format, one of: 'yaml', 'json', 'ruby', 'minjson'") do |format|
|
118
|
+
options[:format] = format.to_sym
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
parser.parse( argv )
|
123
|
+
|
124
|
+
return self.new( options )
|
125
|
+
end
|
126
|
+
|
127
|
+
private
|
128
|
+
def to_stream( input_output, maybe_stream )
|
129
|
+
case maybe_stream.downcase
|
130
|
+
when "stdin"
|
131
|
+
raise ArgumentError, "Output stream cannot be STDIN"
|
132
|
+
return STDIN
|
133
|
+
when "stdout"
|
134
|
+
raise ArgumentError, "Input stream cannot be STDOUT"
|
135
|
+
return STDOUT
|
136
|
+
when "stderr"
|
137
|
+
raise ArgumentError, "Input stream cannot be STDERR"
|
138
|
+
return STDERR
|
139
|
+
end
|
140
|
+
|
141
|
+
if maybe_stream == "-"
|
142
|
+
if input_output == :input
|
143
|
+
return STDIN
|
144
|
+
end
|
145
|
+
|
146
|
+
return STDOUT
|
147
|
+
end
|
148
|
+
|
149
|
+
mode = input_output == :input ? "r" : "w"
|
150
|
+
return File.open( maybe_stream, mode )
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
data/so-pretty.gemspec
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
lib_path = "./lib"
|
2
|
+
spec_path = "./spec"
|
3
|
+
bin_path = "./bin"
|
4
|
+
|
5
|
+
libs = Dir.glob("#{lib_path}/**/*.rb")
|
6
|
+
libs += Dir.glob("#{lib_path}/**/*.erb")
|
7
|
+
|
8
|
+
specs = Dir.glob("#{spec_path}/**/*.rb")
|
9
|
+
bins = Dir.glob("#{bin_path}/so-pretty")
|
10
|
+
bins += Dir.glob("#{bin_path}/sp")
|
11
|
+
|
12
|
+
flist = libs + specs + bins
|
13
|
+
flist << "so-pretty.gemspec"
|
14
|
+
flist << "README.md"
|
15
|
+
|
16
|
+
require_relative './lib/so-pretty/version'
|
17
|
+
|
18
|
+
summary = <<-EOD
|
19
|
+
A gem to pretty print anything. Currently supports input formats:
|
20
|
+
|
21
|
+
- ruby (.inspect formatted)
|
22
|
+
- yaml
|
23
|
+
- json
|
24
|
+
|
25
|
+
And will auto-detect input type, though you can force detection.
|
26
|
+
|
27
|
+
Outputs yaml by default, but can output ruby, yaml, or json. Input streams
|
28
|
+
or files may be compressed with zlib/deflate, however, gzip is not currently
|
29
|
+
supported.
|
30
|
+
EOD
|
31
|
+
|
32
|
+
Gem::Specification.new do |gem|
|
33
|
+
gem.name = "so-pretty"
|
34
|
+
gem.version = SoPretty::VERSION
|
35
|
+
gem.authors = ["Brett Nakashima"]
|
36
|
+
gem.email = ["brettnak@gmail.com"]
|
37
|
+
gem.summary = "SoPretty print anything"
|
38
|
+
gem.description = summary
|
39
|
+
|
40
|
+
# TODO: Rename this repo to match the gem name
|
41
|
+
gem.homepage = "http://gitlab.com/brettnak/pretty"
|
42
|
+
|
43
|
+
gem.files = flist
|
44
|
+
gem.executables = bins.map{ |f| File.basename(f) }
|
45
|
+
gem.test_files = specs
|
46
|
+
gem.require_paths = ["lib/"]
|
47
|
+
|
48
|
+
gem.add_development_dependency 'rspec', '~> 3.1'
|
49
|
+
gem.add_development_dependency 'awesome_print'
|
50
|
+
|
51
|
+
gem.license = "MIT"
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: so-pretty
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brett Nakashima
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-09-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: awesome_print
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
description: |
|
42
|
+
A gem to pretty print anything. Currently supports input formats:
|
43
|
+
|
44
|
+
- ruby (.inspect formatted)
|
45
|
+
- yaml
|
46
|
+
- json
|
47
|
+
|
48
|
+
And will auto-detect input type, though you can force detection.
|
49
|
+
|
50
|
+
Outputs yaml by default, but can output ruby, yaml, or json. Input streams
|
51
|
+
or files may be compressed with zlib/deflate, however, gzip is not currently
|
52
|
+
supported.
|
53
|
+
email:
|
54
|
+
- brettnak@gmail.com
|
55
|
+
executables:
|
56
|
+
- so-pretty
|
57
|
+
- sp
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- "./bin/so-pretty"
|
62
|
+
- "./bin/sp"
|
63
|
+
- "./lib/so-pretty.rb"
|
64
|
+
- "./lib/so-pretty/version.rb"
|
65
|
+
- README.md
|
66
|
+
- bin/so-pretty
|
67
|
+
- bin/sp
|
68
|
+
- so-pretty.gemspec
|
69
|
+
homepage: http://gitlab.com/brettnak/pretty
|
70
|
+
licenses:
|
71
|
+
- MIT
|
72
|
+
metadata: {}
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib/
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '0'
|
82
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
requirements: []
|
88
|
+
rubyforge_project:
|
89
|
+
rubygems_version: 2.6.12
|
90
|
+
signing_key:
|
91
|
+
specification_version: 4
|
92
|
+
summary: SoPretty print anything
|
93
|
+
test_files: []
|