Module: TestBench::Bootstrap::Output

Extended by:
Output
Included in:
Output
Defined in:
lib/test_bench/bootstrap.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#indentation ⇒ Object



223
224
225
# File 'lib/test_bench/bootstrap.rb', line 223

def indentation
  @indentation ||= 0
end

Instance Method Details

#device ⇒ Object



228
229
230
# File 'lib/test_bench/bootstrap.rb', line 228

def device
  @device ||= Defaults.output_device
end

#indent(text, device: nil, sgr_code: nil, sgr_codes: nil, tab_indent: nil, &block) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/test_bench/bootstrap.rb', line 186

def indent(text, device: nil, sgr_code: nil, sgr_codes: nil, tab_indent: nil, &block)
  unless text.nil?
    sgr_codes ||= []
    unless sgr_code.nil?
      sgr_codes << sgr_code
    end

    unless sgr_codes.empty?
      sgr_codes.map! do |sgr_code|
        sgr_code.to_s(16)
      end

      text = "\e[#{sgr_codes.join(';')}m#{text}\e[0m"
    end

    text = "#{"\t" if tab_indent}#{'  ' * indentation}#{text}\n"

    raw_write(text, device)
  end

  return if block.nil?

  self.indentation += 1 unless text.nil?

  begin
    block.()
  ensure
    self.indentation -= 1 unless text.nil?
  end
end

#newline ⇒ Object



182
183
184
# File 'lib/test_bench/bootstrap.rb', line 182

def newline
  write('')
end

#raw_write(text, device = nil) ⇒ Object



217
218
219
220
221
# File 'lib/test_bench/bootstrap.rb', line 217

def raw_write(text, device=nil)
  device ||= self.device

  device.write(text)
end

#write(text, device: nil, sgr_code: nil, sgr_codes: nil, tab_indent: nil) ⇒ Object



178
179
180
# File 'lib/test_bench/bootstrap.rb', line 178

def write(text, device: nil, sgr_code: nil, sgr_codes: nil, tab_indent: nil)
  indent(text, device: device, sgr_code: sgr_code, sgr_codes: sgr_codes, tab_indent: tab_indent)
end