Class: Console::Terminal::Text
- Inherits:
-
Object
- Object
- Console::Terminal::Text
- Defined in:
- lib/console/terminal/text.rb
Direct Known Subclasses
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #colors? ⇒ Boolean
-
#initialize(output) ⇒ Text
constructor
A new instance of Text.
-
#print(*arguments) ⇒ Object
Print out the given arguments.
-
#print_line(*arguments) ⇒ Object
Print out the arguments as per #print, followed by the reset sequence and a newline.
- #puts(*arguments, style: nil) ⇒ Object
- #reset ⇒ Object
- #style(foreground, background = nil, *attributes) ⇒ Object
- #write(*arguments, style: nil) ⇒ Object
Constructor Details
#initialize(output) ⇒ Text
Returns a new instance of Text.
12 13 14 15 |
# File 'lib/console/terminal/text.rb', line 12 def initialize(output) @output = output @styles = {reset: self.reset} end |
Instance Method Details
#[](key) ⇒ Object
17 18 19 |
# File 'lib/console/terminal/text.rb', line 17 def [] key @styles[key] end |
#[]=(key, value) ⇒ Object
21 22 23 |
# File 'lib/console/terminal/text.rb', line 21 def []= key, value @styles[key] = value end |
#colors? ⇒ Boolean
25 26 27 |
# File 'lib/console/terminal/text.rb', line 25 def colors? false end |
#print(*arguments) ⇒ Object
Print out the given arguments. When the argument is a symbol, look up the style and inject it into the output 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 output.
59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/console/terminal/text.rb', line 59 def print(*arguments) arguments.each do |argument| case argument when Symbol @output.write(self[argument]) when Proc argument.call(self) else @output.write(argument) end end end |
#print_line(*arguments) ⇒ Object
Print out the arguments as per #print, followed by the reset sequence and a newline.
73 74 75 76 |
# File 'lib/console/terminal/text.rb', line 73 def print_line(*arguments) print(*arguments) @output.puts(self.reset) end |
#puts(*arguments, style: nil) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# File 'lib/console/terminal/text.rb', line 45 def puts(*arguments, style: nil) if style and prefix = self[style] @output.write(prefix) @output.puts(*arguments) @output.write(self.reset) else @output.puts(*arguments) end end |
#reset ⇒ Object
32 33 |
# File 'lib/console/terminal/text.rb', line 32 def reset end |
#style(foreground, background = nil, *attributes) ⇒ Object
29 30 |
# File 'lib/console/terminal/text.rb', line 29 def style(foreground, background = nil, *attributes) end |
#write(*arguments, style: nil) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/console/terminal/text.rb', line 35 def write(*arguments, style: nil) if style and prefix = self[style] @output.write(prefix) @output.write(*arguments) @output.write(self.reset) else @output.write(*arguments) end end |