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.



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

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

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



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

def stream
  @stream
end

Instance Method Details

#[](key) ⇒ Object

Get the style associated with the given key.



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

def [] key
	@styles[key]
end

#[]=(key, value) ⇒ Object

Set the style associated with the given key.



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

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

#colors?Boolean

Returns:

  • (Boolean)


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

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.



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/console/terminal/text.rb', line 102

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.



118
119
120
121
# File 'lib/console/terminal/text.rb', line 118

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.



85
86
87
88
89
90
91
92
93
# File 'lib/console/terminal/text.rb', line 85

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

#resetObject

Generate a reset sequence.



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

def reset
end

#sizeObject



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

def size
	[24, 80]
end

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

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



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

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

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



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

attr :stream

#widthObject



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

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.



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

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