Class: Sus::Output::Text
- Inherits:
-
Object
- Object
- Sus::Output::Text
- Includes:
- Messages
- Defined in:
- lib/sus/output/text.rb
Overview
Represents a plain text output handler without color support.
Direct Known Subclasses
Constant Summary collapse
- INDENTATION =
The indentation string.
"\t"
Constants included from Messages
Messages::FAILED_PREFIX, Messages::PASSED_PREFIX
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#styles ⇒ Object
readonly
Returns the value of attribute styles.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get a style by key.
-
#[]=(key, value) ⇒ Object
Set a style by key.
-
#append(buffer) ⇒ Object
Append and replay chunks from a buffer.
-
#buffered ⇒ Object
Create a buffered output handler.
- #colors? ⇒ Boolean
-
#indent ⇒ Object
Increase indentation level.
-
#indented ⇒ Object
Execute a block with increased indentation.
-
#initialize(io) ⇒ Text
constructor
Initialize a new Text output handler.
- #interactive? ⇒ Boolean
-
#outdent ⇒ Object
Decrease indentation level.
-
#puts(*arguments) ⇒ Object
Print out the arguments as per #write, followed by the reset sequence and a newline.
- #reset ⇒ Object
- #size ⇒ Object
-
#style(foreground, background = nil, *attributes) ⇒ Object
Create a style string (no-op for Text output).
- #The IO object to write to.=(IOobjecttowriteto. = (value)) ⇒ Object
- #The style definitions.=(styledefinitions. = (value)) ⇒ Object
- #width ⇒ Object
-
#write(*arguments) ⇒ Object
Print out the given arguments.
Methods included from Messages
#assert, #error, #error_prefix, #fail_prefix, #inform, #inform_prefix, #pass_prefix, #skip, #skip_prefix
Constructor Details
#initialize(io) ⇒ Text
Initialize a new Text output handler.
17 18 19 20 21 22 23 24 |
# File 'lib/sus/output/text.rb', line 17 def initialize(io) @io = io @styles = {reset: self.reset} @indent = String.new @styles[:indent] = @indent end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
44 45 46 |
# File 'lib/sus/output/text.rb', line 44 def io @io end |
#styles ⇒ Object (readonly)
Returns the value of attribute styles.
27 28 29 |
# File 'lib/sus/output/text.rb', line 27 def styles @styles end |
Instance Method Details
#[](key) ⇒ Object
Get a style by key.
76 77 78 |
# File 'lib/sus/output/text.rb', line 76 def [] key @styles[key] end |
#[]=(key, value) ⇒ Object
Set a style by key.
83 84 85 |
# File 'lib/sus/output/text.rb', line 83 def []= key, value @styles[key] = value end |
#append(buffer) ⇒ Object
Append and replay chunks from a buffer.
37 38 39 40 41 |
# File 'lib/sus/output/text.rb', line 37 def append(buffer) buffer.each do |operation| self.public_send(*operation) end end |
#buffered ⇒ Object
Create a buffered output handler.
31 32 33 |
# File 'lib/sus/output/text.rb', line 31 def buffered Buffered.new(self) end |
#colors? ⇒ Boolean
98 99 100 |
# File 'lib/sus/output/text.rb', line 98 def colors? false end |
#indent ⇒ Object
Increase indentation level.
50 51 52 |
# File 'lib/sus/output/text.rb', line 50 def indent @indent << INDENTATION end |
#indented ⇒ Object
Execute a block with increased indentation.
61 62 63 64 65 66 |
# File 'lib/sus/output/text.rb', line 61 def indented self.indent yield ensure self.outdent end |
#interactive? ⇒ Boolean
69 70 71 |
# File 'lib/sus/output/text.rb', line 69 def interactive? @io.tty? end |
#outdent ⇒ Object
Decrease indentation level.
55 56 57 |
# File 'lib/sus/output/text.rb', line 55 def outdent @indent.slice!(INDENTATION) end |
#puts(*arguments) ⇒ Object
Print out the arguments as per #write, followed by the reset sequence and a newline.
138 139 140 141 |
# File 'lib/sus/output/text.rb', line 138 def puts(*arguments) write(*arguments) @io.puts(self.reset) end |
#reset ⇒ Object
111 112 |
# File 'lib/sus/output/text.rb', line 111 def reset end |
#size ⇒ Object
88 89 90 |
# File 'lib/sus/output/text.rb', line 88 def size [24, 80] end |
#style(foreground, background = nil, *attributes) ⇒ Object
Create a style string (no-op for Text output).
107 108 |
# File 'lib/sus/output/text.rb', line 107 def style(foreground, background = nil, *attributes) end |
#The IO object to write to.=(IOobjecttowriteto. = (value)) ⇒ Object
44 |
# File 'lib/sus/output/text.rb', line 44 attr :io |
#The style definitions.=(styledefinitions. = (value)) ⇒ Object
27 |
# File 'lib/sus/output/text.rb', line 27 attr :styles |
#width ⇒ Object
93 94 95 |
# File 'lib/sus/output/text.rb', line 93 def width size.last end |
#write(*arguments) ⇒ Object
Print out the given arguments. When the argument is a symbol, look up the style and inject it into the io stream. When the argument is a proc/lambda, call it with self as the argument. When the argument is anything else, write it directly to the io.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/sus/output/text.rb', line 119 def write(*arguments) arguments.each do |argument| case argument when Symbol @io.write(self[argument]) when Proc argument.call(self) else if argument.respond_to?(:print) argument.print(self) else @io.write(argument) end end end end |