Module: Mongrel2::CLI::Subcommand

Overview

Convenience module for subcommand registration syntax sugar.

Class Method Summary collapse

Class Method Details

.display_table(header, rows) ⇒ Object

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



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'lib/mongrel2/cli.rb', line 334

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.



327
328
329
# File 'lib/mongrel2/cli.rb', line 327

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.

Raises:

  • (GLI::CustomExit)


282
283
284
# File 'lib/mongrel2/cli.rb', line 282

def exit_now!( message, exit_code=1 )
	raise GLI::CustomExit.new( message, exit_code )
end

.extended(mod) ⇒ Object

Extension callback – register the extending object as a subcommand.



271
272
273
274
# File 'lib/mongrel2/cli.rb', line 271

def self::extended( mod )
	Mongrel2::CLI.log.debug "Registering subcommands from %p" % [ mod ]
	Mongrel2::CLI.register_subcommands( mod )
end

.headline_string(string) ⇒ Object

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



309
310
311
# File 'lib/mongrel2/cli.rb', line 309

def headline_string( string )
	return hl.headline( string )
end

.help_now!(message = nil) ⇒ Object

Exit with a helpful message and display the usage.



288
289
290
291
292
293
# File 'lib/mongrel2/cli.rb', line 288

def help_now!( message=nil )
	exception = OptionParser::ParseError.new( message )
	def exception.exit_code; 64; end

	raise exception
end

.highlight_string(string) ⇒ Object

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



315
316
317
# File 'lib/mongrel2/cli.rb', line 315

def highlight_string( string )
	return hl.highlight( string )
end

.hlObject

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



303
304
305
# File 'lib/mongrel2/cli.rb', line 303

def hl
	return Mongrel2::CLI.pastel
end

.promptObject

Get the prompt (a TTY::Prompt object)



297
298
299
# File 'lib/mongrel2/cli.rb', line 297

def prompt
	return Mongrel2::CLI.prompt
end

.success_string(string) ⇒ Object

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



321
322
323
# File 'lib/mongrel2/cli.rb', line 321

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.



367
368
369
370
371
372
373
374
# File 'lib/mongrel2/cli.rb', line 367

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.



359
360
361
# File 'lib/mongrel2/cli.rb', line 359

def visible_chars( string )
	return string.to_s.gsub(/\e\[.*?m/, '').scan( /\P{Cntrl}/ ).size
end