Class: Bovem::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/bovem/console.rb

Overview

This is a text utility wrapper console I/O.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConsole

Initializes a new Console.



148
149
150
151
152
# File 'lib/bovem/console.rb', line 148

def initialize
  @line_width = self.get_screen_width
  @indentation = 0
  @indentation_string = " "
end

Instance Attribute Details

#indentationObject

Current indentation width.



41
42
43
# File 'lib/bovem/console.rb', line 41

def indentation
  @indentation
end

#indentation_stringObject

The string used for indentation.



44
45
46
# File 'lib/bovem/console.rb', line 44

def indentation_string
  @indentation_string
end

#line_widthObject

The line width. Default to 80.



35
36
37
# File 'lib/bovem/console.rb', line 35

def line_width
  @line_width
end

#screen_widthObject

The current screen width.



38
39
40
# File 'lib/bovem/console.rb', line 38

def screen_width
  @screen_width
end

Class Method Details

.execute(command) ⇒ String

Executes a command and returns its output.

Parameters:

  • command (String)

    The command to execute.

Returns:

  • (String)

    The command's output.



143
144
145
# File 'lib/bovem/console.rb', line 143

def self.execute(command)
  %x{#{command}}
end

.instanceConsole

Returns a unique instance for Console.

Returns:



58
59
60
# File 'lib/bovem/console.rb', line 58

def self.instance
  @instance ||= ::Bovem::Console.new
end

.min_banner_lengthFixnum

Returns the minimum length of a banner, not including brackets and leading spaces.

Returns:

  • (Fixnum)

    The minimum length of a banner.



135
136
137
# File 'lib/bovem/console.rb', line 135

def self.min_banner_length
  1
end

.parse_style(style) ⇒ String

Parse a style and returns terminal codes.

Supported styles and colors are those in TERM_COLORS and TERM_EFFECTS. You can also prefix colors with bg_ (like bg_red) for background colors.

Parameters:

  • style (String)

    The style to parse.

Returns:

  • (String)

    A string with ANSI color codes.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/bovem/console.rb', line 68

def self.parse_style(style)
  rv = ""
  style = style.ensure_string.strip.parameterize

  if style.present? && style !~ /^[,-]$/ then
    style = style.ensure_string
    sym = style.to_sym

    if ::Bovem::TERM_EFFECTS.include?(sym) then
      rv = "\e[#{Bovem::TERM_EFFECTS[sym]}m"
    elsif style.index("bg_") == 0 then
      sym = style[3, style.length].to_sym
      rv = "\e[#{40 + ::Bovem::TERM_COLORS[sym]}m" if ::Bovem::TERM_COLORS.include?(sym)
    elsif style != "reset" then
      rv = "\e[#{30 + ::Bovem::TERM_COLORS[sym]}m" if ::Bovem::TERM_COLORS.include?(sym)
    end
  end

  rv
end

.replace_markers(message, plain = false) ⇒ String

Replaces colors markers in a string.

You can specify markers by enclosing in {mark=[style]} and {/mark} tags. Separate styles with spaces, dashes or commas. Nesting markers is supported.

Example:

Bovem::Console.new.replace_markers("{mark=bright bg_red}{mark=green}Hello world!{/mark}{/mark}")
# => "\e[1m\e[41m\e[32mHello world!\e[1m\e[41m\e[0m"

Parameters:

  • message (String)

    The message to analyze.

  • plain (Boolean) (defaults to: false)

    If ignore (cleanify) color markers into the message.

Returns:

  • (String)

    The replaced message.

See Also:

  • #parse_style


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/bovem/console.rb', line 104

def self.replace_markers(message, plain = false)
  stack = []
  mark_regexp = /((\{mark=([a-z\-_\s,]+)\})|(\{\/mark\}))/mi
  split_regex = /\s*[\s,-]\s*/

  message = message.ensure_string.gsub(mark_regexp) do
    tag = $1
    styles = $3
    replacement = ""

    if tag == "{/mark}" then # If it is a tag, pop from the latest opened.
      stack.pop
      styles = stack.last
      replacement = plain || stack.blank? ? "" : styles.split(split_regex).collect { |s| self.parse_style(s) }.join("")
    else
      replacement = plain ? "" : styles.split(split_regex).collect { |s| self.parse_style(s) }.join("")

      if replacement.length > 0 then
        stack << "reset" if stack.blank?
        stack << styles
      end
    end

    replacement
  end

  message
end

Instance Method Details

#begin(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true) ⇒ Object

Writes a message prepending a green banner.

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: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:



408
409
410
411
412
# File 'lib/bovem/console.rb', line 408

def begin(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  banner = self.get_banner("*", "bright green")
  message = self.indent(message, indented_banner ? 0 : indent)
  self.write(banner + " " + message, suffix, indented_banner ? indent : 0, wrap, plain, print)
end

#debug(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true) ⇒ Object

Writes a message prepending a magenta banner.

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: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:



480
481
482
483
484
# File 'lib/bovem/console.rb', line 480

def debug(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  banner = self.get_banner("D", "bright magenta", full_colored)
  message = self.indent(message, indented_banner ? 0 : indent)
  self.write(banner + " " + message, suffix, indented_banner ? indent : 0, wrap, plain, print)
end

#error(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true) ⇒ Object

Writes a message prepending a red banner.

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: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:



444
445
446
447
448
# File 'lib/bovem/console.rb', line 444

def error(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  banner = self.get_banner("E", "bright red", full_colored)
  message = self.indent(message, indented_banner ? 0 : indent)
  self.write(banner + " " + message, suffix, indented_banner ? indent : 0, wrap, plain, print)
end

#fatal(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, return_code = -1,, print = true) ⇒ Object

Writes a message prepending a red banner and then quits the application.

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: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • return_code (Fixnum) (defaults to: -1,)

    The code to return to the shell.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:



463
464
465
466
# File 'lib/bovem/console.rb', line 463

def fatal(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, return_code = -1, print = true)
  self.error(message, suffix, indent, wrap, plain, indented_banner, full_colored, print)
  Kernel.exit(return_code.to_integer(-1))
end

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

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:



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/bovem/console.rb', line 252

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 = @line_width

    if indent == true then
      wrap -= self.indentation
    else
      indent_i = indent.to_integer
      wrap -= (indent_i > 0 ? self.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.

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.



283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/bovem/console.rb', line 283

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

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

  @screen_width ||= self.get_screen_width
  width = (width == true || width.to_integer < 1 ? @screen_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

#get_banner(label, base_color, full_colored = false, bracket_color = "blue", brackets = ["[", "]"]) ⇒ String

Gets a banner for the messages.

Parameters:

  • label (String)

    The label for the banner.

  • base_color (String)

    The color for the label.

  • full_colored (String) (defaults to: false)

    If all the message should be of the label color.

  • bracket_color (String) (defaults to: "blue")

    The color of the brackets.

  • brackets (Array) (defaults to: ["[", "]"])

    An array of dimension 2 to use for brackets.

Returns:

  • (String)

    The banner.

See Also:



371
372
373
374
375
376
# File 'lib/bovem/console.rb', line 371

def get_banner(label, base_color, full_colored = false, bracket_color = "blue", brackets = ["[", "]"])
  label = label.rjust(Bovem::Console.min_banner_length, " ")
  brackets = brackets.ensure_array
  bracket_color = base_color if full_colored
  "{mark=%s}%s{mark=%s}%s{/mark}%s{/mark}" % [bracket_color.parameterize, brackets[0], base_color.parameterize, label, brackets[1]]
end

#get_screen_widthFixnum

Gets the current screen width.

Returns:

  • (Fixnum)

    The screen width.



157
158
159
# File 'lib/bovem/console.rb', line 157

def get_screen_width
  ::Bovem::Console.execute("tput cols").to_integer(80)
end

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

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 indentend message.



216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/bovem/console.rb', line 216

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

#info(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true) ⇒ Object

Writes a message prepending a cyan banner.

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: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:



390
391
392
393
394
# File 'lib/bovem/console.rb', line 390

def info(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  banner = self.get_banner("I", "bright cyan", full_colored)
  message = self.indent(message, indented_banner ? 0 : indent)
  self.write(banner + " " + message, suffix, indented_banner ? indent : 0, wrap, plain, print)
end

#read(prompt = true, default_value = nil, validator = nil, echo = true) ⇒ Object

Reads a string from the console.

Parameters:

  • prompt (String|Boolean) (defaults to: true)

    A prompt to show. If true, Please insert a value: will be used, if nil or false no prompt will be shown.

  • default_value (String) (defaults to: nil)

    Default value if user simply pressed the enter key.

  • validator (Array|Regexp) (defaults to: nil)

    An array of values or a Regexp to match the submitted value against.

  • echo (Boolean) (defaults to: true)

    If to show submitted text to the user.



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'lib/bovem/console.rb', line 492

def read(prompt = true, default_value = nil, validator = nil, echo = true)
  # Write the prompt
  prompt = "Please insert a value" if prompt == true
  final_prompt = !prompt.nil? ? prompt.gsub(/:?\s*$/, "") + ": " : nil

  # Adjust validator
  validator = validator.ensure_array.collect {|v| v.ensure_string} if validator.present? && !validator.is_a?(::Regexp)

  # Handle echo
  stty = ::Bovem::Console.execute("which stty").strip
  disable_echo = !echo && stty.present? && /-echo\b/mix.match(::Bovem::Console.execute(stty)).nil?

  # Disable echo
  ::Bovem::Console.execute("#{stty} -echo") if disable_echo

  begin
    catch(:reply) do
      while true do
        valid = true

        if final_prompt then
          Kernel.print self.format(final_prompt, false, false)
          $stdout.flush
        end

        reply = Kernel.gets.chop
        reply = default_value if reply.empty?

        # Match against the validator
        if validator.present? then
          if validator.is_a?(Array) then
            valid = false if validator.length > 0 && !validator.include?(reply)
          elsif validator.is_a?(Regexp) then
            valid = false if !validator.match(reply)
          end
        end

        if !valid then
          self.write("Sorry, your reply was not understood. Please try again.", false, false)
        else
          throw(:reply, reply)
        end
      end
    end
  rescue Interrupt
    default_value
  ensure
    ::Bovem::Console.execute("#{stty} echo") if disable_echo
  end
end

#replace_markers(message, plain = false) ⇒ String

Replaces colors markers in a string.

Parameters:

  • message (String)

    The message to analyze.

  • plain (Boolean) (defaults to: false)

    If ignore (cleanify) color markers into the message.

Returns:

  • (String)

    The replaced message.

See Also:



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

def replace_markers(message, plain = false)
  ::Bovem::Console.replace_markers(message, plain)
end

#reset_indentationFixnum

Resets indentation width to 0.

Returns:

  • (Fixnum)

    The new indentation width.



174
175
176
177
# File 'lib/bovem/console.rb', line 174

def reset_indentation
  self.indentation = 0
  self.indentation
end

#set_indentation(width, is_absolute = false) ⇒ Fixnum

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.



166
167
168
169
# File 'lib/bovem/console.rb', line 166

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

#status(status, plain = false, go_up = true, right = true, print = true) ⇒ Array

Writes a status to the output. Valid values are :ok, :pass, :fail, :warn.

Parameters:

  • status (Symbol)

    The status to write.

  • plain (Boolean) (defaults to: false)

    If not use colors.

  • go_up (Boolean) (defaults to: true)

    If go up one line before formatting.

  • right (Boolean) (defaults to: true)

    If to print results on the right.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

Returns:

  • (Array)

    An dictionary with :label and :color keys for the status.



338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/bovem/console.rb', line 338

def status(status, plain = false, go_up = true, right = true, print = true)
  statuses = {
    :ok => {:label => " OK ", :color => "bright green"},
    :pass => {:label => "PASS", :color => "bright cyan"},
    :warn => {:label => "WARN", :color => "bright yellow"},
    :fail => {:label => "FAIL", :color => "bright red"}
  }
  statuses.default = statuses[:ok]

  rv = statuses[status]

  if print then
    banner = self.get_banner(rv[:label], rv[:color])

    if right then
      Kernel.puts self.format_right(banner + " ", true, go_up, plain)
    else
      Kernel.puts self.format(banner + " ", "\n", true, true, plain)
    end
  end

  rv
end

#task(message = nil, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, block_indentation = 2, block_indentation_absolute = false) ⇒ Symbol

Executes a block of code in a indentation region and then prints out and ending status message.

Parameters:

  • message (String) (defaults to: nil)

    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: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • block_indentation (Fixnum) (defaults to: 2)

    The new width for the indented region.

  • block_indentation_absolute (Boolean) (defaults to: false)

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

Returns:

  • (Symbol)

    The exit status for the block.



555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
# File 'lib/bovem/console.rb', line 555

def task(message = nil, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, block_indentation = 2, block_indentation_absolute = false)
  status = nil

  self.begin(message, suffix, indent, wrap, plain, indented_banner, full_colored) if message.present?

  self.with_indentation(block_indentation, block_indentation_absolute) do
    rv = block_given? ? yield.ensure_array : [:ok] # Execute block
    status = rv[0] # Return value

    if status == :fatal then
      self.status(:fail, plain)
      exit(rv.length > 1 ? rv[1].to_integer : -1)
    else
      self.status(status, plain) if message.present?
    end
  end

  status
end

#warn(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true) ⇒ Object

Writes a message prepending a yellow banner.

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: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • indented_banner (Boolean) (defaults to: false)

    If also the banner should be indented.

  • full_colored (Boolean) (defaults to: false)

    If the banner should be fully colored.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

See Also:



426
427
428
429
430
# File 'lib/bovem/console.rb', line 426

def warn(message, suffix = "\n", indent = true, wrap = false, plain = false, indented_banner = false, full_colored = false, print = true)
  banner = self.get_banner("W", "bright yellow", full_colored)
  message = self.indent(message, indented_banner ? 0 : indent)
  self.write(banner + " " + message, suffix, indented_banner ? indent : 0, wrap, plain, print)
end

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

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.



184
185
186
187
188
189
190
191
# File 'lib/bovem/console.rb', line 184

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

  self.indentation
end

#wrap(message, width = nil) ⇒ String

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.



198
199
200
201
202
203
204
205
206
207
208
# File 'lib/bovem/console.rb', line 198

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

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

Writes a message.

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: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

Returns:

  • (String)

    The printed message.

See Also:



309
310
311
312
313
# File 'lib/bovem/console.rb', line 309

def write(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)
  rv = self.format(message, suffix, indent, wrap, plain)
  Kernel.puts(rv) if print
  rv
end

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

Writes a message, aligning to a call with an empty banner.

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: false)

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

  • plain (Boolean) (defaults to: false)

    If ignore color markers into the message.

  • print (Boolean) (defaults to: true)

    If false, the result will be returned instead of be printed.

Returns:

  • (String)

    The printed message.

See Also:



326
327
328
# File 'lib/bovem/console.rb', line 326

def write_banner_aligned(message, suffix = "\n", indent = true, wrap = false, plain = false, print = true)
  self.write((" " * (::Bovem::Console.min_banner_length + 3)) + message.ensure_string, suffix, indent, wrap, plain, print)
end