Class: TTYString::Screen

Inherits:
Object
  • Object
show all
Defined in:
lib/tty_string/screen.rb

Overview

a grid to draw on

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScreen

Returns a new instance of Screen.



10
11
12
13
# File 'lib/tty_string/screen.rb', line 10

def initialize
  @cursor = Cursor.new
  @screen = []
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



8
9
10
# File 'lib/tty_string/screen.rb', line 8

def cursor
  @cursor
end

Instance Method Details

#[](row, col) ⇒ Object



24
25
26
27
# File 'lib/tty_string/screen.rb', line 24

def []((row, col))
  screen[row] ||= []
  screen[row][col]
end

#[]=(row, col, *value) ⇒ Object



19
20
21
22
# File 'lib/tty_string/screen.rb', line 19

def []=((row, col), *value)
  screen[row] ||= []
  screen[row][col] = value.flatten(1)
end

#clearObject



45
46
47
# File 'lib/tty_string/screen.rb', line 45

def clear
  @screen = []
end

#clear_at_cursorObject



29
30
31
# File 'lib/tty_string/screen.rb', line 29

def clear_at_cursor
  self[cursor] = nil
end

#clear_backwardObject



57
58
59
60
# File 'lib/tty_string/screen.rb', line 57

def clear_backward
  clear_line_backward
  clear_lines_before
end

#clear_forwardObject



62
63
64
65
# File 'lib/tty_string/screen.rb', line 62

def clear_forward
  clear_lines_after
  clear_line_forward
end

#clear_lineObject



41
42
43
# File 'lib/tty_string/screen.rb', line 41

def clear_line
  screen[row] = []
end

#clear_line_backwardObject



37
38
39
# File 'lib/tty_string/screen.rb', line 37

def clear_line_backward
  screen[row].fill(nil, 0..col)
end

#clear_line_forwardObject



33
34
35
# File 'lib/tty_string/screen.rb', line 33

def clear_line_forward
  screen[row].fill(nil, col..-1)
end

#clear_lines_afterObject



53
54
55
# File 'lib/tty_string/screen.rb', line 53

def clear_lines_after
  screen.slice!((row + 1)..-1)
end

#clear_lines_beforeObject



49
50
51
# File 'lib/tty_string/screen.rb', line 49

def clear_lines_before
  screen.fill([], 0...row)
end

#to_sObject



15
16
17
# File 'lib/tty_string/screen.rb', line 15

def to_s
  screen.map { |c| Array(c).map { |x| x || ' ' }.join.rstrip }.join("\n")
end

#write(string) ⇒ Object



67
68
69
70
71
72
73
74
# File 'lib/tty_string/screen.rb', line 67

def write(string)
  return self[cursor] if string.empty?

  string.each_char do |char|
    self[cursor] = char
    cursor.right
  end
end