Class: RubyRich::Viewport
- Inherits:
-
Object
- Object
- RubyRich::Viewport
- Defined in:
- lib/ruby_rich/viewport.rb
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#height ⇒ Object
Returns the value of attribute height.
-
#scroll_top ⇒ Object
Returns the value of attribute scroll_top.
-
#selected_text ⇒ Object
readonly
Returns the value of attribute selected_text.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #at_bottom? ⇒ Boolean
- #attach(layout, priority: 100) ⇒ Object
- #blur ⇒ Object
- #focus ⇒ Object
- #handle_event(event_data, layout = nil) ⇒ Object
-
#initialize(content = "", scrollbar: true, auto_scroll: false, scrollbar_style: :blue) ⇒ Viewport
constructor
A new instance of Viewport.
- #max_scroll_top ⇒ Object
- #render ⇒ Object
- #scroll_by(delta) ⇒ Object
- #scroll_to(line) ⇒ Object
- #scroll_to_bottom ⇒ Object
Constructor Details
#initialize(content = "", scrollbar: true, auto_scroll: false, scrollbar_style: :blue) ⇒ Viewport
Returns a new instance of Viewport.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ruby_rich/viewport.rb', line 8 def initialize(content = "", scrollbar: true, auto_scroll: false, scrollbar_style: :blue) @content = content @scrollbar = @auto_scroll = auto_scroll @scrollbar_style = @width = 0 @height = 0 @scroll_top = 0 @dragging_scrollbar = false @drag_start_y = nil @drag_start_scroll_top = nil @selecting = false @selection_start = nil @selection_end = nil @selected_text = "" @focused = true @rendered_lines_cache_key = nil @rendered_lines_cache = nil end |
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content.
6 7 8 |
# File 'lib/ruby_rich/viewport.rb', line 6 def content @content end |
#height ⇒ Object
Returns the value of attribute height.
5 6 7 |
# File 'lib/ruby_rich/viewport.rb', line 5 def height @height end |
#scroll_top ⇒ Object
Returns the value of attribute scroll_top.
5 6 7 |
# File 'lib/ruby_rich/viewport.rb', line 5 def scroll_top @scroll_top end |
#selected_text ⇒ Object (readonly)
Returns the value of attribute selected_text.
6 7 8 |
# File 'lib/ruby_rich/viewport.rb', line 6 def selected_text @selected_text end |
#width ⇒ Object
Returns the value of attribute width.
5 6 7 |
# File 'lib/ruby_rich/viewport.rb', line 5 def width @width end |
Instance Method Details
#at_bottom? ⇒ Boolean
135 136 137 |
# File 'lib/ruby_rich/viewport.rb', line 135 def at_bottom? @scroll_top >= max_scroll_top end |
#attach(layout, priority: 100) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ruby_rich/viewport.rb', line 46 def attach(layout, priority: 100) [:page_up, :page_down, :home, :end, :up, :down].each do |event_name| layout.key(event_name, priority) do |event_data, _live| handle_event(event_data, layout) false end end [:mouse_wheel, :mouse_down, :mouse_drag, :mouse_up].each do |event_name| layout.key(event_name, priority) do |event_data, _live| handle_event(event_data, layout) end end self end |
#blur ⇒ Object
33 34 35 36 |
# File 'lib/ruby_rich/viewport.rb', line 33 def blur @focused = false self end |
#focus ⇒ Object
28 29 30 31 |
# File 'lib/ruby_rich/viewport.rb', line 28 def focus @focused = true self end |
#handle_event(event_data, layout = nil) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ruby_rich/viewport.rb', line 63 def handle_event(event_data, layout = nil) return false if keyboard_event?(event_data) && !@focused return false if mouse_event?(event_data) && !@focused case event_data[:name] when :page_up scroll_by(-page_size) false when :page_down scroll_by(page_size) false when :home scroll_to(0) false when :end scroll_to_bottom false when :up scroll_by(-1) false when :down scroll_by(1) false when :mouse_wheel scroll_by(event_data[:direction] == :down ? 3 : -3) true when :mouse_down return copy_selection if event_data[:button] == :right (event_data, layout) || start_selection(event_data, layout) when :mouse_drag (event_data, layout) || drag_selection(event_data, layout) when :mouse_up || stop_selection else false end end |
#max_scroll_top ⇒ Object
139 140 141 |
# File 'lib/ruby_rich/viewport.rb', line 139 def max_scroll_top [rendered_lines.length - @height, 0].max end |
#render ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ruby_rich/viewport.rb', line 102 def render clamp_scroll return [] if @height.to_i <= 0 || @width.to_i <= 0 visible_width = content_width visible = rendered_lines[@scroll_top, @height] || [] lines = Array.new(@height) { "" } @height.times do |index| absolute_line = @scroll_top + index lines[index] = apply_selection(fit_line(visible[index].to_s, visible_width), absolute_line) end return lines unless = lines.each_with_index.map { |line, index| line + [index] } end |
#scroll_by(delta) ⇒ Object
121 122 123 |
# File 'lib/ruby_rich/viewport.rb', line 121 def scroll_by(delta) scroll_to(@scroll_top + delta) end |
#scroll_to(line) ⇒ Object
125 126 127 128 |
# File 'lib/ruby_rich/viewport.rb', line 125 def scroll_to(line) @scroll_top = line.to_i clamp_scroll end |
#scroll_to_bottom ⇒ Object
130 131 132 133 |
# File 'lib/ruby_rich/viewport.rb', line 130 def scroll_to_bottom @scroll_top = max_scroll_top clamp_scroll end |