Module: Inversion::CLI::Subcommand
- Included in:
- ApiCommand, TagTokensCommand, TreeCommand
- Defined in:
- lib/inversion/cli.rb
Overview
Convenience module for subcommand registration syntax sugar.
Class Method Summary collapse
-
.display_list(items) ⇒ Object
Display the given list of
items. -
.display_table(header, rows) ⇒ Object
Output a table with the given
header(an array) androws(an array of arrays). -
.error_string(string) ⇒ Object
Return the specified
stringin the ‘error’ ANSI color. -
.exit_now!(message, exit_code = 1) ⇒ Object
Exit with the specified
exit_codeafter printing the givenmessage. -
.extended(mod) ⇒ Object
Extension callback – register the extending object as a subcommand.
-
.headline_string(string) ⇒ Object
Return the specified
stringin the ‘headline’ ANSI color. -
.help_now!(message = nil) ⇒ Object
Exit with a helpful
messageand display the usage. -
.highlight_string(string) ⇒ Object
Return the specified
stringin the ‘highlight’ ANSI color. -
.hl ⇒ Object
Return the global Pastel object for convenient formatting, color, etc.
-
.load_template(tmplpath) ⇒ Object
Load the Inversion::Template from the specified
tmplpathand return it. -
.output_blank_line ⇒ Object
Output a blank line.
-
.output_subheader(caption) ⇒ Object
Output a subheader with the given
caption. -
.output_template_header(template) ⇒ Object
Output a header between each template.
-
.prompt ⇒ Object
Get the prompt (a TTY::Prompt object).
-
.success_string(string) ⇒ Object
Return the specified
stringin the ‘success’ ANSI color. -
.unless_dryrun(description, return_value = true) ⇒ Object
(also: unless_dry_run)
In dry-run mode, output the description instead of running the provided block and return the
return_value. -
.visible_chars(string) ⇒ Object
Return the count of visible (i.e., non-control) characters in the given
string.
Class Method Details
.display_list(items) ⇒ Object
Display the given list of items.
327 328 329 330 331 332 |
# File 'lib/inversion/cli.rb', line 327 def display_list( items ) items.flatten.each do |item| self.prompt.say( "- %s" % [ self.highlight_string(item) ] ) end end |
.display_table(header, rows) ⇒ Object
Output a table with the given header (an array) and rows (an array of arrays).
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/inversion/cli.rb', line 302 def display_table( header, rows ) table = TTY::Table.new( header, rows ) renderer = nil if hl.enabled? renderer = TTY::Table::Renderer::Unicode.new( table, multiline: true, padding: [0,1,0,1] ) renderer.border.style = :dim else renderer = TTY::Table::Renderer::ASCII.new( table, multiline: true, padding: [0,1,0,1] ) end puts renderer.render end |
.error_string(string) ⇒ Object
Return the specified string in the ‘error’ ANSI color.
295 296 297 |
# File 'lib/inversion/cli.rb', line 295 def error_string( string ) return hl.error( string ) end |
.exit_now!(message, exit_code = 1) ⇒ Object
Exit with the specified exit_code after printing the given message.
250 251 252 |
# File 'lib/inversion/cli.rb', line 250 def exit_now!( , exit_code=1 ) raise GLI::CustomExit.new( , exit_code ) end |
.extended(mod) ⇒ Object
Extension callback – register the extending object as a subcommand.
239 240 241 242 |
# File 'lib/inversion/cli.rb', line 239 def self::extended( mod ) Inversion::CLI.log.debug "Registering subcommands from %p" % [ mod ] Inversion::CLI.register_subcommands( mod ) end |
.headline_string(string) ⇒ Object
Return the specified string in the ‘headline’ ANSI color.
277 278 279 |
# File 'lib/inversion/cli.rb', line 277 def headline_string( string ) return hl.headline( string ) end |
.help_now!(message = nil) ⇒ Object
Exit with a helpful message and display the usage.
256 257 258 259 260 261 |
# File 'lib/inversion/cli.rb', line 256 def help_now!( =nil ) exception = OptionParser::ParseError.new( ) def exception.exit_code; 64; end raise exception end |
.highlight_string(string) ⇒ Object
Return the specified string in the ‘highlight’ ANSI color.
283 284 285 |
# File 'lib/inversion/cli.rb', line 283 def highlight_string( string ) return hl.highlight( string ) end |
.hl ⇒ Object
Return the global Pastel object for convenient formatting, color, etc.
271 272 273 |
# File 'lib/inversion/cli.rb', line 271 def hl return Inversion::CLI.pastel end |
.load_template(tmplpath) ⇒ Object
Load the Inversion::Template from the specified tmplpath and return it. If there is an error loading the template, output the error and return nil.
358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'lib/inversion/cli.rb', line 358 def load_template( tmplpath ) template = Inversion::Template.load( tmplpath ) return template rescue Errno => err self.prompt.say "Failed to load %s: %s" % [ tmplpath, err. ] return nil rescue Inversion::ParseError => err self.prompt.say "%s: Invalid template: %p: %s" % [ tmplpath, err.class, err. ] self.prompt.say( err.backtrace.join("\n ") ) if $DEBUG return nil end |
.output_blank_line ⇒ Object
Output a blank line
373 374 375 |
# File 'lib/inversion/cli.rb', line 373 def output_blank_line self.prompt.say( "\n" ) end |
.output_subheader(caption) ⇒ Object
Output a subheader with the given caption.
388 389 390 |
# File 'lib/inversion/cli.rb', line 388 def output_subheader( caption ) self.prompt.say( highlight_string caption ) end |
.output_template_header(template) ⇒ Object
Output a header between each template.
379 380 381 382 383 384 |
# File 'lib/inversion/cli.rb', line 379 def output_template_header( template ) header_info = "%s (%0.2fK, %s)" % [ template.source_file, template.source.bytesize/1024.0, template.source.encoding ] header_line = "-- %s" % [ header_info ] self.prompt.say( headline_string header_line ) end |
.prompt ⇒ Object
Get the prompt (a TTY::Prompt object)
265 266 267 |
# File 'lib/inversion/cli.rb', line 265 def prompt return Inversion::CLI.prompt end |
.success_string(string) ⇒ Object
Return the specified string in the ‘success’ ANSI color.
289 290 291 |
# File 'lib/inversion/cli.rb', line 289 def success_string( string ) return hl.success( string ) end |
.unless_dryrun(description, return_value = true) ⇒ Object Also known as: unless_dry_run
In dry-run mode, output the description instead of running the provided block and return the return_value. If dry-run mode is not enabled, yield to the block.
344 345 346 347 348 349 350 351 |
# File 'lib/inversion/cli.rb', line 344 def unless_dryrun( description, return_value=true ) if $DRYRUN self.log.warn( "DRYRUN> #{description}" ) return return_value else return yield end end |
.visible_chars(string) ⇒ Object
Return the count of visible (i.e., non-control) characters in the given string.
336 337 338 |
# File 'lib/inversion/cli.rb', line 336 def visible_chars( string ) return string.to_s.gsub(/\e\[.*?m/, '').scan( /\P{Cntrl}/ ).size end |