Class: InteractiveScrollArea

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

Overview

Interactice scroll area which scrolls and reprints the content in response to user pressing arrow keys.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) ⇒ InteractiveScrollArea

Returns a new instance of InteractiveScrollArea.



13
14
15
16
17
18
19
20
21
# File 'lib/terminal-scroll-area/interactive_scroll_area.rb', line 13

def initialize(width, height)
  @width = width
  @height = height

  @scroll_area = ScrollArea.new(@width, @height)

  @reader = TTY::Reader.new(interrupt: :exit)
  @reader.subscribe(self)
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



10
11
12
# File 'lib/terminal-scroll-area/interactive_scroll_area.rb', line 10

def content
  @content
end

#heightObject

Returns the value of attribute height.



11
12
13
# File 'lib/terminal-scroll-area/interactive_scroll_area.rb', line 11

def height
  @height
end

#scroll_areaObject

Returns the value of attribute scroll_area.



11
12
13
# File 'lib/terminal-scroll-area/interactive_scroll_area.rb', line 11

def scroll_area
  @scroll_area
end

#widthObject

Returns the value of attribute width.



11
12
13
# File 'lib/terminal-scroll-area/interactive_scroll_area.rb', line 11

def width
  @width
end

Instance Method Details

#add_line(line) ⇒ Object



40
41
42
# File 'lib/terminal-scroll-area/interactive_scroll_area.rb', line 40

def add_line(line)
  @scroll_area.add_line(line)
end

#add_string(string) ⇒ Object



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

def add_string(string)
  @scroll_area.add_string(string)
end

#keydown(_event) ⇒ Object



44
45
46
47
# File 'lib/terminal-scroll-area/interactive_scroll_area.rb', line 44

def keydown(_event)
  @scroll_area.scroll_down
  print_in_place(@scroll_area.render)
end

#keyleft(_event) ⇒ Object



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

def keyleft(_event)
  @scroll_area.scroll_left
  print_in_place(@scroll_area.render)
end

#keyright(_event) ⇒ Object



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

def keyright(_event)
  @scroll_area.scroll_right
  print_in_place(@scroll_area.render)
end

#keyup(_event) ⇒ Object



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

def keyup(_event)
  @scroll_area.scroll_up
  print_in_place(@scroll_area.render)
end

#scrollObject



23
24
25
26
27
28
29
30
# File 'lib/terminal-scroll-area/interactive_scroll_area.rb', line 23

def scroll
  print_in_place(@scroll_area.render)
  TTY::Cursor.invisible do
    loop do
      @reader.read_keypress
    end
  end
end