samovar 1.10.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +1 -2
- data/lib/samovar/command.rb +42 -31
- data/lib/samovar/{command/system.rb → error.rb} +26 -26
- data/lib/samovar/many.rb +16 -5
- data/lib/samovar/nested.rb +21 -10
- data/lib/samovar/one.rb +15 -4
- data/lib/samovar/option.rb +107 -0
- data/lib/samovar/options.rb +40 -77
- data/lib/samovar/output.rb +1 -151
- data/lib/samovar/{command/track_time.rb → output/columns.rb} +18 -13
- data/lib/samovar/{command/terminal.rb → output/header.rb} +12 -7
- data/lib/samovar/output/row.rb +38 -0
- data/lib/samovar/output/rows.rb +86 -0
- data/lib/samovar/output/usage_formatter.rb +87 -0
- data/lib/samovar/split.rb +15 -4
- data/lib/samovar/table.rb +61 -13
- data/lib/samovar/version.rb +1 -1
- data/spec/samovar/coerce_spec.rb +19 -1
- data/spec/samovar/command_spec.rb +21 -11
- data/spec/samovar/many_spec.rb +49 -0
- data/spec/samovar/nested_spec.rb +78 -9
- data/spec/samovar/one_spec.rb +49 -0
- data/spec/samovar/options_spec.rb +22 -3
- data/spec/samovar/split_spec.rb +49 -0
- data/spec/samovar/table_spec.rb +40 -0
- data/spec/spec_helper.rb +19 -1
- metadata +17 -6
- data/lib/samovar/output/line_wrapper.rb +0 -87
@@ -0,0 +1,49 @@
|
|
1
|
+
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'samovar/many'
|
22
|
+
|
23
|
+
RSpec.describe Samovar::One do
|
24
|
+
let(:default) {"1"}
|
25
|
+
let(:input) {["2", "3", "4"]}
|
26
|
+
|
27
|
+
subject{described_class.new(:thing, "a thing", default: default)}
|
28
|
+
|
29
|
+
it "has string representation" do
|
30
|
+
expect(subject.to_s).to be == "<thing>"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have default" do
|
34
|
+
expect(subject.default).to be == default
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should use default" do
|
38
|
+
expect(subject.parse([])).to be == default
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should use specified default" do
|
42
|
+
expect(subject.parse([], nil, "2")).to be == "2"
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not use default if input specified" do
|
46
|
+
expect(subject.parse(input)).to be == "2"
|
47
|
+
expect(input).to be == ["3", "4"]
|
48
|
+
end
|
49
|
+
end
|
@@ -1,3 +1,22 @@
|
|
1
|
+
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
1
20
|
|
2
21
|
RSpec.describe Samovar::Options do
|
3
22
|
subject(:options) do
|
@@ -10,17 +29,17 @@ RSpec.describe Samovar::Options do
|
|
10
29
|
end
|
11
30
|
|
12
31
|
it "should set defaults" do
|
13
|
-
values = options.parse([], nil)
|
32
|
+
values = options.parse([], nil, nil)
|
14
33
|
expect(values).to be == {x: 2}
|
15
34
|
end
|
16
35
|
|
17
36
|
it "should preserve current values" do
|
18
|
-
values = options.parse([], {x: 1, y: 2, z: 3})
|
37
|
+
values = options.parse([], nil, {x: 1, y: 2, z: 3})
|
19
38
|
expect(values).to be == {x: 1, y: 2, z: 3}
|
20
39
|
end
|
21
40
|
|
22
41
|
it "should update specified values" do
|
23
|
-
values = options.parse(['-x', 10], {x: 1, y: 2, z: 3})
|
42
|
+
values = options.parse(['-x', 10], nil, {x: 1, y: 2, z: 3})
|
24
43
|
expect(values).to be == {x: 10, y: 2, z: 3}
|
25
44
|
end
|
26
45
|
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'samovar/split'
|
22
|
+
|
23
|
+
RSpec.describe Samovar::Split do
|
24
|
+
let(:default) {["1", "2", "3"]}
|
25
|
+
let(:input) {["2", "--", "3"]}
|
26
|
+
|
27
|
+
subject{described_class.new(:arguments, "arguments", default: default)}
|
28
|
+
|
29
|
+
it "has string representation" do
|
30
|
+
expect(subject.to_s).to be == "-- <arguments...>"
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should have default" do
|
34
|
+
expect(subject.default).to be == default
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should use default" do
|
38
|
+
expect(subject.parse([])).to be == default
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should use specified default" do
|
42
|
+
expect(subject.parse([], nil, ["2"])).to be == ["2"]
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should not use default if input specified" do
|
46
|
+
expect(subject.parse(input)).to be == ["3"]
|
47
|
+
expect(input).to be == ["2"]
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'samovar/table'
|
22
|
+
require 'samovar/options'
|
23
|
+
|
24
|
+
RSpec.describe Samovar::Table do
|
25
|
+
let(:parent) {described_class.new}
|
26
|
+
subject{described_class.new(parent)}
|
27
|
+
|
28
|
+
it "can merge options" do
|
29
|
+
parent << Samovar::Options.parse
|
30
|
+
subject << Samovar::Options.parse do
|
31
|
+
option "--help"
|
32
|
+
end
|
33
|
+
|
34
|
+
subject.merged
|
35
|
+
parent.merged
|
36
|
+
|
37
|
+
expect(parent[:options]).to be_empty
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,24 @@
|
|
1
|
+
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
1
20
|
|
2
21
|
require 'covered/rspec'
|
3
|
-
require "samovar"
|
4
22
|
|
5
23
|
RSpec.configure do |config|
|
6
24
|
config.disable_monkey_patching!
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: samovar
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-03-
|
11
|
+
date: 2019-03-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mapping
|
@@ -109,25 +109,32 @@ files:
|
|
109
109
|
- Rakefile
|
110
110
|
- lib/samovar.rb
|
111
111
|
- lib/samovar/command.rb
|
112
|
-
- lib/samovar/
|
113
|
-
- lib/samovar/command/terminal.rb
|
114
|
-
- lib/samovar/command/track_time.rb
|
112
|
+
- lib/samovar/error.rb
|
115
113
|
- lib/samovar/failure.rb
|
116
114
|
- lib/samovar/flags.rb
|
117
115
|
- lib/samovar/many.rb
|
118
116
|
- lib/samovar/nested.rb
|
119
117
|
- lib/samovar/one.rb
|
118
|
+
- lib/samovar/option.rb
|
120
119
|
- lib/samovar/options.rb
|
121
120
|
- lib/samovar/output.rb
|
122
|
-
- lib/samovar/output/
|
121
|
+
- lib/samovar/output/columns.rb
|
122
|
+
- lib/samovar/output/header.rb
|
123
|
+
- lib/samovar/output/row.rb
|
124
|
+
- lib/samovar/output/rows.rb
|
125
|
+
- lib/samovar/output/usage_formatter.rb
|
123
126
|
- lib/samovar/split.rb
|
124
127
|
- lib/samovar/table.rb
|
125
128
|
- lib/samovar/version.rb
|
126
129
|
- samovar.gemspec
|
127
130
|
- spec/samovar/coerce_spec.rb
|
128
131
|
- spec/samovar/command_spec.rb
|
132
|
+
- spec/samovar/many_spec.rb
|
129
133
|
- spec/samovar/nested_spec.rb
|
134
|
+
- spec/samovar/one_spec.rb
|
130
135
|
- spec/samovar/options_spec.rb
|
136
|
+
- spec/samovar/split_spec.rb
|
137
|
+
- spec/samovar/table_spec.rb
|
131
138
|
- spec/spec_helper.rb
|
132
139
|
- teapot.png
|
133
140
|
homepage: https://github.com/ioquatix/samovar
|
@@ -157,6 +164,10 @@ summary: Samovar is a flexible option parser excellent support for sub-commands
|
|
157
164
|
test_files:
|
158
165
|
- spec/samovar/coerce_spec.rb
|
159
166
|
- spec/samovar/command_spec.rb
|
167
|
+
- spec/samovar/many_spec.rb
|
160
168
|
- spec/samovar/nested_spec.rb
|
169
|
+
- spec/samovar/one_spec.rb
|
161
170
|
- spec/samovar/options_spec.rb
|
171
|
+
- spec/samovar/split_spec.rb
|
172
|
+
- spec/samovar/table_spec.rb
|
162
173
|
- spec/spec_helper.rb
|
@@ -1,87 +0,0 @@
|
|
1
|
-
# Copyright, 2016, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
-
#
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
8
|
-
# furnished to do so, subject to the following conditions:
|
9
|
-
#
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
11
|
-
# all copies or substantial portions of the Software.
|
12
|
-
#
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
# THE SOFTWARE.
|
20
|
-
|
21
|
-
module Samovar
|
22
|
-
module Output
|
23
|
-
# This is an incomplete implementation of an automatic line wrapping output buffer which handles any kind of output, provided it has special wrapping markers.
|
24
|
-
class LineWrapping
|
25
|
-
MARKER = "\e[0;0m".freeze
|
26
|
-
|
27
|
-
def initialize(output, wrapping_width = 80, minimum_width = 20)
|
28
|
-
@output = output
|
29
|
-
@wrapping_width = wrapping_width
|
30
|
-
@minimum_width = minimum_width
|
31
|
-
end
|
32
|
-
|
33
|
-
ESCAPE_SEQUENCE = /(.*?)(\e\[.*?m|$)/
|
34
|
-
|
35
|
-
def printable_width(text)
|
36
|
-
text.size
|
37
|
-
end
|
38
|
-
|
39
|
-
def wrap(line)
|
40
|
-
wrapping_offset = nil
|
41
|
-
offset = 0
|
42
|
-
buffer = String.new
|
43
|
-
lines = []
|
44
|
-
prefix = nil
|
45
|
-
|
46
|
-
line.scan(ESCAPE_SEQUENCE) do |text, escape_sequence|
|
47
|
-
width = printable_width(text)
|
48
|
-
next_offset = offset + printable_width
|
49
|
-
|
50
|
-
if next_offset > @wrapping_width
|
51
|
-
if wrapping_offset
|
52
|
-
text_wrap_offset = @wrapping_width - offset
|
53
|
-
|
54
|
-
# This text flows past the end of the line and we have a valid wrapping offset. We need to wrap this text.
|
55
|
-
if best_split_index = text.rindex(/\s/, text_wrap_offset) and best_split_index >= @minimum_width
|
56
|
-
# We have enough space to wrap.
|
57
|
-
buffer << text[0...best_split_index]
|
58
|
-
lines << buffer
|
59
|
-
buffer = String.new()
|
60
|
-
else
|
61
|
-
# In this case we can't really wrap on the current line. We fall back to letting the terminal wrap.
|
62
|
-
return line
|
63
|
-
end
|
64
|
-
else
|
65
|
-
# We don't have a specific wrapping offset, and the text flows longer than the wrapping width. We can't do anything - let the terminal wrap.
|
66
|
-
return line
|
67
|
-
end
|
68
|
-
else
|
69
|
-
buffer << text << escape_sequence
|
70
|
-
end
|
71
|
-
|
72
|
-
offset = next_offset
|
73
|
-
|
74
|
-
if wrapping_offset.nil? and offset < @wrapping_width and escape_sequence == MARKER
|
75
|
-
wrapping_offset = offset
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def puts(*lines)
|
81
|
-
lines = lines.flat_map{|line| wrap(line)}
|
82
|
-
|
83
|
-
@output.puts(*lines)
|
84
|
-
end
|
85
|
-
end
|
86
|
-
end
|
87
|
-
end
|