toys-core 0.3.5 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -112,7 +112,7 @@ module Toys
112
112
  #
113
113
  # @param [Hash] opts New default options to set
114
114
  #
115
- def config_defaults(opts = {})
115
+ def configure_defaults(opts = {})
116
116
  @default_opts.add(opts)
117
117
  self
118
118
  end
@@ -107,6 +107,8 @@ module Toys
107
107
  # display all subcommands recursively. Defaults to false.
108
108
  # @param [String,nil] search An optional string to search for when
109
109
  # listing subcommands. Defaults to `nil` which finds all subcommands.
110
+ # @param [Boolean] show_source_path If true, shows the source path
111
+ # section. Defaults to false.
110
112
  # @param [Integer] indent Indent width. Default is {DEFAULT_INDENT}.
111
113
  # @param [Integer] indent2 Second indent width. Default is
112
114
  # {DEFAULT_INDENT}.
@@ -116,12 +118,12 @@ module Toys
116
118
  #
117
119
  # @return [String] A usage string.
118
120
  #
119
- def help_string(recursive: false, search: nil,
121
+ def help_string(recursive: false, search: nil, show_source_path: false,
120
122
  indent: nil, indent2: nil, wrap_width: nil, styled: true)
121
123
  indent ||= DEFAULT_INDENT
122
124
  indent2 ||= DEFAULT_INDENT
123
125
  subtools = find_subtools(recursive, search)
124
- assembler = HelpStringAssembler.new(@tool, @binary_name, subtools, search,
126
+ assembler = HelpStringAssembler.new(@tool, @binary_name, subtools, search, show_source_path,
125
127
  indent, indent2, wrap_width, styled)
126
128
  assembler.result
127
129
  end
@@ -199,9 +201,11 @@ module Toys
199
201
  end
200
202
 
201
203
  def add_flag(flag)
202
- flags_str = (flag.single_flag_syntax + flag.double_flag_syntax)
203
- .map(&:str_without_value).join(", ")
204
- flags_str << flag.value_delim << flag.value_label if flag.value_label
204
+ flags = flag.single_flag_syntax + flag.double_flag_syntax
205
+ last_index = flags.size - 1
206
+ flags_str = flags.each_with_index.map do |fs, i|
207
+ i == last_index ? fs.canonical_str : fs.str_without_value
208
+ end.join(", ")
205
209
  flags_str = " #{flags_str}" if flag.single_flag_syntax.empty?
206
210
  add_right_column_desc(flags_str, wrap_desc(flag.desc))
207
211
  end
@@ -269,12 +273,13 @@ module Toys
269
273
 
270
274
  ## @private
271
275
  class HelpStringAssembler
272
- def initialize(tool, binary_name, subtools, search_term,
276
+ def initialize(tool, binary_name, subtools, search_term, show_source_path,
273
277
  indent, indent2, wrap_width, styled)
274
278
  @tool = tool
275
279
  @binary_name = binary_name
276
280
  @subtools = subtools
277
281
  @search_term = search_term
282
+ @show_source_path = show_source_path
278
283
  @indent = indent
279
284
  @indent2 = indent2
280
285
  @wrap_width = wrap_width
@@ -359,7 +364,7 @@ module Toys
359
364
  end
360
365
 
361
366
  def add_source_section
362
- return unless @tool.definition_path
367
+ return unless @tool.definition_path && @show_source_path
363
368
  @lines << ""
364
369
  @lines << bold("SOURCE")
365
370
  @lines << indent_str("Defined in #{@tool.definition_path}")
@@ -387,15 +392,16 @@ module Toys
387
392
  end
388
393
 
389
394
  def flag_spec_string(flag)
390
- single_flags = flag.single_flag_syntax.map do |fs|
395
+ flag.flag_syntax.map do |fs|
391
396
  str = bold(fs.str_without_value)
392
- flag.value_label ? "#{str} #{underline(flag.value_label)}" : str
393
- end
394
- double_flags = flag.double_flag_syntax.map do |fs|
395
- str = bold(fs.str_without_value)
396
- flag.value_label ? "#{str}#{flag.value_delim}#{underline(flag.value_label)}" : str
397
- end
398
- (single_flags + double_flags).join(", ")
397
+ if fs.flag_type != :value
398
+ str
399
+ elsif fs.value_type == :optional
400
+ "#{str}#{fs.value_delim}[#{underline(fs.value_label)}]"
401
+ else
402
+ "#{str}#{fs.value_delim}#{underline(fs.value_label)}"
403
+ end
404
+ end.join(", ")
399
405
  end
400
406
 
401
407
  def add_positional_arguments_section
@@ -49,6 +49,7 @@ module Toys
49
49
  else
50
50
  styled ? true : false
51
51
  end
52
+ @buffer = ""
52
53
  end
53
54
 
54
55
  ##
@@ -61,7 +62,7 @@ module Toys
61
62
  # Whether output is styled
62
63
  # @return [Boolean]
63
64
  #
64
- attr_reader :styled
65
+ attr_accessor :styled
65
66
 
66
67
  ##
67
68
  # If the sink is a Logger, the level to log, otherwise `nil`.
@@ -76,29 +77,45 @@ module Toys
76
77
  # @param [Symbol...] styles Styles to apply to the entire line.
77
78
  #
78
79
  def puts(str = "", *styles)
79
- if styled
80
- str = color(str, *styles) unless styles.empty?
81
- else
82
- str = ::HighLine.uncolor(str)
83
- end
80
+ str = @buffer + apply_styles(str, styles)
81
+ @buffer = ""
84
82
  case sink
85
83
  when ::Logger
86
84
  sink.log(log_level, str)
87
85
  when ::IO
88
86
  sink.puts(str)
87
+ sink.flush
89
88
  end
90
89
  self
91
90
  end
92
91
 
93
92
  ##
94
- # Apply the given styles to a string
93
+ # Write a newline and flush the current line.
94
+ #
95
+ def newline
96
+ puts
97
+ end
98
+
99
+ ##
100
+ # Buffer a partial line but do not write it out yet because the line
101
+ # may not yet be complete.
95
102
  #
96
- # @param [String] str The string
97
- # @param [Symbol...] styles Styles to apply to the string.
98
- # @return [String] The string with styles applied.
103
+ # @param [String] str The line to write
104
+ # @param [Symbol...] styles Styles to apply to the partial line.
99
105
  #
100
- def color(str, *styles)
101
- ::HighLine.color(str, *styles)
106
+ def write(str = "", *styles)
107
+ @buffer << apply_styles(str, styles)
108
+ self
109
+ end
110
+
111
+ private
112
+
113
+ def apply_styles(str, styles)
114
+ if styled
115
+ styles.empty? ? str : ::HighLine.color(str, *styles)
116
+ else
117
+ ::HighLine.uncolor(str)
118
+ end
102
119
  end
103
120
  end
104
121
  end
@@ -135,6 +135,29 @@ module Toys
135
135
  result = [] if result.all?(&:empty?)
136
136
  result
137
137
  end
138
+
139
+ ##
140
+ # Make the given object a WrappableString.
141
+ # If the object is already a WrappableString, return it. Otherwise,
142
+ # treat it as a string or an array of strings and wrap it in a
143
+ # WrappableString.
144
+ #
145
+ # @param [Toys::Utils::WrappableString,String,Array<String>] obj
146
+ # @return [Toys::Utils::WrappableString]
147
+ #
148
+ def self.make(obj)
149
+ obj.is_a?(Utils::WrappableString) ? obj : Utils::WrappableString.new(obj)
150
+ end
151
+
152
+ ##
153
+ # Make the given object an array of WrappableString.
154
+ #
155
+ # @param [Array<Toys::Utils::WrappableString,String,Array<String>>] objs
156
+ # @return [Array<Toys::Utils::WrappableString>]
157
+ #
158
+ def self.make_array(objs)
159
+ Array(objs).map { |obj| make(obj) }
160
+ end
138
161
  end
139
162
  end
140
163
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toys-core
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.5
4
+ version: 0.3.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Azuma
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-05-15 00:00:00.000000000 Z
11
+ date: 2018-05-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: highline
@@ -126,7 +126,7 @@ files:
126
126
  - lib/toys/middleware/handle_usage_errors.rb
127
127
  - lib/toys/middleware/set_default_descriptions.rb
128
128
  - lib/toys/middleware/show_help.rb
129
- - lib/toys/middleware/show_version.rb
129
+ - lib/toys/middleware/show_root_version.rb
130
130
  - lib/toys/template.rb
131
131
  - lib/toys/templates.rb
132
132
  - lib/toys/templates/clean.rb
@@ -135,6 +135,9 @@ files:
135
135
  - lib/toys/templates/rubocop.rb
136
136
  - lib/toys/templates/yardoc.rb
137
137
  - lib/toys/tool.rb
138
+ - lib/toys/tool/acceptor.rb
139
+ - lib/toys/tool/arg_definition.rb
140
+ - lib/toys/tool/flag_definition.rb
138
141
  - lib/toys/utils/exec.rb
139
142
  - lib/toys/utils/help_text.rb
140
143
  - lib/toys/utils/line_output.rb