Inversion::CLI::

Subcommand module

Convenience module for subcommand registration syntax sugar.

Public Class Methods

extended( mod )

Extension callback – register the extending object as a subcommand.

# File lib/inversion/cli.rb, line 238
def self::extended( mod )
        Inversion::CLI.log.debug "Registering subcommands from %p" % [ mod ]
        Inversion::CLI.register_subcommands( mod )
end

Public Instance Methods

display_list( items )

Display the given list of ‘items`.

# File lib/inversion/cli.rb, line 326
def display_list( items )
        items.flatten.each do |item|
                self.prompt.say( "- %s" % [ self.highlight_string(item) ] )
        end

end
display_table( header, rows )

Output a table with the given ‘header` (an array) and `rows` (an array of arrays).

# File lib/inversion/cli.rb, line 301
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 )

Return the specified ‘string` in the ’error’ ANSI color.

# File lib/inversion/cli.rb, line 294
def error_string( string )
        return hl.error( string )
end
exit_now!( message, exit_code=1 )

Exit with the specified ‘exit_code` after printing the given `message`.

# File lib/inversion/cli.rb, line 249
def exit_now!( message, exit_code=1 )
        raise GLI::CustomExit.new( message, exit_code )
end
headline_string( string )

Return the specified ‘string` in the ’headline’ ANSI color.

# File lib/inversion/cli.rb, line 276
def headline_string( string )
        return hl.headline( string )
end
help_now!( message=nil )

Exit with a helpful ‘message` and display the usage.

# File lib/inversion/cli.rb, line 255
def help_now!( message=nil )
        exception = OptionParser::ParseError.new( message )
        def exception.exit_code; 64; end

        raise exception
end
highlight_string( string )

Return the specified ‘string` in the ’highlight’ ANSI color.

# File lib/inversion/cli.rb, line 282
def highlight_string( string )
        return hl.highlight( string )
end
hl()

Return the global Pastel object for convenient formatting, color, etc.

# File lib/inversion/cli.rb, line 270
def hl
        return Inversion::CLI.pastel
end
load_template( tmplpath )

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`.

# File lib/inversion/cli.rb, line 357
def load_template( tmplpath )
        template = Inversion::Template.load( tmplpath )
        return template
rescue Errno => err
        self.prompt.say "Failed to load %s: %s" % [ tmplpath, err.message ]
        return nil
rescue Inversion::ParseError => err
        self.prompt.say "%s: Invalid template: %p: %s" %
                [ tmplpath, err.class, err.message ]
        self.prompt.say( err.backtrace.join("\n  ") ) if $DEBUG
        return nil
end
output_blank_line()

Output a blank line

# File lib/inversion/cli.rb, line 372
def output_blank_line
        self.prompt.say( "\n" )
end
output_subheader( caption )

Output a subheader with the given ‘caption`.

# File lib/inversion/cli.rb, line 387
def output_subheader( caption )
        self.prompt.say( highlight_string caption )
end
output_template_header( template )

Output a header between each template.

# File lib/inversion/cli.rb, line 378
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()

Get the prompt (a TTY::Prompt object)

# File lib/inversion/cli.rb, line 264
def prompt
        return Inversion::CLI.prompt
end
success_string( string )

Return the specified ‘string` in the ’success’ ANSI color.

# File lib/inversion/cli.rb, line 288
def success_string( string )
        return hl.success( string )
end
unless_dry_run( description, return_value=true )
Alias for: unless_dryrun
unless_dryrun( description, return_value=true ) { || ... }

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/inversion/cli.rb, line 343
def unless_dryrun( description, return_value=true )
        if $DRYRUN
                self.log.warn( "DRYRUN> #{description}" )
                return return_value
        else
                return yield
        end
end
Also aliased as: unless_dry_run
visible_chars( string )

Return the count of visible (i.e., non-control) characters in the given ‘string`.

# File lib/inversion/cli.rb, line 335
def visible_chars( string )
        return string.to_s.gsub(/\e\[.*?m/, '').scan( /\P{Cntrl}/ ).size
end