Class: Brut::CLI::Output
- Inherits:
-
Object
- Object
- Brut::CLI::Output
- Defined in:
- lib/brut/cli/output.rb
Overview
An IO-like class that provides user-helpful output, to be used in place of puts. This is not strictly an IO and is intended to provide only a few options for sending output to a human.
Command line apps that a human runs are often executed in the context of other apps or themselves spawn child processes. Thus, it can be hard to know what output is coming from where. This class addresses that by prefixing every line with the name of the command line app:
> bin/my_app doit
[ bin/my_app ] About to do sometohing
Cannot connect to house
[ bin/my_app ] Problem connecting
The prefix allows the human to know what output was generated by the app they ran and what not. This can be extremely helpful for debugging or just understanding what is going on.
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#prefix ⇒ Object
readonly
Returns the value of attribute prefix.
Class Method Summary collapse
Instance Method Summary collapse
-
#flush ⇒ Object
Flush the underlying
IO. -
#initialize(io:, prefix:) ⇒ Output
constructor
Create a wrapper for the given IO that will use the given prefix.
-
#print(*objects) ⇒ Object
Calls
printwithout any prefix. -
#printf(format_string, *objects) ⇒ Object
Prints a string via
printf, using the prefix. -
#printf_no_prefix(format_string, *objects) ⇒ Object
Prints a string via
printf, without the prefix. -
#puts(*objects) ⇒ Object
Calls
puts, adding a prefix to each of the objects in*objects. -
#puts_no_prefix(*objects) ⇒ Object
Calls
putswithout the prefix.
Constructor Details
#initialize(io:, prefix:) ⇒ Output
Create a wrapper for the given IO that will use the given prefix
34 35 36 37 38 |
# File 'lib/brut/cli/output.rb', line 34 def initialize(io:, prefix:) @io = io @prefix = prefix @sync_status = @io.sync end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
27 28 29 |
# File 'lib/brut/cli/output.rb', line 27 def io @io end |
#prefix ⇒ Object (readonly)
Returns the value of attribute prefix.
28 29 30 |
# File 'lib/brut/cli/output.rb', line 28 def prefix @prefix end |
Class Method Details
Instance Method Details
#flush ⇒ Object
Flush the underlying IO.
86 87 88 89 |
# File 'lib/brut/cli/output.rb', line 86 def flush @io.flush self end |
#print(*objects) ⇒ Object
Calls print without any prefix. In theory, this is the escape hatch to sending arbitrary data to the underlying IO without
expose said IO
66 67 68 |
# File 'lib/brut/cli/output.rb', line 66 def print(*objects) @io.print(*objects) end |
#printf(format_string, *objects) ⇒ Object
Prints a string via printf, using the prefix. This is useful for communciating to a human, but you need more power for
formatting than is afforded by #puts.
74 75 76 |
# File 'lib/brut/cli/output.rb', line 74 def printf(format_string,*objects) @io.printf(@prefix + format_string,*objects) end |
#printf_no_prefix(format_string, *objects) ⇒ Object
Prints a string via printf, without the prefix.
81 82 83 |
# File 'lib/brut/cli/output.rb', line 81 def printf_no_prefix(format_string,*objects) @io.printf(format_string,*objects) end |
#puts(*objects) ⇒ Object
Calls puts, adding a prefix to each of the objects in *objects. This is useful for sending messages that a human may want to
read.
52 53 54 55 56 57 58 59 60 |
# File 'lib/brut/cli/output.rb', line 52 def puts(*objects) if objects.empty? objects << "" end objects.each do |object| @io.puts(@prefix + object.to_s) end nil end |
#puts_no_prefix(*objects) ⇒ Object
Calls puts without the prefix. This is useful if the output is going to be obvious to the human user (e.g. CLI help), or if
it's intended to be piped into another app.
44 45 46 |
# File 'lib/brut/cli/output.rb', line 44 def puts_no_prefix(*objects) @io.puts(*objects) end |