Class: ShellOpts::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/shellopts/formatter.rb

Constant Summary collapse

MARGIN_RIGHT =

Right margin

3
USAGE_STRING =

String for ‘Usage’ in error messages

"Usage"
USAGE_INDENT =

Indent to use in usage output

USAGE_STRING.size
USAGE_MAX_WIDTH =

Width of usage (after usage string)

70
BRIEF_INDENT =

Indent to use in brief output

2
BRIEF_COL_SEP =

Number of characters between columns in brief output

2
BRIEF_COL1_MIN_WIDTH =

Maximum width of first column in brief option and command lists

6
BRIEF_COL1_MAX_WIDTH =

Maximum width of first column in brief option and command lists

40
BRIEF_COL2_MAX_WIDTH =

Minimum width of second column in brief option and command lists

50
HELP_INDENT =

Indent to use in help output

4

Class Method Summary collapse

Class Method Details

.brief(command) ⇒ Object

When the user gives a -h option



232
233
234
235
236
# File 'lib/shellopts/formatter.rb', line 232

def self.brief(command)
  command = Grammar::Command.command(command)
  @command_prefix = command.ancestors.map { |node| node.name + " " }.join
  setup_indent(BRIEF_INDENT) { command.puts_brief }
end

.command_of(obj) ⇒ Object

Short-hand to get the Grammar::Command object



250
251
252
253
# File 'lib/shellopts/formatter.rb', line 250

def self.command_of(obj)
  constrain obj, Grammar::Command, ::ShellOpts::Program
  obj.is_a?(Grammar::Command) ? obj : obj.__grammar__
end

.command_prefixObject

Command prefix when subject is a sub-command



215
# File 'lib/shellopts/formatter.rb', line 215

def self.command_prefix() @command_prefix end

.compute_columns(width, fields) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/shellopts/formatter.rb', line 285

def self.compute_columns(width, fields)
  first_max = [
    (fields.map { |first, _| first.size } + [BRIEF_COL1_MIN_WIDTH]).max, 
    BRIEF_COL1_MAX_WIDTH
  ].min
  second_max = fields.map { |_, second| second ? second&.map(&:size).sum + second.size - 1: 0 }.max

  if first_max + BRIEF_COL_SEP + second_max <= width
    first_width = first_max
    second_width = second_max
  elsif first_max + BRIEF_COL_SEP + BRIEF_COL2_MAX_WIDTH <= width
    first_width = first_max
    second_width = width - first_width - BRIEF_COL_SEP
  else
    first_width = [width - BRIEF_COL_SEP - BRIEF_COL2_MAX_WIDTH, BRIEF_COL1_MAX_WIDTH].min
    second_width = BRIEF_COL2_MAX_WIDTH
  end

  [first_width, second_width]
end

.help(subject) ⇒ Object

When the user gives a –help option



243
244
245
246
247
# File 'lib/shellopts/formatter.rb', line 243

def self.help(subject)
  subject = Grammar::Command.command(subject)
  @command_prefix = subject.ancestors.map { |node| node.name + " " }.join
  setup_indent(HELP_INDENT) { subject.puts_help }
end

.puts_columns(widths, fields) ⇒ Object

# TODO

def self.help_w_lambda(program)
  if @help_lambda
    #
  else
    program = Grammar::Command.command(program)
    setup_indent(HELP_INDENT) { program.puts_descr }
  end
end

def self.help=(help_lambda) @help_lambda end


267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/shellopts/formatter.rb', line 267

def self.puts_columns(widths, fields)
  l = []
  first_width, second_width = *widths
  second_col = first_width + 2

  for (first, second) in fields
    if first.size > first_width
      puts first
      indent(first_width + BRIEF_COL_SEP, ' ') { puts second.wrap(second_width) } if second
    elsif second
      printf "%-#{first_width + BRIEF_COL_SEP}s", first
      indent(first_width, bol: false) { puts second.wrap(second_width) }
    else
      puts first
    end
  end
end

.restObject



310
# File 'lib/shellopts/formatter.rb', line 310

def self.rest() width - $stdout.margin end

.usage(subject) ⇒ Object

Usage string in error messages



218
219
220
221
222
223
224
225
# File 'lib/shellopts/formatter.rb', line 218

def self.usage(subject)
  subject = Grammar::Command.command(subject)
  @command_prefix = subject.ancestors.map { |node| node.name + " " }.join
  setup_indent(1) {
    print lead = "#{USAGE_STRING}: "
    indent(lead.size, ' ', bol: false) { subject.puts_usage }
  }
end

.widthObject



306
307
308
# File 'lib/shellopts/formatter.rb', line 306

def self.width()
  @width ||= TermInfo.screen_width - MARGIN_RIGHT
end