Module: Votd::Helper::CommandLine

Extended by:
CommandLine
Included in:
CommandLine
Defined in:
lib/votd/helper/command_line.rb

Overview

This module contains helper methods that support the command-line application

Instance Method Summary collapse

Instance Method Details

Generates a text banner suitable for displaying from a command-line utility.

Examples:

banner("My Banner", 20)

->
====================
     My Banner
====================

Parameters:

  • line_width (Integer) (defaults to: 40)

    number of columns for width

  • text (String)

    text to print inside the banner

Returns:

  • (nil)


19
20
21
22
23
24
25
26
27
# File 'lib/votd/helper/command_line.rb', line 19

def banner(text, line_width=40)
  banner_text = "=" * line_width
  banner_text << "\n"
  banner_text << text.center(line_width)
  banner_text << "\n"
  banner_text <<  "=" * line_width
  puts banner_text
  nil
end

#word_wrap(text, line_width = 40) ⇒ String

Word-wraps text to the specified column width.

Parameters:

  • text (String)

    the text to be wrapped

  • line_width (Integer) (defaults to: 40)

    column width

Returns:

  • (String)

    wrapped text



33
34
35
36
37
# File 'lib/votd/helper/command_line.rb', line 33

def word_wrap(text, line_width=40)
  text.split("\n").collect do |line|
    line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
  end * "\n"
end