class InteractiveScrollArea
Interactice scroll area which scrolls and reprints the content in response to user pressing arrow keys.
Attributes
content[R]
height[RW]
scroll_area[RW]
width[RW]
Public Class Methods
new(width, height)
click to toggle source
# 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
Public Instance Methods
add_line(line)
click to toggle source
# File lib/terminal-scroll-area/interactive_scroll_area.rb, line 40 def add_line(line) @scroll_area.add_line(line) end
add_string(string)
click to toggle source
# File lib/terminal-scroll-area/interactive_scroll_area.rb, line 36 def add_string(string) @scroll_area.add_string(string) end
content=(new_content)
click to toggle source
# File lib/terminal-scroll-area/interactive_scroll_area.rb, line 32 def content=(new_content) @scroll_area.content = new_content end
keydown(_event)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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
scroll()
click to toggle source
# 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
Private Instance Methods
print_in_place(text)
click to toggle source
# File lib/terminal-scroll-area/interactive_scroll_area.rb, line 66 def print_in_place(text) cursor = TTY::Cursor # Scrolling down is needed if there are less lines under # the terminal prompt than the content height. Or else, clearing # lines upwards by the content height will remove the prompt as well. in_place = TTY::Cursor.scroll_down * (@height - 1) in_place << cursor.clear_lines(@height, :up) in_place << cursor.save in_place << text in_place << cursor.restore print(in_place) end