class ScrollArea
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.
Attributes
content[R]
height[RW]
start_x[R]
start_y[R]
width[RW]
Public Class Methods
new(width, height)
click to toggle source
# 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
Public Instance Methods
add_line(line)
click to toggle source
# File lib/terminal-scroll-area/scroll_area.rb, line 32 def add_line(line) self.content += "#{line}\n" end
add_string(string)
click to toggle source
# File lib/terminal-scroll-area/scroll_area.rb, line 28 def add_string(string) self.content += string end
content=(new_content)
click to toggle source
# File lib/terminal-scroll-area/scroll_area.rb, line 23 def content=(new_content) @content = new_content update_content_dimensions end
end_x()
click to toggle source
# File lib/terminal-scroll-area/scroll_area.rb, line 60 def end_x @start_x + (@width - 1) end
end_y()
click to toggle source
# File lib/terminal-scroll-area/scroll_area.rb, line 64 def end_y @start_y + (@height - 1) end
render()
click to toggle source
# 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_down()
click to toggle source
# 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_left()
click to toggle source
# 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_right()
click to toggle source
# 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_up()
click to toggle source
# 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
Private Instance Methods
crop_text(text, x_start, y_start, x_end, y_end)
click to toggle source
# File lib/terminal-scroll-area/scroll_area.rb, line 75 def crop_text(text, x_start, y_start, x_end, y_end) return '' if x_start >= @col_count || y_start >= @line_count lines = text.split("\n") lines = lines[y_start..y_end] lines = lines.map { |line| line[x_start..x_end] } lines.join("\n") end
update_content_dimensions()
click to toggle source
# File lib/terminal-scroll-area/scroll_area.rb, line 70 def update_content_dimensions @line_count = @content.split("\n").length @col_count = @content.split("\n").map(&:length).max end