module CLIUtils::PrettyIO

CLIMessenger Module Outputs color-coordinated messages to a CLI

Public Class Methods

included(k) click to toggle source

Methods

—————————————————-

included method

Hook called when this module gets mixed in; extends the includer with the methods defined here. @param k The includer @return Void


# File lib/cliutils/pretty-io.rb, line 29
def self.included(k)
  k.extend(self)
end
wrap(on) click to toggle source

wrap method

Toggles wrapping on or off @return Integer


# File lib/cliutils/pretty-io.rb, line 210
def self.wrap(on)
  @@wrap = on
end
wrap_at(chars) click to toggle source

::wrap_at method

Sets the number of characters at which to wrap @return Integer


# File lib/cliutils/pretty-io.rb, line 230
def self.wrap_at(chars)
  @@wrap_char_limit = chars
end
wrap_limit() click to toggle source

::wrap_limit method

Returns the current character wrap amount @return Integer


# File lib/cliutils/pretty-io.rb, line 220
def self.wrap_limit
  @@wrap_char_limit
end

Public Instance Methods

color_chart() click to toggle source

#color_chart method

Displays a chart of all the possible ANSI foreground and background color combinations. @return Void


# File lib/cliutils/pretty-io.rb, line 40
def color_chart
  [0, 1, 4, 5, 7].each do |attr|
    puts '----------------------------------------------------------------'
    puts "ESC[#{attr};Foreground;Background"
    30.upto(37) do |fg|
      40.upto(47) do |bg|
        print "\033[#{attr};#{fg};#{bg}m #{fg};#{bg}  "
      end
    puts "\033[0m"
    end
  end
end
debug(m) click to toggle source

debug method

Empty method so that Messenging doesn't freak out when passed a debug message. @return Void


# File lib/cliutils/pretty-io.rb, line 60
def debug(m); end
error(m) click to toggle source

error method

Outputs a formatted-red error message. @param m The message to output @return Void


# File lib/cliutils/pretty-io.rb, line 69
def error(m)
  puts _word_wrap(m, '# ').red
end
info(m) click to toggle source

info method

Outputs a formatted-blue informational message. @param m The message to output @return Void


# File lib/cliutils/pretty-io.rb, line 80
def info(m)
  puts _word_wrap(m, '# ').blue
end
info_block(m1, m2 = 'Done.', multiline = false) { || ... } click to toggle source

#info_block method

Wraps a block in an opening and closing info message. @param m1 The opening message to output @param m2 The closing message to output @param multiline Whether the message should be multiline @return Void


# File lib/cliutils/pretty-io.rb, line 93
def info_block(m1, m2 = 'Done.', multiline = false)
  if block_given?
    if multiline
      info(m1)
    else
      print _word_wrap(m1, '# ').blue
    end

    yield

    if multiline
      info(m2)
    else
      puts _word_wrap(m2, '# ').blue
    end
  else
    fail 'Did not specify a valid block'
  end
end
log(m) click to toggle source

log method

Empty method so that Messenging doesn't freak out when passed a debug message. @return Void


# File lib/cliutils/pretty-io.rb, line 120
def log(m); end
prompt(prompt, default = nil, start_dir = '') click to toggle source

prompt method

Outputs a prompt, collects the user's response, and returns it. @param prompt The prompt to output @param default The default option @return String


# File lib/cliutils/pretty-io.rb, line 131
def prompt(prompt, default = nil, start_dir = '')
  Readline.completion_append_character = nil
  Readline.completion_proc = lambda do |prefix|
    files = Dir["#{start_dir}#{prefix}*"]
    files.
      map { |f| File.expand_path(f) }.
      map { |f| File.directory?(f) ? f + "/" : f }
  end
  choice = Readline.readline("# #{ prompt }#{ default.nil? ? ':' : " [default: #{ default }]:" } ".cyan)
  if choice.empty?
    default
  else
    choice
  end
end
section(m) click to toggle source

section method

Outputs a formatted-purple section message. @param m The message to output @return Void


# File lib/cliutils/pretty-io.rb, line 154
def section(m)
  puts _word_wrap(m, '---> ').purple
end
section_block(m, multiline = true) { || ... } click to toggle source

#section_block method

Wraps a block in an opening and closing section message. @param m1 The opening message to output @param m2 The closing message to output @param multiline A multiline message or not @return Void


# File lib/cliutils/pretty-io.rb, line 168
def section_block(m, multiline = true)
  if block_given?
    if multiline
      section(m)
    else
      print _word_wrap(m, '---> ').purple
    end

    yield
  else
    fail 'Did not specify a valid block'
  end
end
success(m) click to toggle source

success method

Outputs a formatted-green success message. @param m The message to output @return Void


# File lib/cliutils/pretty-io.rb, line 189
def success(m)
  puts _word_wrap(m, '# ').green
end
warn(m) click to toggle source

warning method

Outputs a formatted-yellow warning message. @param m The message to output @return Void


# File lib/cliutils/pretty-io.rb, line 200
def warn(m)
  puts _word_wrap(m, '# ').yellow
end