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/buffer.rb,
lib/test_bench/output/writer/defaults.rb,
lib/test_bench/output/writer/substitute.rb

Direct Known Subclasses

Substitute::Writer

Defined Under Namespace

Modules: Defaults, Style, Styling, Substitute Classes: Buffer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bufferObject



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

def buffer
  @buffer ||= Buffer.new
end

#column_sequenceObject



25
26
27
# File 'lib/test_bench/output/writer.rb', line 25

def column_sequence
  @column_sequence ||= 0
end

#deviceObject



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

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

#digestObject



15
16
17
# File 'lib/test_bench/output/writer.rb', line 15

def digest
  @digest ||= Digest.new
end

#sequenceObject



20
21
22
# File 'lib/test_bench/output/writer.rb', line 20

def sequence
  @sequence ||= 0
end

#styling_policyObject Also known as: styling



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

def styling_policy
  @styling_policy ||= Styling.default
end

#ttyObject Also known as: tty?



69
70
71
# File 'lib/test_bench/output/writer.rb', line 69

def tty
  @tty.nil? ? @tty = device_tty? : @tty
end

Class Method Details

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



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

def self.build(device=nil, styling: nil, inert_digest: nil)
  device ||= Defaults.device

  instance = new
  instance.device = device
  instance.styling_policy = styling

  Digest.configure(instance, inert: inert_digest)

  instance.configure

  instance
end

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



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/test_bench/output/writer.rb', line 53

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

  if not writer.nil?
    instance = writer
  else
    instance = build(device, styling:, inert_digest:)
  end

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

Instance Method Details

#configureObject



35
36
37
# File 'lib/test_bench/output/writer.rb', line 35

def configure
  Buffer.configure(self)
end

#current?(sequence) ⇒ Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/test_bench/output/writer.rb', line 162

def current?(sequence)
  sequence >= self.sequence
end

#device_tty?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/test_bench/output/writer.rb', line 138

def device_tty?
  device.tty?
end

#flushObject



142
143
144
# File 'lib/test_bench/output/writer.rb', line 142

def flush
  buffer.flush(device)
end


109
110
111
112
113
114
115
# File 'lib/test_bench/output/writer.rb', line 109

def print(text)
  self.column_sequence += text.length

  write(text)

  self
end

#puts(text = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/test_bench/output/writer.rb', line 75

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

    print(text)
  end

  style(:reset)

  if tty?
    write("\e[0K")
  end

  write("\n")

  self.column_sequence = 0
end

#style(style, *additional_styles) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/test_bench/output/writer.rb', line 93

def style(style, *additional_styles)
  control_code = Style.control_code(style)
  control_codes = [control_code]

  additional_styles.each do |style|
    control_code = Style.control_code(style)
    control_codes << control_code
  end

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

  self
end

#styling?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/test_bench/output/writer.rb', line 166

def styling?
  Styling.styling?(styling_policy, tty?)
end

#syncObject



65
66
67
# File 'lib/test_bench/output/writer.rb', line 65

def sync
  @sync.nil? ? @sync = true : @sync
end

#sync=(sync) ⇒ Object



146
147
148
149
150
151
152
# File 'lib/test_bench/output/writer.rb', line 146

def sync=(sync)
  @sync = sync

  if sync
    flush
  end
end

#write(data) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/test_bench/output/writer.rb', line 117

def write(data)
  if sync
    bytes_written = write!(data)
  else
    bytes_written = buffer.receive(data)
  end

  self.sequence += bytes_written

  data = data[0...bytes_written]
  digest.update(data)

  bytes_written
end

#write!(data) ⇒ Object



132
133
134
135
136
# File 'lib/test_bench/output/writer.rb', line 132

def write!(data)
  device.write(data)

  data.bytesize
end

#written?(data = nil) ⇒ Boolean

Returns:

  • (Boolean)


154
155
156
157
158
159
160
# File 'lib/test_bench/output/writer.rb', line 154

def written?(data=nil)
  if data.nil?
    sequence > 0
  else
    digest.digest?(data)
  end
end