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

Inherits:
Struct
  • Object
show all
Defined in:
lib/test_bench/output/writer/buffer/console.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



83
84
85
# File 'lib/test_bench/output/writer/buffer/console.rb', line 83

def column
  @column
end

#heightObject

Returns the value of attribute height

Returns:

  • (Object)

    the current value of height



83
84
85
# File 'lib/test_bench/output/writer/buffer/console.rb', line 83

def height
  @height
end

#rowObject

Returns the value of attribute row

Returns:

  • (Object)

    the current value of row



83
84
85
# File 'lib/test_bench/output/writer/buffer/console.rb', line 83

def row
  @row
end

#widthObject

Returns the value of attribute width

Returns:

  • (Object)

    the current value of width



83
84
85
# File 'lib/test_bench/output/writer/buffer/console.rb', line 83

def width
  @width
end

Class Method Details

.getObject



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/test_bench/output/writer/buffer/console.rb', line 84

def self.get
  instance = new

  STDIN.raw do |stdin|
    instance.height, instance.width = stdin.winsize

    instance.row, instance.column = stdin.cursor
  end

  instance
end

Instance Method Details

#bottom_row?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/test_bench/output/writer/buffer/console.rb', line 96

def bottom_row?
  row + 1 == height && column == 0
end

#capacityObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/test_bench/output/writer/buffer/console.rb', line 120

def capacity
  capacity = 0

  rows_remaining = height - row - 1

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

    final_row = width - column
    capacity += final_row
  end

  capacity
end

#next(text) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/test_bench/output/writer/buffer/console.rb', line 100

def next(text)
  write_ahead_text = text.slice!(0, capacity)

  next!(write_ahead_text)

  write_ahead_text
end

#next!(text) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/test_bench/output/writer/buffer/console.rb', line 108

def next!(text)
  escape_sequence_pattern = %r{\A\e\[[[:digit:]]+(?:;[[:digit:]]+)*[[:alpha:]]$}
  if escape_sequence_pattern.match?(text)
    return
  end

  row, column, _scroll_rows = next_position(text)

  self.row = row
  self.column = column
end

#next_position(text) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/test_bench/output/writer/buffer/console.rb', line 135

def next_position(text)
  text_length = text.length

  newline = text.end_with?("\n")
  if newline
    text_length -= 1
  end

  row = self.row
  column = self.column

  text_rows, text_columns = text_length.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 newline
    reached_next_line = column == 0 && row > self.row

    newline_needed = !reached_next_line
    if newline_needed
      row += 1
      column = 0
    end
  end

  if row >= height
    row_limit = height - 1

    scroll_rows = row - row_limit
    row = row_limit
  else
    scroll_rows = 0
  end

  return row, column, scroll_rows
end