Class: Console::Terminal::Text

Inherits:
Object
  • Object
show all
Defined in:
lib/console/terminal/text.rb

Overview

A simple text-based terminal output.

Direct Known Subclasses

XTerm

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stream) ⇒ Text

Create a new text terminal output.



17
18
19
20
# File 'lib/console/terminal/text.rb', line 17

def initialize(stream)
  @stream = stream
  @styles = {reset: self.reset}
end

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



23
24
25
# File 'lib/console/terminal/text.rb', line 23

def stream
  @stream
end

Instance Method Details

#[](key) ⇒ Object

Get the style associated with the given key.



29
30
31
# File 'lib/console/terminal/text.rb', line 29

def [] key
  @styles[key]
end

#[]=(key, value) ⇒ Object

Set the style associated with the given key.



37
38
39
# File 'lib/console/terminal/text.rb', line 37

def []= key, value
  @styles[key] = value
end

#colors?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/console/terminal/text.rb', line 42

def colors?
  false
end

Print rich text to the output stream.

  • 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.



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/console/terminal/text.rb', line 107

def print(*arguments)
  arguments.each do |argument|
    case argument
    when Symbol
      @stream.write(self[argument])
    when Proc
      argument.call(self)
    else
      @stream.write(argument)
    end
  end
end

Print rich text to the output stream, followed by the reset sequence and a newline.



123
124
125
126
# File 'lib/console/terminal/text.rb', line 123

def print_line(*arguments)
  print(*arguments)
  @stream.puts(self.reset)
end

#puts(*arguments, style: nil) ⇒ Object

Write the given arguments to the output stream using the given style. The reset sequence is automatically appended at the end of each line.



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/console/terminal/text.rb', line 87

def puts(*arguments, style: nil)
  if style and prefix = self[style]
    arguments.each do |argument|
      argument.to_s.lines.each do |line|
        @stream.write(prefix, line.chomp)
        @stream.puts(self.reset)
      end
    end
  else
    @stream.puts(*arguments)
  end
end

#resetObject

Generate a reset sequence.



65
66
# File 'lib/console/terminal/text.rb', line 65

def reset
end

#sizeObject



47
48
49
# File 'lib/console/terminal/text.rb', line 47

def size
  [24, 80]
end

#style(foreground, background = nil, *attributes) ⇒ Object

Generate a style string for the given foreground, background, and attributes.



59
60
# File 'lib/console/terminal/text.rb', line 59

def style(foreground, background = nil, *attributes)
end

#The stream to write to.=(streamtowriteto. = (value)) ⇒ Object



23
# File 'lib/console/terminal/text.rb', line 23

attr :stream

#widthObject



52
53
54
# File 'lib/console/terminal/text.rb', line 52

def width
  self.size.last
end

#write(*arguments, style: nil) ⇒ Object

Write the given arguments to the output stream using the given style. The reset sequence is automatically appended.



72
73
74
75
76
77
78
79
80
# File 'lib/console/terminal/text.rb', line 72

def write(*arguments, style: nil)
  if style and prefix = self[style]
    @stream.write(prefix)
    @stream.write(*arguments)
    @stream.write(self.reset)
  else
    @stream.write(*arguments)
  end
end