Module: Bovem::ConsoleMethods::Output
- Included in:
- Bovem::Console
- Defined in:
- lib/bovem/console.rb
Overview
Methods for formatting output messages.
Instance Method Summary (collapse)
-
- (String) emphasize(message, style = "bright")
Embeds a message in a style.
-
- (String) format(message, suffix = "\n", indent = true, wrap = true, plain = false)
Formats a message.
-
- (String) format_right(message, width = true, go_up = true, plain = false)
Formats a message to be written right-aligned.
-
- (String) indent(message, width = true, newline_separator = "\n")
Indents a message.
-
- (Fixnum) reset_indentation
Resets indentation width to
0
. -
- (Fixnum) set_indentation(width, is_absolute = false)
Sets the new indentation width.
-
- (Fixnum) with_indentation(width = 3, is_absolute = false)
Starts a indented region of text.
-
- (String) wrap(message, width = nil)
Wraps a message in fixed line width.
Instance Method Details
- (String) emphasize(message, style = "bright")
Embeds a message in a style.
237 238 239 |
# File 'lib/bovem/console.rb', line 237 def emphasize(, style = "bright") "{mark=#{style}}#{}{/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.
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(, suffix = "\n", indent = true, wrap = true, plain = false) rv = 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.
220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/bovem/console.rb', line 220 def format_right(, width = true, go_up = true, plain = false) = replace_markers(, plain) width = (width == true || width.to_integer < 1 ? line_width : to_integer) # Get padding padding = width - .to_s.gsub(/(\e\[[0-9]*[a-z]?)|(\\n)/i, "").length # Return "#{go_up ? "\e[A" : ""}\e[0G\e[#{padding}C#{}" end |
- (String) indent(message, width = true, newline_separator = "\n")
Indents a message.
166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/bovem/console.rb', line 166 def indent(, 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 = .split(newline_separator).collect {|line| (@indentation_string * width) + line }.join(newline_separator) end end |
- (Fixnum) reset_indentation
Resets indentation width to 0
.
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.
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.
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.
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/bovem/console.rb', line 148 def wrap(, width = nil) if width.to_integer <= 0 then else width = (width == true || width.to_integer < 0 ? line_width : width.to_integer) .split("\n").collect { |line| line.length > width ? line.gsub(/(.{1,#{width}})(\s+|$)/, "\\1\n").strip : line }.join("\n") end end |