Module: Strelka::CLI::Subcommand
Overview
Convenience module for subcommand registration syntax sugar.
Class Method Summary collapse
-
.display_table(rows) ⇒ Object
Output a table with the given
rows. -
.error_string(string) ⇒ Object
Return the specified
stringin the 'error' ANSI color. -
.extended(mod) ⇒ Object
Extension callback -- register the extending object as a subcommand.
-
.headline_string(string) ⇒ Object
Return the specified
stringin the 'headline' ANSI color. -
.highlight_string(string) ⇒ Object
Return the specified
stringin the 'highlight' ANSI color. -
.hl(text) ⇒ Object
Return the specified
textas a Highline::String for convenient formatting, color, etc. -
.prompt ⇒ Object
Get the prompt (a Highline 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_table(rows) ⇒ Object
Output a table with the given rows.
327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/strelka/cli.rb', line 327 def display_table( rows ) colwidths = rows.transpose.map do |colvals| colvals.map {|val| visible_chars(val) }.max end rows.each do |row| row_string = row.zip( colwidths ).inject( '' ) do |accum, (val, colsize)| padding = ' ' * (colsize - visible_chars(val) + 2) accum + val.to_s + padding end Strelka::CLI.prompt.say( row_string + "\n" ) end end |
.error_string(string) ⇒ Object
Return the specified string in the 'error' ANSI color.
321 322 323 |
# File 'lib/strelka/cli.rb', line 321 def error_string( string ) return hl( string ).color( :error ) end |
.extended(mod) ⇒ Object
Extension callback -- register the extending object as a subcommand.
279 280 281 282 |
# File 'lib/strelka/cli.rb', line 279 def self::extended( mod ) Strelka::CLI.log.debug "Registering subcommands from %p" % [ mod ] Strelka::CLI.register_subcommands( mod ) end |
.headline_string(string) ⇒ Object
Return the specified string in the 'headline' ANSI color.
303 304 305 |
# File 'lib/strelka/cli.rb', line 303 def headline_string( string ) return hl( string ).color( :headline ) end |
.highlight_string(string) ⇒ Object
Return the specified string in the 'highlight' ANSI color.
309 310 311 |
# File 'lib/strelka/cli.rb', line 309 def highlight_string( string ) return hl( string ).color( :highlight ) end |
.hl(text) ⇒ Object
Return the specified text as a Highline::String for convenient formatting,
color, etc.
297 298 299 |
# File 'lib/strelka/cli.rb', line 297 def hl( text ) return HighLine::String.new( text.to_s ) end |
.prompt ⇒ Object
Get the prompt (a Highline object)
290 291 292 |
# File 'lib/strelka/cli.rb', line 290 def prompt return Strelka::CLI.prompt end |
.success_string(string) ⇒ Object
Return the specified string in the 'success' ANSI color.
315 316 317 |
# File 'lib/strelka/cli.rb', line 315 def success_string( string ) return hl( string ).color( :success ) 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.
352 353 354 355 356 357 358 359 |
# File 'lib/strelka/cli.rb', line 352 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.
344 345 346 |
# File 'lib/strelka/cli.rb', line 344 def visible_chars( string ) return string.to_s.gsub(/\e\[.*?m/, '').scan( /\P{Cntrl}/ ).size end |