Class: TestBench::Output::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/output/writer.rb,
lib/test_bench/output/writer/style.rb,
lib/test_bench/output/writer/substitute.rb

Direct Known Subclasses

Substitute::Writer

Defined Under Namespace

Modules: Defaults, Style, Substitute

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#deviceObject



4
5
6
# File 'lib/test_bench/output/writer.rb', line 4

def device
  @device ||= Device::Substitute.build
end

#indentation_depthObject



12
13
14
# File 'lib/test_bench/output/writer.rb', line 12

def indentation_depth
  @indentation_depth ||= 0
end

#stylingObject

Returns the value of attribute styling.



9
10
11
# File 'lib/test_bench/output/writer.rb', line 9

def styling
  @styling
end

Class Method Details

.build(styling: nil, device: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/test_bench/output/writer.rb', line 17

def self.build(styling: nil, device: nil)
  instance = new

  Device.configure(instance, device:)

  device = instance.device

  styling = styling(styling, device)
  instance.styling = styling

  instance
end

.configure(receiver, styling: nil, device: nil, attr_name: nil) ⇒ Object



30
31
32
33
34
35
# File 'lib/test_bench/output/writer.rb', line 30

def self.configure(receiver, styling: nil, device: nil, attr_name: nil)
  attr_name ||= :writer

  instance = build(styling:, device:)
  receiver.public_send(:"#{attr_name}=", instance)
end

.styling(styling_or_styling_policy, device) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/test_bench/output/writer.rb', line 37

def self.styling(styling_or_styling_policy, device)
  case styling_or_styling_policy
  in true | false => styling
    return styling
  in :detect | :on | :off => styling_policy
  in nil
    styling_policy = Defaults.styling_policy
  end

  case styling_policy
  in :detect
    device.tty?
  in :on
    true
  in :off
    false
  end
end

Instance Method Details

#decrease_indentationObject



102
103
104
# File 'lib/test_bench/output/writer.rb', line 102

def decrease_indentation
  self.indentation_depth -= 1
end

#increase_indentationObject



98
99
100
# File 'lib/test_bench/output/writer.rb', line 98

def increase_indentation
  self.indentation_depth += 1
end

#indentObject



56
57
58
59
60
# File 'lib/test_bench/output/writer.rb', line 56

def indent
  indentation_width = 2 * indentation_depth

  print(' ' * indentation_width)
end


88
89
90
91
92
# File 'lib/test_bench/output/writer.rb', line 88

def print(text)
  write(text)

  self
end

#puts(text = nil) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/test_bench/output/writer.rb', line 62

def puts(text=nil)
  if not text.nil?
    print(text.chomp)
  end

  style(:reset)

  print("\n")
end

#style(style, *additional_styles) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/test_bench/output/writer.rb', line 72

def style(style, *additional_styles)
  styles = [style, *additional_styles]

  control_codes = styles.map do |style|
    Style.control_code(style)
  end

  if styling?
    control_sequence = "\e[#{control_codes.join(';')}m"

    print(control_sequence)
  end

  self
end

#styling?Object

Returns the value of attribute styling.



10
11
12
# File 'lib/test_bench/output/writer.rb', line 10

def styling
  @styling
end

#write(text) ⇒ Object



94
95
96
# File 'lib/test_bench/output/writer.rb', line 94

def write(text)
  device.write(text)
end