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

#initialize(initial_style:) ⇒ Screen

Returns a new instance of Screen.



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

def initialize(initial_style:)
  @cursor = Cursor.new
  @screen = [Row.new(newline_style: initial_style)]
  @current_style = @initial_style = initial_style
end

Instance Attribute Details

#cursorObject (readonly)

Returns the value of attribute cursor.



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

def cursor
  @cursor
end

Instance Method Details

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



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

def []=((row, col), value)
  screen[row] ||= Row.new(newline_style: current_style)
  screen[row][col] = value
end

#clearObject



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

def clear
  @screen = []
end

#clear_at_cursorObject



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

def clear_at_cursor
  self[cursor] = Cell.new(' ', style: current_style)
end

#clear_backwardObject



86
87
88
89
# File 'lib/tty_string/screen.rb', line 86

def clear_backward
  clear_line_backward
  clear_lines_before
end

#clear_forwardObject



91
92
93
94
# File 'lib/tty_string/screen.rb', line 91

def clear_forward
  clear_lines_after
  clear_line_forward
end

#clear_lineObject



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

def clear_line
  screen[row] = Row.new(newline_style: current_style)
end

#clear_line_backwardObject



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

def clear_line_backward
  screen[row].fill(Cell.new(' ', style: current_style), 0..col)
end

#clear_line_forwardObject



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

def clear_line_forward
  screen[row].slice!(col..-1)
end

#clear_lines_afterObject



82
83
84
# File 'lib/tty_string/screen.rb', line 82

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

#clear_lines_beforeObject



78
79
80
# File 'lib/tty_string/screen.rb', line 78

def clear_lines_before
  screen.fill(Row.new(newline_style: current_style), 0...row)
end

#ensure_rowObject



96
97
98
# File 'lib/tty_string/screen.rb', line 96

def ensure_row
  screen[row] ||= Row.new(newline_style: current_style)
end

#scroll_downObject



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

def scroll_down
  screen.unshift(Row.new(newline_style: current_style))
  screen.pop
end

#scroll_upObject



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

def scroll_up
  screen.push(Row.new(newline_style: current_style))
  screen.shift
end

#style(style_codes) ⇒ Object



107
108
109
# File 'lib/tty_string/screen.rb', line 107

def style(style_codes)
  self.current_style = current_style.new(style_codes)
end

#to_sObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/tty_string/screen.rb', line 19

def to_s # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
  style_context = initial_style
  str = +''
  screen.each_with_index do |row, index|
    unless index.zero?
      str << row.newline_style.to_s(context: style_context) if row
      str << "\n"
      style_context = row.newline_style if row
    end

    Array(row).each do |cell|
      if cell
        str << cell.style.to_s(context: style_context)
        str << cell.value
        style_context = cell.style
      else
        str << ' '
      end
    end
  end
  str << current_style.to_s(context: style_context)
  str
end

#write(string) ⇒ Object



100
101
102
103
104
105
# File 'lib/tty_string/screen.rb', line 100

def write(string)
  string.each_char do |char|
    self[cursor] = Cell.new(char, style: current_style)
    cursor.right
  end
end