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

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

Reads a string from the console.

Parameters:

  • prompt (String|Boolean)

    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)

    Default value if user simply pressed the enter key.

  • validator (Array|Regexp)

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

  • echo (Boolean)

    If to show submitted text to the user. Not supported and thus ignored on Rubinius.



564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
# File 'lib/bovem/console.rb', line 564

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

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

#task(message = nil, suffix: "\n", indented: 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)

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

  • indented (Object)

    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)

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

  • plain (Boolean)

    If ignore color markers into the message.

  • indented_banner (Boolean)

    If also the banner should be indented.

  • full_colored (Boolean)

    If the banner should be fully colored.

  • block_indentation (Fixnum)

    The new width for the indented region.

  • block_indentation_absolute (Boolean)

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

Returns:

  • (Symbol)

    The exit status for the block.



593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
# File 'lib/bovem/console.rb', line 593

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

  if message.present?
    self.begin(message, suffix: suffix, indented: indented, wrap: wrap, plain: plain, indented_banner: indented_banner, full_colored: full_colored)
  end

  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