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

#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

#flushObject



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

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_debug(str = "Window") ⇒ Object



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

def flush_debug(str = "Window")
  padding = "-" * 10
  puts "\n#{padding} #{str} #{padding}\n"
  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
    print render_line
    puts
  end
end

TODO: Refine later and include errors properly in the window



63
64
65
66
67
# File 'lib/termplot/window.rb', line 63

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