Module: CLIUtils::PrettyIO

Included in:
Messenging, Prefs
Defined in:
lib/cliutils/pretty-io.rb

Overview

CLIMessenger Module
Outputs color-coordinated messages to a CLI
======================================================

Constant Summary

@@wrap =
true
@@wrap_char_limit =
40

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) included(k)

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
----------------------------------------------------


29
30
31
# File 'lib/cliutils/pretty-io.rb', line 29

def self.included(k)
  k.extend(self)
end

+ (Object) wrap(on)


wrap method

Toggles wrapping on or off
@return Integer
----------------------------------------------------


210
211
212
# File 'lib/cliutils/pretty-io.rb', line 210

def self.wrap(on)
  @@wrap = on
end

+ (Object) wrap_at(chars)


wrap_at method

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


230
231
232
# File 'lib/cliutils/pretty-io.rb', line 230

def self.wrap_at(chars)
  @@wrap_char_limit = chars
end

+ (Object) wrap_limit


wrap_limit method

Returns the current character wrap amount
@return Integer
----------------------------------------------------


220
221
222
# File 'lib/cliutils/pretty-io.rb', line 220

def self.wrap_limit
  @@wrap_char_limit
end

Instance Method Details

- (Object) color_chart


color_chart method

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


40
41
42
43
44
45
46
47
48
49
50
51
# 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

- (Object) debug(m)


debug method

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


60
# File 'lib/cliutils/pretty-io.rb', line 60

def debug(m); end

- (Object) error(m)


error method

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


69
70
71
# File 'lib/cliutils/pretty-io.rb', line 69

def error(m)
  puts _word_wrap(m, '# ').red
end

- (Object) info(m)


info method

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


80
81
82
# File 'lib/cliutils/pretty-io.rb', line 80

def info(m)
  puts _word_wrap(m, '# ').blue
end

- (Object) info_block(m1, m2 = 'Done.', multiline = false)


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
----------------------------------------------------


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# 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

- (Object) log(m)


log method

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


120
# File 'lib/cliutils/pretty-io.rb', line 120

def log(m); end

- (Object) prompt(prompt, default = nil, start_dir = '')


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
----------------------------------------------------


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 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

- (Object) section(m)


section method

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


154
155
156
# File 'lib/cliutils/pretty-io.rb', line 154

def section(m)
  puts _word_wrap(m, '---> ').purple
end

- (Object) section_block(m, multiline = true)


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
----------------------------------------------------


168
169
170
171
172
173
174
175
176
177
178
179
180
# 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

- (Object) success(m)


success method

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


189
190
191
# File 'lib/cliutils/pretty-io.rb', line 189

def success(m)
  puts _word_wrap(m, '# ').green
end

- (Object) warn(m)


warning method

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


200
201
202
# File 'lib/cliutils/pretty-io.rb', line 200

def warn(m)
  puts _word_wrap(m, '# ').yellow
end