extends ScrollMode to have a line-based cursor.
# File lib/sup/modes/line-cursor-mode.rb, line 15 def initialize opts={} @cursor_top = @curpos = opts.delete(:skip_top_rows) || 0 @load_more_callbacks = [] @load_more_q = Queue.new @load_more_thread = ::Thread.new do while true e = @load_more_q.pop @load_more_callbacks.each { |c| c.call e } sleep 0.5 @load_more_q.pop until @load_more_q.empty? end end super opts end
# File lib/sup/modes/line-cursor-mode.rb, line 31 def cleanup @load_more_thread.kill super end
# File lib/sup/modes/line-cursor-mode.rb, line 36 def draw super set_status end
# File lib/sup/modes/line-cursor-mode.rb, line 92 def cursor_down call_load_more_callbacks buffer.content_height if @curpos >= lines - [buffer.content_height/2,1].max return false unless @curpos < lines - 1 if @curpos >= botline - 1 page_down set_cursor_pos topline else @curpos += 1 unless buffer.dirty? draw_line @curpos - 1 draw_line @curpos set_status buffer.commit end end true end
# File lib/sup/modes/line-cursor-mode.rb, line 111 def cursor_up return false unless @curpos > @cursor_top if @curpos == topline old_topline = topline page_up set_cursor_pos [old_topline - 1, topline].max else @curpos -= 1 unless buffer.dirty? draw_line @curpos + 1 draw_line @curpos set_status buffer.commit end end true end
# File lib/sup/modes/line-cursor-mode.rb, line 48 def draw_line ln, opts={} if ln == @curpos super ln, :highlight => true, :debug => opts[:debug] else super end end
# File lib/sup/modes/line-cursor-mode.rb, line 56 def ensure_mode_validity super raise @curpos.inspect unless @curpos.is_a?(Integer) c = @curpos.clamp topline, botline - 1 c = @cursor_top if c < @cursor_top buffer.mark_dirty unless c == @curpos @curpos = c end
# File lib/sup/modes/line-cursor-mode.rb, line 166 def jump_to_end super if topline < (lines - buffer.content_height) set_cursor_pos(lines - 1) end
# File lib/sup/modes/line-cursor-mode.rb, line 161 def jump_to_start super set_cursor_pos @cursor_top end
# File lib/sup/modes/line-cursor-mode.rb, line 81 def line_down # overwrite scrollmode super call_load_more_callbacks([topline + buffer.content_height - lines, 10].max) if topline + buffer.content_height > lines set_cursor_pos topline if @curpos < topline end
# File lib/sup/modes/line-cursor-mode.rb, line 87 def line_up # overwrite scrollmode super set_cursor_pos botline - 1 if @curpos > botline - 1 end
more complicated than one might think. three behaviors.
# File lib/sup/modes/line-cursor-mode.rb, line 140 def page_down ## if we're on the last page, and it's not a full page, just move ## the cursor down to the bottom and assume we can't load anything ## else via the callbacks. if topline > lines - buffer.content_height set_cursor_pos(lines - 1) ## if we're on the last page, and it's a full page, try and load ## more lines via the callbacks and then shift the page down elsif topline == lines - buffer.content_height call_load_more_callbacks buffer.content_height super ## otherwise, just move down else relpos = @curpos - topline super set_cursor_pos [topline + relpos, lines - 1].min end end
# File lib/sup/modes/line-cursor-mode.rb, line 129 def page_up # overwrite if topline <= @cursor_top set_cursor_pos @cursor_top else relpos = @curpos - topline super set_cursor_pos topline + relpos end end
override search behavior to be cursor-based. this is a stupid implementation and should be made better. TODO: improve.
# File lib/sup/modes/line-cursor-mode.rb, line 73 def search_goto_line line page_down while line >= botline page_up while line < topline set_cursor_pos line end
# File lib/sup/modes/line-cursor-mode.rb, line 79 def search_start_line; @curpos end
# File lib/sup/modes/line-cursor-mode.rb, line 65 def set_cursor_pos p return if @curpos == p @curpos = p.clamp @cursor_top, lines buffer.mark_dirty end
callbacks when the cursor is asked to go beyond the bottom
# File lib/sup/modes/line-cursor-mode.rb, line 44 def to_load_more &b @load_more_callbacks << b end