Class: Termplot::Window

Inherits:
Object
  • Object
show all
Defined in:
lib/termplot/window.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cols:, rows:) ⇒ Window

Returns a new instance of Window.



7
8
9
10
11
# File 'lib/termplot/window.rb', line 7

def initialize(cols:, rows:)
  @rows = rows
  @cols = cols
  @buffer = Array.new(cols * rows) { CharacterMap::DEFAULT[:empty] }
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



6
7
8
# File 'lib/termplot/window.rb', line 6

def buffer
  @buffer
end

#colsObject (readonly)

Returns the value of attribute cols.



6
7
8
# File 'lib/termplot/window.rb', line 6

def cols
  @cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



6
7
8
# File 'lib/termplot/window.rb', line 6

def rows
  @rows
end

Instance Method Details

#blit(other, start_row, start_col) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/termplot/window.rb', line 71

def blit(other, start_row, start_col)
  cursor.position = start_row * cols + start_col

  other.each_row do |row|
    row.each do |char|
      write(char)
    end
    cursor.down unless cursor.beginning_of_line?
    cursor.col = start_col
  end
end

#clearObject



33
34
35
36
# File 'lib/termplot/window.rb', line 33

def clear
  cursor.reset_position
  size.times { write CharacterMap::DEFAULT[:empty] }
end

#console_cursorObject



17
18
19
20
21
22
# File 'lib/termplot/window.rb', line 17

def console_cursor
  # Console buffer has an extra rows - 1 to account for new line characters
  # between rows
  @console_cursor ||=
    Termplot::Cursors::BufferedConsoleCursor.new(self, Array.new(cols * rows + rows - 1))
end

#cursorObject



13
14
15
# File 'lib/termplot/window.rb', line 13

def cursor
  @cursor ||= Termplot::Cursors::VirtualCursor.new(self)
end

#each_rowObject



83
84
85
86
87
# File 'lib/termplot/window.rb', line 83

def each_row
  buffer.each_slice(cols) do |row|
    yield row
  end
end

#flushObject

Flush rendered window to a string



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/termplot/window.rb', line 39

def flush
  console_cursor.clear_buffer
  console_cursor.reset_position
  buffer.each_slice(cols).with_index do |line, i|
    line.each do |v|
      console_cursor.write(v)
    end
    console_cursor.new_line
  end
  console_cursor.flush
end

#flush_debugObject

Flush to 2d array rather than string



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/termplot/window.rb', line 52

def flush_debug
  debug_arr = []
  buffer.each_slice(cols).with_index do |line, y|
    render_line = line.each_with_index.map do |c, x|
      y * cols + x == cursor.position ? "𝥺" : c
    end
    debug_arr << render_line
    debug_arr << "\n"
  end
  debug_arr
end

TODO: Refine later and include errors properly in the window



65
66
67
68
69
# File 'lib/termplot/window.rb', line 65

def print_errors(errors)
  print errors.join(Termplot::ControlChars::NEWLINE)
  print Termplot::ControlChars::NEWLINE
  errors.length.times { print Termplot::ControlChars::UP }
end

#sizeObject



24
25
26
# File 'lib/termplot/window.rb', line 24

def size
  rows * cols
end

#write(char) ⇒ Object



28
29
30
31
# File 'lib/termplot/window.rb', line 28

def write(char)
  buffer[cursor.position] = char
  cursor.write(char)
end