Class: Sus::Output::Text

Inherits:
Object
  • Object
show all
Includes:
Messages
Defined in:
lib/sus/output/text.rb

Direct Known Subclasses

XTerm

Constant Summary collapse

INDENTATION =
"\t"

Constants included from Messages

Messages::FAILED_PREFIX, Messages::PASSED_PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Messages

#assert, #error, #error_prefix, #fail_prefix, #inform, #inform_prefix, #pass_prefix, #skip, #skip_prefix

Constructor Details

#initialize(io) ⇒ Text

Returns a new instance of Text.



14
15
16
17
18
19
20
21
# File 'lib/sus/output/text.rb', line 14

def initialize(io)
	@io = io
	
	@styles = {reset: self.reset}
	
	@indent = String.new
	@styles[:indent] = @indent
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



35
36
37
# File 'lib/sus/output/text.rb', line 35

def io
  @io
end

#stylesObject (readonly)

Returns the value of attribute styles.



23
24
25
# File 'lib/sus/output/text.rb', line 23

def styles
  @styles
end

Instance Method Details

#[](key) ⇒ Object



58
59
60
# File 'lib/sus/output/text.rb', line 58

def [] key
	@styles[key]
end

#[]=(key, value) ⇒ Object



62
63
64
# File 'lib/sus/output/text.rb', line 62

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

#append(buffer) ⇒ Object



29
30
31
32
33
# File 'lib/sus/output/text.rb', line 29

def append(buffer)
	buffer.each do |operation|
		self.public_send(*operation)
	end
end

#bufferedObject



25
26
27
# File 'lib/sus/output/text.rb', line 25

def buffered
	Buffered.new(self)
end

#colors?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/sus/output/text.rb', line 74

def colors?
	false
end

#indentObject



39
40
41
# File 'lib/sus/output/text.rb', line 39

def indent
	@indent << INDENTATION
end

#indentedObject



47
48
49
50
51
52
# File 'lib/sus/output/text.rb', line 47

def indented
	self.indent
	yield
ensure
	self.outdent
end

#interactive?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/sus/output/text.rb', line 54

def interactive?
	@io.tty?
end

#outdentObject



43
44
45
# File 'lib/sus/output/text.rb', line 43

def outdent
	@indent.slice!(INDENTATION)
end

#puts(*arguments) ⇒ Object

Print out the arguments as per #print, followed by the reset sequence and a newline.



106
107
108
109
# File 'lib/sus/output/text.rb', line 106

def puts(*arguments)
	write(*arguments)
	@io.puts(self.reset)
end

#resetObject



81
82
# File 'lib/sus/output/text.rb', line 81

def reset
end

#sizeObject



66
67
68
# File 'lib/sus/output/text.rb', line 66

def size
	[24, 80]
end

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



78
79
# File 'lib/sus/output/text.rb', line 78

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

#widthObject



70
71
72
# File 'lib/sus/output/text.rb', line 70

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.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sus/output/text.rb', line 88

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