vv 0.0.10 → 0.0.11

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: 51e268d782cc4e4615a19f9bef609d810465b50bf74fa7161d9cb1ed02fa3caf
4
- data.tar.gz: 3f2ce5fac2641daf1b82d38df2b0e682aec543bf14a8f485c1976acb5bc9a0ca
3
+ metadata.gz: d2d912eb9fcb0485c2139ebe4518ae370ce45125c86d5bb626b3beb056068a7c
4
+ data.tar.gz: c0c9004e1998216defa6efc4a7568ef397920bc35754c43f23a6279be596dbf4
5
5
  SHA512:
6
- metadata.gz: 85bab86083aa0ff67da2ba25e85fab3a8fb2edc7f430e477bdf2bacee7f95f4aa959eabb0cae117d8af0073e507fa446f8da4b121f3d27170c8fe0fb390c9cc7
7
- data.tar.gz: 923ae7cbe0ff1166a5653597e58b18ec38bf42cd2ce0f6644553c1f27598d13670c7fce0b5a5008cf7053590756a5d39900e4fcef5ed2525e18d2719dc9008ae
6
+ metadata.gz: 65d5efbd8f5b7e2e9da027954375920164b2e0b246b95edeb3b381a139b30d4e76570f77b0fa43c9c45dd55542fabacbd5bd996beb832a8c344fcca0b2740606
7
+ data.tar.gz: 31b97ec5a249b43d8419debf89cb59e48fa6c4786684be18eae8d61b9f8c6d4bd5e4dffe1af2617ce22b20d4aae202edff0acbf4110598627f418b764dabbb45
data/lib/vv/cli.rb CHANGED
@@ -79,15 +79,58 @@ module VV
79
79
  absolute_silence ][index]
80
80
  end
81
81
 
82
+ def await_input message: nil,
83
+ force_match: false,
84
+ input: nil
85
+
86
+ input = input
87
+ input ||= Readline.prompt message
88
+
89
+ @prompt_message = message.unstyled
90
+ @prompt_message ||= ""
91
+
92
+ response = self.parse_message! input: input
93
+
94
+ @prompt_message = nil
95
+
96
+ response
97
+ end
98
+
99
+ def parse_message! input:
100
+ @prompt_table = LookupTable.new ignore_case: true
101
+ options = @prompt_message.split(String.colon).last
102
+ tokens = options.split_english
103
+
104
+ tokens.each do |_token|
105
+ token = _token.gsub("(","").gsub(")","")
106
+ alias_token = _token.split("(")[-1].split(")")[0]
107
+ @prompt_table[token] = true
108
+ @prompt_table.alias key: alias_token, to: token
109
+ end
110
+
111
+ response = @prompt_table.lookup_key input
112
+
113
+ response || input
114
+ end
115
+
82
116
  def help?
83
117
  return false if @settings.nil?
84
118
  @settings["-h"]
85
119
  end
86
120
 
121
+ def version?
122
+ return false if @settings.nil?
123
+ @settings["-V"]
124
+ end
125
+
87
126
  def print_help width: 80
88
127
  option_router.help_doc.cli_print width: width
89
128
  end
90
129
 
130
+ def print_version width: 80
131
+ "#{self.name} version #{@version}".cli_puts
132
+ end
133
+
91
134
  end
92
135
 
93
136
  class OptionRouter
@@ -47,4 +47,11 @@ class Object
47
47
  fail message
48
48
  end
49
49
 
50
+ def is_a! klass
51
+ message = \
52
+ "Expected `#{klass}` instance, not `#{self.class}`."
53
+ fail ArgumentError, message unless self.is_a? klass
54
+ end
55
+ alias_method :must_be_a!, :is_a!
56
+
50
57
  end
@@ -18,6 +18,7 @@ module Readline
18
18
  module_function :safe_readline
19
19
 
20
20
  def prompt message, exit_on_exit: true
21
+ message.is_a! String
21
22
  message.concat " "
22
23
 
23
24
  input = Readline.safe_readline message
@@ -130,6 +130,45 @@ module VV
130
130
  self.with_ending newline
131
131
  end
132
132
 
133
+ def split_via *arguments, ignore_newlines: false
134
+ newlines_encountered = self.include? String.newline
135
+ newlines_ok = ( not newlines_encountered )
136
+ newlines_ok ||= ignore_newlines
137
+
138
+ message = "Newlines encountered, but disallowed. "
139
+ message += \
140
+ "Set `ignore_newlines` to true to treat as spaces."
141
+
142
+ fail message unless newlines_ok
143
+
144
+ args = arguments.flatten.sort_by(&:length).reverse
145
+
146
+ response = [self.gsub(String.newline, String.space)]
147
+
148
+ args.map do |arg|
149
+
150
+ response.map! do |fragment|
151
+ fragment.split arg
152
+ end
153
+
154
+ response.flatten!
155
+ end
156
+
157
+ response
158
+ end
159
+
160
+ def split_english ignore_newlines: false
161
+
162
+ options = [ ", ",
163
+ ", and ",
164
+ ", or ",
165
+ " and ",
166
+ " or "]
167
+ self.split_via(options,
168
+ ignore_newlines: ignore_newlines)
169
+ .map(&:strip)
170
+ end
171
+
133
172
  def squish!
134
173
  self.gsub!(/\A[[:space:]]+/, "")
135
174
  self.gsub!(/[[:space:]]+\z/, "")
@@ -465,6 +504,10 @@ module VV
465
504
  self[9]
466
505
  end
467
506
 
507
+ def cli_puts **kwargs
508
+ puts String.get_stdout { self.cli_print( **kwargs ) }
509
+ end
510
+
468
511
  def cli_print width: 80,
469
512
  padding: 0,
470
513
  position: 0,
@@ -1,14 +1,26 @@
1
1
  class LookupTable
2
2
 
3
- attr_reader :canonnicals, :aliases, :data
3
+ attr_reader :canonnicals,
4
+ :aliases,
5
+ :data,
6
+ :ignore_case
4
7
 
5
- def initialize
8
+ def initialize ignore_case: false
9
+ @ignore_case = ignore_case
6
10
  @canonicals = Hash.new
7
11
  @aliases = Hash.new
8
12
  @data = Hash.new
9
13
  end
10
14
 
11
15
  def alias( key:, to: )
16
+
17
+ if @ignore_case
18
+ key = key.downcase
19
+ to = to.downcase
20
+ end
21
+
22
+ return if key == to
23
+
12
24
  _ensure_alias_possible key
13
25
 
14
26
  @canonicals[key] = to
@@ -18,14 +30,17 @@ class LookupTable
18
30
  end
19
31
 
20
32
  def []= key, value
33
+ key = key.downcase if @ignore_case
21
34
  @data[ self.canonical key ] = value
22
35
  end
23
36
 
24
37
  def [] key
38
+ key = key.downcase if @ignore_case
25
39
  @data[ self.canonical key ]
26
40
  end
27
41
 
28
42
  def canonical key
43
+ key = key.downcase if @ignore_case
29
44
  @canonicals[key] || key
30
45
  end
31
46
 
@@ -34,6 +49,7 @@ class LookupTable
34
49
  end
35
50
 
36
51
  def include? key
52
+ key = key.downcase if @ignore_case
37
53
  @data.include?( self.canonical key )
38
54
  end
39
55
 
@@ -48,30 +64,32 @@ class LookupTable
48
64
  }
49
65
  end
50
66
 
51
- # Make Object class method called `delegate_missing`?
52
- def method_missing(method, *args, **kwargs, &block)
53
- super
54
- rescue NoMethodError
55
- begin
56
- if args.size > 0 && kwargs.size > 0
57
- return self.to_h.public_send method, *args, **kwargs, &block
58
- elsif args.size > 0
59
- return self.to_h.public_send method, *args, &block
60
- elsif kwargs.size > 0
61
- return self.to_h.public_send method, **kwargs, &block
62
- else
63
- return self.to_h.public_send method, &block
64
- end
65
- rescue NoMethodError
66
- end
67
- raise
67
+ # Because case is ignorable we can't automatically
68
+ # delegate all methods, only those without arguments
69
+ def keys *args, **kwargs, &block
70
+ both = args.present? && kwargs.present?
71
+
72
+ return to_h.keys(*args, **kwargs, &block) if both
73
+ return to_h.keys(**kwargs, &block) if kwargs.present?
74
+ return to_h.keys(*args, &block) if args.present?
75
+
76
+ to_h.keys( &block )
68
77
  end
69
78
 
70
79
  def lookup_canonical key
80
+ key = key.downcase if @ignore_case
81
+
71
82
  return key if self.canonical_keys.include? key
72
83
 
73
84
  @canonicals[key]
74
85
  end
86
+ alias_method :lookup_key, :lookup_canonical
87
+
88
+ def fuzzy_lookup_canonical key
89
+ raise NotImplementedError
90
+ key = key.downcase if @ignore_case
91
+ Dir.glob pattern
92
+ end
75
93
 
76
94
  protected
77
95
 
data/lib/vv/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module VV
2
- VERSION = '0.0.10'
2
+ VERSION = '0.0.11'
3
3
  end
data/lib/vv.rb CHANGED
@@ -1,8 +1,9 @@
1
- require 'securerandom'
2
- require 'set'
3
- require 'bigdecimal'
4
- require 'bigdecimal/util'
5
- require 'fileutils'
1
+ require "securerandom"
2
+ require "readline"
3
+ require "set"
4
+ require "bigdecimal"
5
+ require "bigdecimal/util"
6
+ require "fileutils"
6
7
 
7
8
  require_relative "vv/gem_methods"
8
9
  require_relative "vv/set_methods"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vv
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Aysan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-02-19 00:00:00.000000000 Z
11
+ date: 2019-02-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake