Module: Bovem::ConsoleMethods::Interactions

Extended by:
ActiveSupport::Concern
Included in:
Bovem::Console
Defined in:
lib/bovem/console.rb

Overview

Methods to interact with the user and other processes.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary (collapse)

Instance Method Details

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

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. Not supported and thus ignored on Rubinius.



463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
# File 'lib/bovem/console.rb', line 463

def read(prompt = true, default_value = nil, validator = nil, echo = true)
  prompt = sanitize_prompt(prompt)
  validator = sanitize_validator(validator)

  begin
    catch(:reply) do
      while true do
        reply = validate_input_value(read_input_value(prompt, echo, default_value), validator)
        handle_reply(reply)
      end
    end
  rescue Interrupt
    default_value
  end
end

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

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.



491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/bovem/console.rb', line 491

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?
  with_indentation(block_indentation, block_indentation_absolute) do
    rv = block_given? ? yield.ensure_array : [:ok] # Execute block
    exit_task(message, rv, plain) # Handle task exit
    status = rv[0] # Return value

  end

  status
end