Module: Cuprum::Cli::Dependencies::StandardIo::Helpers

Included in:
Commands::Ci::RSpecCommand, Commands::Ci::RSpecEachCommand, Commands::File::GenerateFile
Defined in:
lib/cuprum/cli/dependencies/standard_io/helpers.rb

Overview

Helper methods for reading from and writing to standard inputs and outputs.

Constant Summary collapse

FALSY_VALUES =

String input values that will be mapped to a boolean false.

Set.new(%w[f false n no]).freeze
INTEGER_PATTERN =

Pattern matching a valid integer input.

/\A-?\d+([\d_,]+\d)?\z/
TRUTHY_VALUES =

String input values that will be mapped to a boolean true.

Set.new(%w[t true y yes]).freeze

Instance Method Summary collapse

Instance Method Details

#ask(prompt = nil, caret: true, format: nil, strip: true, **options) ⇒ String, ...

Requests an input from the input stream.

Parameters:

  • prompt (String, nil) (defaults to: nil)

    the prompt to display to the user, if any.

  • options (Hash)

    options for requesting the input.

Options Hash (**options):

  • caret (true, false)

    if true, prints a caret "> " to the output stream after the prompt. Defaults to true when the newline option is true, otherwise false.

  • format (String, Symbol)

    the expected format of the input. Valid values are :string (the default), :boolean, and :integer. The input string will be transformed into the given format, or an exception raised if the value cannot be transformed.

  • newline (true, false)

    if true, a newline will be printed after the prompt if a prompt is given.

  • strip (true, false)

    if true, strips the trailing newline from the input. Defaults to true.

Returns:

  • (String, Integer, true, false, nil)

    the received and formatted input value, or nil if the input value was empty.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/cuprum/cli/dependencies/standard_io/helpers.rb', line 37

def ask( # rubocop:disable Metrics/ParameterLists
  prompt = nil,
  caret:   nil,
  format:  nil,
  newline: true,
  strip:   true,
  **
)
  validate_prompt(prompt)
  display_prompt(caret:, newline:, prompt:)

  value = standard_io.read_input&.then { |str| strip ? str.strip : str }

  return if value.nil? || value.empty?
  return value if format.nil?

  send(:"format_#{format}", value)
end

#say(message, newline: true, quiet: false, verbose: false, **options) ⇒ nil

Prints a message to the output stream.

Parameters:

  • message (String)

    the message to print.

  • options (Hash)

    options for printing the message.

Options Hash (**options):

  • newline (true, false)

    if true, appends a newline to the message if the message does not end with a newline. Defaults to true.

  • quiet (true, false)

    if true, prints the message even if the command has the :quiet option enabled. Defaults to false. Ignored if the command does not support the :quiet option.

  • verbose (true, false)

    if true, prints the message only if the command has the :verbose option enabled. Defaults to false. Ignored if the command does not support the :verbose option.

Returns:

  • (nil)


73
74
75
76
77
# File 'lib/cuprum/cli/dependencies/standard_io/helpers.rb', line 73

def say(message, newline: true, **)
  validate_message(message)

  standard_io.write_output(message, newline:)
end

#warn(message, **options) ⇒ nil

Prints a message to the error stream.

Parameters:

  • message (String)

    the message to print.

  • options (Hash)

    options for printing the message.

Options Hash (**options):

  • newline (true, false)

    if true, appends a newline to the message if the message does not end with a newline. Defaults to true.

Returns:

  • (nil)


89
90
91
92
93
# File 'lib/cuprum/cli/dependencies/standard_io/helpers.rb', line 89

def warn(message, newline: true, **)
  validate_message(message)

  standard_io.write_error(message, newline:)
end