Class: TestBench::Output::Writer
- Inherits:
-
Object
- Object
- TestBench::Output::Writer
show all
- Defined in:
- lib/test_bench/output/writer.rb,
lib/test_bench/output/writer/sgr.rb,
lib/test_bench/output/writer/dependency.rb,
lib/test_bench/output/writer/substitute.rb
Defined Under Namespace
Modules: Defaults, Dependency, Mode, SGR, Substitute
Constant Summary
collapse
- Error =
Class.new(RuntimeError)
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Instance Attribute Details
#byte_offset ⇒ Object
26
27
28
|
# File 'lib/test_bench/output/writer.rb', line 26
def byte_offset
@byte_offset ||= 0
end
|
#device ⇒ Object
6
7
8
|
# File 'lib/test_bench/output/writer.rb', line 6
def device
@device ||= StringIO.new
end
|
#indentation_depth ⇒ Object
31
32
33
|
# File 'lib/test_bench/output/writer.rb', line 31
def indentation_depth
@indentation_depth ||= 0
end
|
#mode ⇒ Object
21
22
23
|
# File 'lib/test_bench/output/writer.rb', line 21
def mode
@mode ||= Mode.text
end
|
#styling ⇒ Object
Returns the value of attribute styling.
11
12
13
|
# File 'lib/test_bench/output/writer.rb', line 11
def styling
@styling
end
|
#styling_enabled ⇒ Object
Also known as:
styling?
13
14
15
16
17
|
# File 'lib/test_bench/output/writer.rb', line 13
def styling_enabled
instance_variable_defined?(:@styling_enabled) ?
@styling_enabled :
@styling_enabled = self.class.styling?(device, styling)
end
|
Class Method Details
.assure_styling_setting(styling_setting) ⇒ Object
147
148
149
150
151
|
# File 'lib/test_bench/output/writer.rb', line 147
def self.assure_styling_setting(styling_setting)
unless styling_settings.include?(styling_setting)
raise Error, "Invalid output styling #{styling_setting.inspect} (Valid values: #{styling_settings.map(&:inspect).join(', ')})"
end
end
|
.build(device = nil, styling: nil) ⇒ Object
47
48
49
50
51
|
# File 'lib/test_bench/output/writer.rb', line 47
def self.build(device=nil, styling: nil)
instance = new
instance.configure(device: device, styling: styling)
instance
end
|
53
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/test_bench/output/writer.rb', line 53
def self.configure(receiver, writer: nil, styling: nil, device: nil, attr_name: nil)
attr_name ||= :writer
if writer.nil?
instance = build(device, styling: styling)
else
instance = writer
end
receiver.public_send(:"#{attr_name}=", instance)
instance
end
|
.default_styling_setting ⇒ Object
161
162
163
|
# File 'lib/test_bench/output/writer.rb', line 161
def self.default_styling_setting
styling_settings.fetch(0)
end
|
.styling?(device, styling_setting = nil) ⇒ Boolean
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
# File 'lib/test_bench/output/writer.rb', line 132
def self.styling?(device, styling_setting=nil)
styling_setting ||= Defaults.styling
assure_styling_setting(styling_setting)
case styling_setting
when :detect
device.tty?
when :on
true
when :off
false
end
end
|
.styling_settings ⇒ Object
153
154
155
156
157
158
159
|
# File 'lib/test_bench/output/writer.rb', line 153
def self.styling_settings
[
:detect,
:on,
:off
]
end
|
Instance Method Details
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/test_bench/output/writer.rb', line 36
def configure(styling: nil, device: nil)
device ||= Defaults.device
unless styling.nil?
self.class.assure_styling_setting(styling)
end
self.device = device
self.styling = styling
end
|
#current?(byte_offset) ⇒ Boolean
120
121
122
|
# File 'lib/test_bench/output/writer.rb', line 120
def current?(byte_offset)
byte_offset >= self.byte_offset
end
|
#decrease_indentation ⇒ Object
128
129
130
|
# File 'lib/test_bench/output/writer.rb', line 128
def decrease_indentation
self.indentation_depth -= 1
end
|
#escape_code(id) ⇒ Object
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/test_bench/output/writer.rb', line 79
def escape_code(id)
escape_code = SGR.escape_code(id)
if mode == Mode.text
return self unless styling?
self.mode = Mode.escape_sequence
write("\e[")
else
write(';')
end
write(escape_code)
self
end
|
#increase_indentation ⇒ Object
124
125
126
|
# File 'lib/test_bench/output/writer.rb', line 124
def increase_indentation
self.indentation_depth += 1
end
|
#indent ⇒ Object
101
102
103
104
105
|
# File 'lib/test_bench/output/writer.rb', line 101
def indent
indentation = ' ' * indentation_depth
text(indentation)
end
|
#newline ⇒ Object
107
108
109
110
111
112
|
# File 'lib/test_bench/output/writer.rb', line 107
def newline
sync
device.puts('')
self.byte_offset += 1
self
end
|
#sync ⇒ Object
97
98
99
|
# File 'lib/test_bench/output/writer.rb', line 97
def sync
text('')
end
|
#text(text) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/test_bench/output/writer.rb', line 67
def text(text)
if mode == Mode.escape_sequence
self.mode = Mode.text
write('m')
end
write(text)
self
end
|
#write(data) ⇒ Object
114
115
116
117
118
|
# File 'lib/test_bench/output/writer.rb', line 114
def write(data)
bytes_written = device.write(data)
self.byte_offset += bytes_written
end
|