Class: Cuprum::Cli::Dependencies::StandardIo

Inherits:
Object
  • Object
show all
Defined in:
lib/cuprum/cli/dependencies/standard_io.rb

Overview

Utility wrapping standard input, output, and error IO streams.

Direct Known Subclasses

Mock

Defined Under Namespace

Modules: Helpers Classes: Mock

Instance Method Summary collapse

Constructor Details

#initialize(error_stream: -> { $stderr }, input_stream: -> { $stdin }, output_stream: -> { $stdout }) ⇒ StandardIo

Returns a new instance of StandardIo.

Parameters:

  • error_stream (IO) (defaults to: -> { $stderr })

    the error stream. Defaults to $stderr.

  • input_stream (IO) (defaults to: -> { $stdin })

    the input stream. Defaults to $stdin.

  • output_stream (IO) (defaults to: -> { $stdout })

    the output stream. Defaulst to $stdout.



24
25
26
27
28
29
30
31
32
# File 'lib/cuprum/cli/dependencies/standard_io.rb', line 24

def initialize(
  error_stream:  -> { $stderr },
  input_stream:  -> { $stdin },
  output_stream: -> { $stdout }
)
  @lazy_error_stream  = error_stream
  @lazy_input_stream  = input_stream
  @lazy_output_stream = output_stream
end

Instance Method Details

#color(text, color) ⇒ String

Wraps the text in an ANSI color escape code.

Parameters:

  • text (String)

    the text to colorize.

  • color (String)

    the color to apply.

Returns:

  • (String)

    the colorized string.

Raises:

  • (KeyError)

    if the requested color does not have an escape code.



42
43
44
45
46
# File 'lib/cuprum/cli/dependencies/standard_io.rb', line 42

def color(text, color)
  color_code = ANSI_COLORS.fetch(color.to_s)

  "\e[#{color_code}m#{text}\e[0m"
end

#read_inputString

Requests a newline-terminated string from the input stream.

Returns:

  • (String)

    the returned input string.



51
52
53
# File 'lib/cuprum/cli/dependencies/standard_io.rb', line 51

def read_input
  input_stream.gets
end

#write_error(message = nil, newline: true) ⇒ nil

Writes the given message to the error stream.

If no error message is given, prints a newline only.

Parameters:

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

    the message to write.

  • newline (true, false) (defaults to: true)

    if true, appends a newline to the message if it does not have a newline. Defaults to true.

Returns:

  • (nil)


64
65
66
# File 'lib/cuprum/cli/dependencies/standard_io.rb', line 64

def write_error(message = nil, newline: true)
  newline ? error_stream.puts(message) : error_stream.print(message)
end

#write_output(message = nil, newline: true) ⇒ nil

Writes the given message to the output stream.

If no message is given, prints a newline only.

Parameters:

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

    the message to write.

  • newline (true, false) (defaults to: true)

    if true, appends a newline to the message if it does not have a newline. Defaults to true.

Returns:

  • (nil)


77
78
79
# File 'lib/cuprum/cli/dependencies/standard_io.rb', line 77

def write_output(message = nil, newline: true)
  newline ? output_stream.puts(message) : output_stream.print(message)
end