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 =

Minimum width of first column in brief option and command lists

20
BRIEF_COL1_MAX_WIDTH =

Maximum width of first column in brief option and command lists

40
BRIEF_COL2_MIN_WIDTH =

Minimum width of second column in brief option and command lists

30
BRIEF_COL2_MAX_WIDTH =

Maximum width of second column in brief option and command lists

70
HELP_INDENT =

Indent to use in help output

4
HELP_MAX_WIDTH =

Max. width of help text (not including indent)

85

Class Method Summary collapse

Class Method Details

.brief(subject) ⇒ Object

When the user gives a -h option



239
240
241
242
243
# File 'lib/shellopts/formatter.rb', line 239

def self.brief(subject)
  command = Grammar::Command.command(subject)
  @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



253
254
255
256
# File 'lib/shellopts/formatter.rb', line 253

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



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

def self.command_prefix() @command_prefix end

.compute_columns(width, fields) ⇒ Object

Returns a tuple of [first-column-width, second-column-width]. width is the maximum width of the colunms and the BRIEF_COL_SEP separator. fields is an array of [subject-string, descr-text] tuples where the descr is an array of words



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/shellopts/formatter.rb', line 281

def self.compute_columns(width, fields)
  first_max =
      fields.map { |first, _| first.size }.select { |size| size <= BRIEF_COL1_MAX_WIDTH }.max ||
      BRIEF_COL1_MIN_WIDTH
  second_max = fields.map { |_, second| second ? second&.map(&:size).sum + second.size - 1 : 0 }.max
  first_width = [[first_max, BRIEF_COL1_MIN_WIDTH].max, BRIEF_COL1_MAX_WIDTH].min
  rest = width - first_width - BRIEF_COL_SEP
  second_min = [BRIEF_COL2_MIN_WIDTH, second_max].min
  if rest < second_min
    first_width = [first_max, width - second_min - BRIEF_COL_SEP].max
    second_width = [width - first_width - BRIEF_COL_SEP, BRIEF_COL2_MIN_WIDTH].max
  else
    second_width = [[rest, BRIEF_COL2_MIN_WIDTH].max, BRIEF_COL2_MAX_WIDTH].min
  end
  [first_width, second_width]
end

.help(subject) ⇒ Object

When the user gives a –help option



246
247
248
249
250
# File 'lib/shellopts/formatter.rb', line 246

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



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/shellopts/formatter.rb', line 258

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
      indent_size = first_width + BRIEF_COL_SEP
      printf "%-#{indent_size}s", first
      indent(indent_size, ' ', bol: false) { puts second.wrap(second_width) }
    else
      puts first
    end
  end
end

.restObject



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

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

.usage(subject) ⇒ Object

Usage string in error messages



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

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

.widthObject



298
299
300
301
# File 'lib/shellopts/formatter.rb', line 298

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

.width=(width) ⇒ Object

Used in rspec



304
305
306
# File 'lib/shellopts/formatter.rb', line 304

def self.width=(width)
  @width = width
end