Class: GithubCLI::Command::Usage
- Inherits:
-
Object
- Object
- GithubCLI::Command::Usage
- Defined in:
- lib/github_cli/command/usage.rb
Constant Summary collapse
- DEFAULT_INDENT =
12
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns command.
-
#flags ⇒ Object
readonly
Returns command flags.
Instance Method Summary collapse
-
#format_usage(options = {}) ⇒ Object
Options indent - Indent the line with by indent value.
-
#initialize(command, flags, options = {}) ⇒ Usage
constructor
Initializes an usage instance.
-
#inspect ⇒ Object
Returns a concise string representation of Usage instance.
- #tokenize_text(text) ⇒ Object
-
#wrap(text, length, indent = 0, padding = 0) ⇒ Object
Wraps text at the given line length using given indent value.
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, ={}) @command = Command.command_to_show(command) @flags = flags end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns command
11 12 13 |
# File 'lib/github_cli/command/usage.rb', line 11 def command @command end |
#flags ⇒ Object (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(={}) synopsis = "#{flags}#{command} <subcommand> [<args>]" indent = [:indent] || DEFAULT_INDENT padding = sprintf("%#{indent}s",'') length = [:length] || Terminal.default_width wrap synopsis, length, indent, padding end |
#inspect ⇒ Object
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 |