module Arborist::CLI::Subcommand

Convenience module for subcommand registration syntax sugar.

Public Class Methods

extended( mod ) click to toggle source

Extension callback – register the extending object as a subcommand.

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

Public Instance Methods

display_table( header, rows ) click to toggle source

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

# File lib/arborist/cli.rb, line 342
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 ) click to toggle source

Return the specified string in the 'error' ANSI color.

# File lib/arborist/cli.rb, line 335
def error_string( string )
        return hl.error( string )
end
exit_now!( message, exit_code=1 ) click to toggle source

Exit with the specified exit_code after printing the given message.

# File lib/arborist/cli.rb, line 290
def exit_now!( message, exit_code=1 )
        raise GLI::CustomExit.new( message, exit_code )
end
headline_string( string ) click to toggle source

Return the specified string in the 'headline' ANSI color.

# File lib/arborist/cli.rb, line 317
def headline_string( string )
        return hl.headline( string )
end
help_now!( message=nil ) click to toggle source

Exit with a helpful message and display the usage.

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

        raise exception
end
highlight_string( string ) click to toggle source

Return the specified string in the 'highlight' ANSI color.

# File lib/arborist/cli.rb, line 323
def highlight_string( string )
        return hl.highlight( string )
end
hl() click to toggle source

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

# File lib/arborist/cli.rb, line 311
def hl
        return Arborist::CLI.pastel
end
prompt() click to toggle source

Get the prompt (a TTY::Prompt object)

# File lib/arborist/cli.rb, line 305
def prompt
        return Arborist::CLI.prompt
end
skips_around() click to toggle source

Use this if the following command should not have the around block executed. By default, the around block is executed, but for commands that might not want the setup to happen, this can be handy

# File lib/arborist/cli.rb, line 280
def skips_around
        @skips_around = true
end
skips_post() click to toggle source

Use this if the following command should not have the post block executed. By default, the post block is executed after each command. Using this will avoid that behavior for the following command

# File lib/arborist/cli.rb, line 272
def skips_post
        @skips_post = true
end
skips_pre() click to toggle source

Use this if the following command should not have the pre block executed. By default, the pre block is executed before each command and can result in aborting the call. Using this will avoid that behavior for the following command

# File lib/arborist/cli.rb, line 264
def skips_pre
        @skips_pre = true
end
success_string( string ) click to toggle source

Return the specified string in the 'success' ANSI color.

# File lib/arborist/cli.rb, line 329
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 ) { || ... } click to toggle source

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/arborist/cli.rb, line 375
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 ) click to toggle source

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

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