Class: GithubCLI::Command::Usage

Inherits:
Object
  • Object
show all
Defined in:
lib/github_cli/command/usage.rb

Constant Summary collapse

DEFAULT_INDENT =
12

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, flags, options = {}) ⇒ Usage

Initializes an usage instance


18
19
20
21
# File 'lib/github_cli/command/usage.rb', line 18

def initialize(command, flags, options={})
  @command = Command.command_to_show(command)
  @flags   = flags
end

Instance Attribute Details

#commandObject (readonly)

Returns command


11
12
13
# File 'lib/github_cli/command/usage.rb', line 11

def command
  @command
end

#flagsObject (readonly)

Returns command flags


14
15
16
# File 'lib/github_cli/command/usage.rb', line 14

def flags
  @flags
end

Instance Method Details

#format_usage(options = {}) ⇒ Object

Options indent - Indent the line with by indent value. Assumes that the first

the first line is already filled in with other padding.

length - Line length, otherwise the default terminal width is assumed.


27
28
29
30
31
32
33
# File 'lib/github_cli/command/usage.rb', line 27

def format_usage(options={})
  synopsis = "#{flags}#{command} <subcommand> [<args>]"
  indent = options[:indent] || DEFAULT_INDENT
  padding = sprintf("%#{indent}s",'')
  length  = options[:length] || Terminal.default_width
  wrap synopsis, length, indent, padding
end

#inspectObject

Returns a concise string representation of Usage instance


61
62
63
# File 'lib/github_cli/command/usage.rb', line 61

def inspect
  "#<#{self.class.inspect} @command=#{command.inspect} @flags=#{flags.inspect}"
end

#tokenize_text(text) ⇒ Object


35
36
37
# File 'lib/github_cli/command/usage.rb', line 35

def tokenize_text(text)
  text.split(/\s+/).map(&:chomp).reject(&:empty?)
end

#wrap(text, length, indent = 0, padding = 0) ⇒ Object

Wraps text at the given line length using given indent value.


41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/github_cli/command/usage.rb', line 41

def wrap(text, length, indent=0, padding=0)
  if text.length > length - indent
    paragraphs = []
    line = ''
    tokenize_text(text).each do |fragment|
      if line.length < length - indent
        line << fragment + ' '
      else
        paragraphs << line
        line = padding + fragment + ' '
      end
    end
    paragraphs << line
    text = paragraphs.join("\n")
  end
  text
end