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

- (String) emphasize(message, style = "bright")

Embeds a message in a style.

Parameters:

  • message (String)

    The message to emphasize.

  • style (String) (defaults to: "bright")

    The style to use.

Returns:

  • (String)

    The emphasized message.



237
238
239
# File 'lib/bovem/console.rb', line 237

def emphasize(message, style = "bright")
  "{mark=#{style}}#{message}{/mark}"
end

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

Formats a message.

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

Parameters:

  • message (String)

    The message to format.

  • suffix (Object) (defaults to: "\n")

    If not nil or false, a suffix to add to the message. true means to add \n.

  • indent (Object) (defaults to: true)

    If not nil or false, the width to use for indentation. true means to use the current indentation, a negative value of -x will indent of x absolute spaces.

  • wrap (Object) (defaults to: true)

    If not nil or false, the maximum length of a line. true means the current line width.

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

Returns:

  • (String)

    The formatted message.

See Also:

  • #replace_markers


191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/bovem/console.rb', line 191

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

  rv = replace_markers(rv, plain) # Replace markers

  # Compute the real width available for the screen, if we both indent and wrap
  if wrap.is_a?(TrueClass) then
    wrap = line_width

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

  rv = indent(wrap(rv, wrap), indent) # Wrap & Indent
  rv += (suffix.is_a?(TrueClass) ? "\n" : suffix.ensure_string) if suffix # Add the suffix
  rv
end

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

Formats a message to be written right-aligned.

Parameters:

  • message (String)

    The message to format.

  • width (Fixnum) (defaults to: true)

    The screen width. If true, it is automatically computed.

  • go_up (Boolean) (defaults to: true)

    If go up one line before formatting.

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

Returns:

  • (String)

    The formatted message.



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/bovem/console.rb', line 220

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

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

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

  # Return
  "#{go_up ? "\e[A" : ""}\e[0G\e[#{padding}C#{message}"
end

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

Indents a message.

Parameters:

  • message (String)

    The message to indent.

  • width (Fixnum) (defaults to: true)

    The indentation width. true means to use the current indentation, a negative value of -x will indent of x absolute spaces. nil or false will skip indentation.

  • newline_separator (String) (defaults to: "\n")

    The character used for newlines.

Returns:

  • (String)

    The indented message.



166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/bovem/console.rb', line 166

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

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

  message
end

- (Fixnum) reset_indentation

Resets indentation width to 0.

Returns:

  • (Fixnum)

    The new indentation width.



125
126
127
# File 'lib/bovem/console.rb', line 125

def reset_indentation
  @indentation = 0
end

- (Fixnum) set_indentation(width, is_absolute = false)

Sets the new indentation width.

Parameters:

  • width (Fixnum)

    The new width.

  • is_absolute (Boolean) (defaults to: false)

    If the new width should not be added to the current one but rather replace it.

Returns:

  • (Fixnum)

    The new indentation width.



118
119
120
# File 'lib/bovem/console.rb', line 118

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

- (Fixnum) with_indentation(width = 3, is_absolute = false)

Starts a indented region of text.

Parameters:

  • width (Fixnum) (defaults to: 3)

    The new width.

  • is_absolute (Boolean) (defaults to: false)

    If the new width should not be added to the current one but rather replace it.

Returns:

  • (Fixnum)

    The new indentation width.



134
135
136
137
138
139
140
141
# File 'lib/bovem/console.rb', line 134

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

  @indentation
end

- (String) wrap(message, width = nil)

Wraps a message in fixed line width.

Parameters:

  • message (String)

    The message to wrap.

  • width (Fixnum) (defaults to: nil)

    The maximum width of a line. Default to the current line width.

Returns:

  • (String)

    The wrapped message.



148
149
150
151
152
153
154
155
156
157
158
# File 'lib/bovem/console.rb', line 148

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

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