Module: Bovem::ConsoleMethods::Output

Included in:
Bovem::Console
Defined in:
lib/bovem/console.rb

Overview

Methods for formatting output messages.

Instance Method Summary collapse

Instance Method Details

#format(message, suffix = "\n", indent = true, wrap = true, plain = false) ⇒ String

Formats a message.

You can style text by using {mark} and {/mark} syntax.

See Also:

  • #replace_markers


194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/bovem/console.rb', line 194

def format(message, suffix = "\n", indent = true, wrap = true, plain = false)
  rv = message

  rv = self.replace_markers(rv, plain) # Replace markers

  # Compute the real width available for the screen, if we both indent and wrap
  if wrap == true then
    wrap = self.line_width

    if indent == true then
      wrap -= @indentation
    else
      indent_i = indent.to_integer
      wrap -= (indent_i > 0 ? @indentation : 0) + indent_i
    end
  end

  rv = self.wrap(rv, wrap) # Wrap
  rv = self.indent(rv, indent) # Indent

  rv += suffix.ensure_string if suffix # Add the suffix
  rv
end

#format_right(message, width = true, go_up = true, plain = false) ⇒ String

Formats a message to be written right-aligned.



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/bovem/console.rb', line 225

def format_right(message, width = true, go_up = true, plain = false)
  message = self.replace_markers(message, plain)

  rv = go_up ? "\e[A" : ""

  width = (width == true || width.to_integer < 1 ? self.line_width : width.to_integer)

  # Get padding
  padding = width - message.to_s.gsub(/(\e\[[0-9]*[a-z]?)|(\\n)/i, "").length

  # Return
  rv + "\e[0G\e[#{padding}C" + message
end

#indent(message, width = true, newline_separator = "\n") ⇒ String

Indents a message.



169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/bovem/console.rb', line 169

def indent(message, width = true, newline_separator = "\n")
  if width.to_integer != 0 then
    width = (width == true ? 0 : width.to_integer)
    width = width < 0 ? -width : @indentation + width

    message = message.split(newline_separator).collect {|line|
      (@indentation_string * width) + line
    }.join(newline_separator)
  end

  message
end

#reset_indentationFixnum

Resets indentation width to 0.



128
129
130
# File 'lib/bovem/console.rb', line 128

def reset_indentation
  @indentation = 0
end

#set_indentation(width, is_absolute = false) ⇒ Fixnum

Sets the new indentation width.



120
121
122
123
# File 'lib/bovem/console.rb', line 120

def set_indentation(width, is_absolute = false)
  @indentation = [(!is_absolute ? @indentation : 0) + width, 0].max.to_i
  @indentation
end

#with_indentation(width = 3, is_absolute = false) ⇒ Fixnum

Starts a indented region of text.



137
138
139
140
141
142
143
144
# File 'lib/bovem/console.rb', line 137

def with_indentation(width = 3, is_absolute = false)
  old = @indentation
  self.set_indentation(width, is_absolute)
  yield
  self.set_indentation(old, true)

  @indentation
end

#wrap(message, width = nil) ⇒ String

Wraps a message in fixed line width.



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/bovem/console.rb', line 151

def wrap(message, width = nil)
  if width.to_integer <= 0 then
    message
  else
    width = (width == true || width.to_integer < 0 ? self.get_screen_width : width.to_integer)

    message.split("\n").collect { |line|
      line.length > width ? line.gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n").strip : line
    }.join("\n")
  end
end