Class: TestBench::Session::Output::Writer::Buffer::Interactive::Viewport

Inherits:
Struct
  • Object
show all
Defined in:
lib/test_bench/session/output/writer/buffer/interactive/viewport.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnObject

Returns the value of attribute column

Returns:

  • (Object)

    the current value of column



7
8
9
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 7

def column
  @column
end

#heightObject

Returns the value of attribute height

Returns:

  • (Object)

    the current value of height



7
8
9
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 7

def height
  @height
end

#rowObject

Returns the value of attribute row

Returns:

  • (Object)

    the current value of row



7
8
9
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 7

def row
  @row
end

#rows_scrolledObject

Returns the value of attribute rows_scrolled

Returns:

  • (Object)

    the current value of rows_scrolled



7
8
9
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 7

def rows_scrolled
  @rows_scrolled
end

#scroll_rowsObject

Returns the value of attribute scroll_rows

Returns:

  • (Object)

    the current value of scroll_rows



7
8
9
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 7

def scroll_rows
  @scroll_rows
end

#widthObject

Returns the value of attribute width

Returns:

  • (Object)

    the current value of width



7
8
9
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 7

def width
  @width
end

Class Method Details

.build(width, height, row, column, scroll_rows = nil) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 8

def self.build(width, height, row, column, scroll_rows=nil)
  scroll_rows ||= 0

  rows_scrolled = 0

  new(width, height, row, column, scroll_rows, rows_scrolled)
end

.escape_sequence_patternObject



152
153
154
155
156
157
158
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 152

def self.escape_sequence_pattern
  initiator = %r{\e\[}
  terminator = %r{[[:alpha:]]}
  sequence = %r{[[:digit:]]+(?:;[[:digit:]]+)*}

  %r{#{initiator}#{sequence}?#{terminator}}
end

.getObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 20

def self.get
  width, height, row, column = nil

  Kernel.require 'io/console'
  STDIN.raw do |stdin|
    height, width = stdin.winsize

    row, column = stdin.cursor
  end

  scroll_rows = row

  build(width, height, row, column, scroll_rows)
end

.nullObject



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

def self.null
  build(0, 0, 0, 0)
end

Instance Method Details

#bottom_row?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 148

def bottom_row?
  row == height - 1
end

#capacityObject



129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 129

def capacity
  capacity = 0

  rows_remaining = height + scroll_rows_remaining - row - 1

  if rows_remaining > 0
    capacity += (rows_remaining - 1) * width

    final_row = width - column
    capacity += final_row
  end

  capacity
end

#capacity?Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 121

def capacity?
  if scroll_rows_remaining > 0
    true
  else
    not bottom_row?
  end
end

#scroll_rows_remainingObject



144
145
146
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 144

def scroll_rows_remaining
  scroll_rows - rows_scrolled
end

#write(text) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 35

def write(text)
  bytes_written = 0

  escape_sequence_pattern = self.class.escape_sequence_pattern

  until text.empty?
    write_text, escape_sequence, text = text.partition(escape_sequence_pattern)

    bytes_written += write!(write_text)
    bytes_written += escape_sequence.bytesize
  end

  bytes_written
end

#write!(text) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 50

def write!(text)
  newline = text.end_with?("\n")

  if newline
    text = text[0...-1]
  end

  bytes_written = write_text(text)

  if newline
    bytes_written += write_newline
  end

  bytes_written
end

#write_newlineObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 105

def write_newline
  if bottom_row?
    if scroll_rows_remaining.zero?
      return 0
    end

    self.rows_scrolled += 1
  else
    self.row += 1
  end

  self.column = 0

  1
end

#write_text(text) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/test_bench/session/output/writer/buffer/interactive/viewport.rb', line 66

def write_text(text)
  if text.start_with?("\e")
    return text.bytesize
  end

  written_text = text[0...capacity]

  bytes_written = written_text.bytesize

  row = self.row
  column = self.column

  text_rows, text_columns = bytes_written.divmod(width)

  row += text_rows

  columns_remaining = width - column
  if columns_remaining > text_columns
    column += text_columns
  else
    row += 1
    column = text_columns - columns_remaining
  end

  if row >= height
    final_row = height - 1

    scroll_rows = row - final_row
    self.rows_scrolled += scroll_rows

    row = final_row
  end

  self.row = row
  self.column = column

  bytes_written
end