Class: Cuprum::Cli::Dependencies::StandardIo
- Inherits:
-
Object
- Object
- Cuprum::Cli::Dependencies::StandardIo
- Defined in:
- lib/cuprum/cli/dependencies/standard_io.rb
Overview
Utility wrapping standard input, output, and error IO streams.
Direct Known Subclasses
Defined Under Namespace
Modules: Helpers Classes: Mock
Instance Method Summary collapse
-
#color(text, color) ⇒ String
Wraps the text in an ANSI color escape code.
-
#initialize(error_stream: -> { $stderr }, input_stream: -> { $stdin }, output_stream: -> { $stdout }) ⇒ StandardIo
constructor
A new instance of StandardIo.
-
#read_input ⇒ String
Requests a newline-terminated string from the input stream.
-
#write_error(message = nil, newline: true) ⇒ nil
Writes the given message to the error stream.
-
#write_output(message = nil, newline: true) ⇒ nil
Writes the given message to the output stream.
Constructor Details
#initialize(error_stream: -> { $stderr }, input_stream: -> { $stdin }, output_stream: -> { $stdout }) ⇒ StandardIo
Returns a new instance of StandardIo.
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.
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_input ⇒ String
Requests a newline-terminated string from the input stream.
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.
64 65 66 |
# File 'lib/cuprum/cli/dependencies/standard_io.rb', line 64 def write_error( = nil, newline: true) newline ? error_stream.puts() : error_stream.print() 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.
77 78 79 |
# File 'lib/cuprum/cli/dependencies/standard_io.rb', line 77 def write_output( = nil, newline: true) newline ? output_stream.puts() : output_stream.print() end |