Class: TestBench::Output::Writer::Buffer::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/test_bench/output/writer/buffer/console.rb

Defined Under Namespace

Classes: Geometry

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cursor_savedObject Also known as: cursor_saved?



16
17
18
# File 'lib/test_bench/output/writer/buffer/console.rb', line 16

def cursor_saved
  @cursor_saved ||= false
end

#deviceObject



6
7
8
# File 'lib/test_bench/output/writer/buffer/console.rb', line 6

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

#geometryObject



11
12
13
# File 'lib/test_bench/output/writer/buffer/console.rb', line 11

def geometry
  @geometry ||= Geometry.get
end

Class Method Details

.build(device = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/test_bench/output/writer/buffer/console.rb', line 22

def self.build(device=nil)
  device ||= Defaults.device

  instance = new

  if device.tty?
    instance.device = device
  end

  instance
end

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



34
35
36
37
38
39
# File 'lib/test_bench/output/writer/buffer/console.rb', line 34

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

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

Instance Method Details

#capacityObject



75
76
77
# File 'lib/test_bench/output/writer/buffer/console.rb', line 75

def capacity
  geometry.capacity
end

#flushObject



68
69
70
71
72
73
# File 'lib/test_bench/output/writer/buffer/console.rb', line 68

def flush(...)
  if cursor_saved?
    device.write("\e[u")
    self.cursor_saved = false
  end
end

#next_position(text) ⇒ Object



79
80
81
# File 'lib/test_bench/output/writer/buffer/console.rb', line 79

def next_position(text)
  geometry.next_position(text)
end

#receive(text) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/test_bench/output/writer/buffer/console.rb', line 46

def receive(text)
  if not cursor_saved?
    device.write("\e[s")
    self.cursor_saved = true
  end

  write_ahead_text = geometry.next(text)

  if not write_ahead_text.empty?
    device.write(write_ahead_text)
  elsif geometry.bottom_row?
    buffering_message = "Output is buffering"

    device.write("\e[2m#{buffering_message}\e[22m")
    geometry.next!(buffering_message)
  end

  device.flush

  write_ahead_text.length
end

#set_geometry(width, height, row, column) ⇒ Object



41
42
43
44
# File 'lib/test_bench/output/writer/buffer/console.rb', line 41

def set_geometry(width, height, row, column)
  geometry = Geometry.new(width, height, row, column)
  self.geometry = geometry
end