Class: ScrollArea

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal-scroll-area/scroll_area.rb

Overview

Scroll area which only shows a specific area of the content it holds at a time. Able to scroll area in all directions to show a different area of the content.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ ScrollArea

Returns a new instance of ScrollArea.



10
11
12
13
14
15
16
17
# File 'lib/terminal-scroll-area/scroll_area.rb', line 10

def initialize(width, height)
  @width = width
  @height = height
  @start_x = 0
  @start_y = 0
  @content = ''
  update_content_dimensions
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



7
8
9
# File 'lib/terminal-scroll-area/scroll_area.rb', line 7

def content
  @content
end

#heightObject

Returns the value of attribute height.



8
9
10
# File 'lib/terminal-scroll-area/scroll_area.rb', line 8

def height
  @height
end

#start_xObject (readonly)

Returns the value of attribute start_x.



7
8
9
# File 'lib/terminal-scroll-area/scroll_area.rb', line 7

def start_x
  @start_x
end

#start_yObject (readonly)

Returns the value of attribute start_y.



7
8
9
# File 'lib/terminal-scroll-area/scroll_area.rb', line 7

def start_y
  @start_y
end

#widthObject

Returns the value of attribute width.



8
9
10
# File 'lib/terminal-scroll-area/scroll_area.rb', line 8

def width
  @width
end

Instance Method Details

#add_line(line) ⇒ Object



32
33
34
# File 'lib/terminal-scroll-area/scroll_area.rb', line 32

def add_line(line)
  self.content += "#{line}\n"
end

#add_string(string) ⇒ Object



28
29
30
# File 'lib/terminal-scroll-area/scroll_area.rb', line 28

def add_string(string)
  self.content += string
end

#end_xObject



60
61
62
# File 'lib/terminal-scroll-area/scroll_area.rb', line 60

def end_x
  @start_x + (@width - 1)
end

#end_yObject



64
65
66
# File 'lib/terminal-scroll-area/scroll_area.rb', line 64

def end_y
  @start_y + (@height - 1)
end

#renderObject



19
20
21
# File 'lib/terminal-scroll-area/scroll_area.rb', line 19

def render
  crop_text(@content, @start_x, @start_y, end_x, end_y)
end

#scroll_downObject



42
43
44
45
46
# File 'lib/terminal-scroll-area/scroll_area.rb', line 42

def scroll_down
  return if @line_count < @height

  @start_y += 1 if end_y < (@line_count - 1)
end

#scroll_leftObject



48
49
50
51
52
# File 'lib/terminal-scroll-area/scroll_area.rb', line 48

def scroll_left
  return if @col_count < @width

  @start_x -= 1 if @start_x >= 1
end

#scroll_rightObject



54
55
56
57
58
# File 'lib/terminal-scroll-area/scroll_area.rb', line 54

def scroll_right
  return if @col_count < @width

  @start_x += 1 if end_x < (@col_count - 1)
end

#scroll_upObject



36
37
38
39
40
# File 'lib/terminal-scroll-area/scroll_area.rb', line 36

def scroll_up
  return if @line_count < @height

  @start_y -= 1 if @start_y.positive?
end