shellopts 2.0.10 → 2.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: 812078e64c677a6aff78081e25a04b81be3b3a1fe9496709838e2abb80573b4a
4
- data.tar.gz: 92c14741c409697f543820d656683b9552d4c6c7ce5c29b01a30677a174549c2
3
+ metadata.gz: 485e81c58a890240d2a77e24a5f3a7a291551da968f395f3aaa4f5b2dac3cc9b
4
+ data.tar.gz: 2d6c92ca01ce1207f6311cf9b0e9bab3e555e309142ba6916d589396f215afb8
5
5
  SHA512:
6
- metadata.gz: 3b1ac7453a534a50cf250e489688aa82ad1928220a615b9e4f7076c27b588d19a7b355e866f3e3cc789fd2259bb2e5de14ac92e905b7b033528c11be26564356
7
- data.tar.gz: d50433ec49a4bf67fd9947d2028ed81be9c8310e7410d93c691e2cd59ed36ef622937c861ef1f4608e7ef5869804cd0f1a4197abf03fd4c2d96cef4985d5dab4
6
+ metadata.gz: 31f3801b98585bed378ff77ae6632d18951ef37a49b3cfde843df9718dc5b46da40e777dff3f7b29e01bf8b32f2ebe0e1e7ab76ddb66744a461acf0061f75b1f
7
+ data.tar.gz: d72bcf1d58e9afdf4f92477af1f2824e01031a46b4cacf5283878ce1045ad91dd930c1c1a15cd588a2552a4ad90fe5ac3add6019b50e099693aa184b7747153f
data/lib/ext/array.rb CHANGED
@@ -41,7 +41,9 @@ module Ext
41
41
 
42
42
  module Wrap
43
43
  refine ::Array do
44
- # Concatenate strings into lines that are at most +width+ characters wide
44
+ # Concatenate array of words into lines that are at most +width+
45
+ # characters wide. +curr+ is the initial number of characters already
46
+ # used on the first line
45
47
  def wrap(width, curr = 0)
46
48
  lines = [[]]
47
49
  curr -= 1 # Simplifies conditions below
@@ -1,12 +1,5 @@
1
1
  require 'terminfo'
2
2
 
3
- # TODO: Move to ext/indented_io.rb
4
- module IndentedIO
5
- class IndentedIO
6
- def margin() combined_indent.size end
7
- end
8
- end
9
-
10
3
  module ShellOpts
11
4
  module Grammar
12
5
  class Node
@@ -216,7 +209,10 @@ module ShellOpts
216
209
  BRIEF_COL1_MAX_WIDTH = 40
217
210
 
218
211
  # Minimum width of second column in brief option and command lists
219
- BRIEF_COL2_MAX_WIDTH = 50
212
+ BRIEF_COL2_MIN_WIDTH = 30
213
+
214
+ # Maximum width of second column in brief option and command lists
215
+ BRIEF_COL2_MAX_WIDTH = 70
220
216
 
221
217
  # Indent to use in help output
222
218
  HELP_INDENT = 4
@@ -226,29 +222,21 @@ module ShellOpts
226
222
 
227
223
  # Usage string in error messages
228
224
  def self.usage(subject)
229
- subject = Grammar::Command.command(subject)
230
- @command_prefix = subject.ancestors.map { |node| node.name + " " }.join
225
+ command = Grammar::Command.command(subject)
226
+ @command_prefix = command.ancestors.map { |node| node.name + " " }.join
231
227
  setup_indent(1) {
232
228
  print lead = "#{USAGE_STRING}: "
233
- indent(lead.size, ' ', bol: false) { subject.puts_usage }
229
+ indent(lead.size, ' ', bol: false) { command.puts_usage }
234
230
  }
235
231
  end
236
232
 
237
- # # TODO
238
- # def self.usage=(usage_lambda)
239
- # end
240
-
241
233
  # When the user gives a -h option
242
- def self.brief(command)
243
- command = Grammar::Command.command(command)
234
+ def self.brief(subject)
235
+ command = Grammar::Command.command(subject)
244
236
  @command_prefix = command.ancestors.map { |node| node.name + " " }.join
245
237
  setup_indent(BRIEF_INDENT) { command.puts_brief }
246
238
  end
247
239
 
248
- # # TODO
249
- # def self.brief=(brief_lambda)
250
- # end
251
-
252
240
  # When the user gives a --help option
253
241
  def self.help(subject)
254
242
  subject = Grammar::Command.command(subject)
@@ -262,18 +250,6 @@ module ShellOpts
262
250
  obj.is_a?(Grammar::Command) ? obj : obj.__grammar__
263
251
  end
264
252
 
265
- # # TODO
266
- # def self.help_w_lambda(program)
267
- # if @help_lambda
268
- # #
269
- # else
270
- # program = Grammar::Command.command(program)
271
- # setup_indent(HELP_INDENT) { program.puts_descr }
272
- # end
273
- # end
274
- #
275
- # def self.help=(help_lambda) @help_lambda end
276
-
277
253
  def self.puts_columns(widths, fields)
278
254
  l = []
279
255
  first_width, second_width = *widths
@@ -284,40 +260,47 @@ module ShellOpts
284
260
  puts first
285
261
  indent(first_width + BRIEF_COL_SEP, ' ') { puts second.wrap(second_width) } if second
286
262
  elsif second
287
- printf "%-#{first_width + BRIEF_COL_SEP}s", first
288
- indent(first_width, bol: false) { puts second.wrap(second_width) }
263
+ indent_size = first_width + BRIEF_COL_SEP
264
+ printf "%-#{indent_size}s", first
265
+ indent(indent_size, ' ', bol: false) { puts second.wrap(second_width) }
289
266
  else
290
267
  puts first
291
268
  end
292
269
  end
293
270
  end
294
271
 
272
+ # Returns a tuple of [first-column-width, second-column-width]. +width+ is
273
+ # the maximum width of the colunms and the BRIEF_COL_SEP separator.
274
+ # +fields+ is an array of [subject-string, descr-text] tuples where the
275
+ # descr is an array of words
295
276
  def self.compute_columns(width, fields)
296
- first_max = [
297
- (fields.map { |first, _| first.size } + [BRIEF_COL1_MIN_WIDTH]).max,
298
- BRIEF_COL1_MAX_WIDTH
299
- ].min
300
- second_max = fields.map { |_, second| second ? second&.map(&:size).sum + second.size - 1: 0 }.max
301
-
302
- if first_max + BRIEF_COL_SEP + second_max <= width
303
- first_width = first_max
304
- second_width = second_max
305
- elsif first_max + BRIEF_COL_SEP + BRIEF_COL2_MAX_WIDTH <= width
306
- first_width = first_max
307
- second_width = width - first_width - BRIEF_COL_SEP
277
+ first_max =
278
+ fields.map { |first, _| first.size }.select { |size| size <= BRIEF_COL1_MAX_WIDTH }.max ||
279
+ BRIEF_COL1_MIN_WIDTH
280
+ second_max = fields.map { |_, second| second ? second&.map(&:size).sum + second.size - 1 : 0 }.max
281
+ first_width = [[first_max, BRIEF_COL1_MIN_WIDTH].max, BRIEF_COL1_MAX_WIDTH].min
282
+ rest = width - first_width - BRIEF_COL_SEP
283
+ second_min = [BRIEF_COL2_MIN_WIDTH, second_max].min
284
+ if rest < second_min
285
+ first_width = [first_max, width - second_min - BRIEF_COL_SEP].max
286
+ second_width = [width - first_width - BRIEF_COL_SEP, BRIEF_COL2_MIN_WIDTH].max
308
287
  else
309
- first_width = [width - BRIEF_COL_SEP - BRIEF_COL2_MAX_WIDTH, BRIEF_COL1_MAX_WIDTH].min
310
- second_width = BRIEF_COL2_MAX_WIDTH
288
+ second_width = [[rest, BRIEF_COL2_MIN_WIDTH].max, BRIEF_COL2_MAX_WIDTH].min
311
289
  end
312
-
313
290
  [first_width, second_width]
314
291
  end
315
292
 
316
293
  def self.width()
317
294
  @width ||= TermInfo.screen_width - MARGIN_RIGHT
295
+ @width
296
+ end
297
+
298
+ # Used in rspec
299
+ def self.width=(width)
300
+ @width = width
318
301
  end
319
302
 
320
- def self.rest() width - $stdout.margin end
303
+ def self.rest() width - $stdout.tab end
321
304
 
322
305
  private
323
306
  # TODO Get rid of?
@@ -1,3 +1,3 @@
1
1
  module ShellOpts
2
- VERSION = "2.0.10"
2
+ VERSION = "2.0.11"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shellopts
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.10
4
+ version: 2.0.11
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-08 00:00:00.000000000 Z
11
+ date: 2022-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forward_to