Convenience module for subcommand registration syntax sugar.
Extension callback – register the extending object as a subcommand.
# File lib/strelka/cli.rb, line 277
def self::extended( mod )
Strelka::CLI.log.debug "Registering subcommands from %p" % [ mod ]
Strelka::CLI.register_subcommands( mod )
end
Output a table with the given rows
.
# File lib/strelka/cli.rb, line 325
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
Return the specified string
in the 'error' ANSI color.
# File lib/strelka/cli.rb, line 319
def error_string( string )
return hl( string ).color( :error )
end
Return the specified string
in the 'headline' ANSI
color.
# File lib/strelka/cli.rb, line 301
def headline_string( string )
return hl( string ).color( :headline )
end
Return the specified string
in the 'highlight' ANSI
color.
# File lib/strelka/cli.rb, line 307
def highlight_string( string )
return hl( string ).color( :highlight )
end
Return the specified text
as a Highline::String for convenient
formatting, color, etc.
# File lib/strelka/cli.rb, line 295
def hl( text )
return HighLine::String.new( text.to_s )
end
Get the prompt (a Highline object)
# File lib/strelka/cli.rb, line 288
def prompt
return Strelka::CLI.prompt
end
Return the specified string
in the 'success' ANSI
color.
# File lib/strelka/cli.rb, line 313
def success_string( string )
return hl( string ).color( :success )
end
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.
# File lib/strelka/cli.rb, line 350
def unless_dryrun( description, return_value=true )
if $DRYRUN
self.log.warn( "DRYRUN> #{description}" )
return return_value
else
return yield
end
end
Return the count of visible (i.e., non-control) characters in the given
string
.
# File lib/strelka/cli.rb, line 342
def visible_chars( string )
return string.to_s.gsub(/\e\[.*?m/, '').scan( /\P{Cntrl}/ ).size
end