Module: TcravitRubyLib

Extended by:
TcravitRubyLib
Included in:
TcravitRubyLib
Defined in:
lib/tcravit_ruby_lib.rb,
lib/tcravit_ruby_lib/utility.rb,
lib/tcravit_ruby_lib/version.rb,
lib/tcravit_ruby_lib/app_banner.rb,
lib/tcravit_ruby_lib/app_config.rb,
lib/tcravit_ruby_lib/rake_tasks.rb,
lib/tcravit_ruby_lib/configurable.rb,
lib/tcravit_ruby_lib/config_searcher.rb

Overview

:nodoc:

Defined Under Namespace

Modules: AppConfig, ConfigSearcher, Configurable, Utility Classes: RakeTasks

Constant Summary collapse

VERSION_DATA =
[0, 2, 9]
VERSION =
VERSION_DATA.join(".")

Instance Method Summary collapse

Instance Method Details

Generate an app startup banner for a command-line application.

Banner Formatting

The app banner will be framed with asterisks. By default, it will be 76 characters long, though this can be overridden by passing in a value for line_length.

If a description is included, the first text line of the banner will take the form “name: description”; otherwise, just the name will be output. This line will be centered, with whitespace added to the ends to make the asterisks line up. Otherwise, just the app name will be output.

If any of version, date, and/or author are supplied, these will be joined (in that order) by commas, and a second centered line of text containing those components will be output. If none are supplied, no second line will be output.

Arguments

For flexibility, parameters are all passed in a hash. The method accepts the following options:

  • ‘:name` - The name of the application. Required.

  • ‘:description` - A brief description of the application. Optional.

  • ‘:version`

  • ‘:date`

  • ‘:author`

  • ‘:line_length`

The only required option is name, and an ArgumentError will be raised if it is not supplied. All other options are optional, and the method will simply use whichever ones are supplied to generate the banner.

For examples, see the RSpec tests for this method.

Parameters:

  • opts (Hash) (defaults to: {})

    A hash of options.

Returns:

  • (String)

    The app banner, ready to print out

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/tcravit_ruby_lib/app_banner.rb', line 66

def Banner(opts={})
  raise ArgumentError, "Name not provided" unless opts.keys.include?("name".to_sym)

  line_length = 76
  line_length = opts[:line_length] if opts.keys.include?(:line_length)

  lines = []
  lines.push("*" * line_length)

  name_line = "#{opts[:name]}"
  if (opts.keys.include?(:description)) then
    name_line = name_line + ": " + opts[:description]
  end
  lines.push(asterisk_pad(name_line, line_length))

   = []
  if (opts.keys.include?(:version)) then
    .push("Version #{opts[:version]}")
  end
  if (opts.keys.include?(:date)) then
    .push(opts[:date])
  end
  if (opts.keys.include?(:author)) then
    .push(opts[:author])
  end
  unless .empty?
    lines.push(asterisk_pad(.join(", ")))
  end

  lines.push("*" * line_length)
  
  return lines.join("\n")
end