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



67
68
69
70
# File 'lib/tty_string/screen.rb', line 67

def clear_backward
  clear_line_backward
  clear_lines_before
end

#clear_forwardObject



72
73
74
75
# File 'lib/tty_string/screen.rb', line 72

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



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

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

#clear_lines_beforeObject



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

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

#scroll_downObject



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

def scroll_down
  screen.unshift([])
  screen.pop
end

#scroll_upObject



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

def scroll_up
  screen.push([])
  screen.shift
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



77
78
79
80
81
82
83
84
# File 'lib/tty_string/screen.rb', line 77

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

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