]> git.cworth.org Git - sup/blob - lib/sup/modes/scroll-mode.rb
moved evertying to devel
[sup] / lib / sup / modes / scroll-mode.rb
1 module Redwood
2
3 class ScrollMode < Mode
4   attr_reader :status, :topline, :botline
5
6   COL_JUMP = 2
7
8   register_keymap do |k|
9     k.add :line_down, "Down one line", :down, 'j', 'J'
10     k.add :line_up, "Up one line", :up, 'k', 'K'
11     k.add :col_left, "Left one column", :left, 'h'
12     k.add :col_right, "Right one column", :right, 'l'
13     k.add :page_down, "Down one page", :page_down, 'n', ' '
14     k.add :page_up, "Up one page", :page_up, 'p', :backspace
15     k.add :jump_to_home, "Jump to top", :home, '^', '1'
16     k.add :jump_to_end, "Jump to bottom", :end, '$', '0'
17   end
18
19   def initialize opts={}
20     @topline, @botline, @leftcol = 0, 0, 0
21     @slip_rows = opts[:slip_rows] || 0 # when we pgup/pgdown,
22                                        # how many lines do we keep?
23     @twiddles = opts.member?(:twiddles) ? opts[:twiddles] : true
24     super()
25   end
26
27   def draw
28     ensure_mode_validity
29     (@topline ... @botline).each { |ln| draw_line ln }
30     ((@botline - @topline) ... buffer.content_height).each do |ln|
31       if @twiddles
32         buffer.write ln, 0, "~", :color => :twiddle_color
33       else
34         buffer.write ln, 0, ""
35       end
36     end
37     @status = "lines #{@topline + 1}:#{@botline}/#{lines}"
38   end
39
40   def col_left
41     return unless @leftcol > 0
42     @leftcol -= COL_JUMP
43     buffer.mark_dirty
44   end
45
46   def col_right
47     @leftcol += COL_JUMP
48     buffer.mark_dirty
49   end
50
51   ## set top line to l
52   def jump_to_line l
53     l = l.clamp 0, lines - 1
54     return if @topline == l
55     @topline = l
56     @botline = [l + buffer.content_height, lines].min
57     buffer.mark_dirty
58   end
59
60   def line_down; jump_to_line @topline + 1; end
61   def line_up;  jump_to_line @topline - 1; end
62   def page_down; jump_to_line @topline + buffer.content_height - @slip_rows; end
63   def page_up; jump_to_line @topline - buffer.content_height + @slip_rows; end
64   def jump_to_home; jump_to_line 0; end
65   def jump_to_end; jump_to_line lines - buffer.content_height; end
66
67   def ensure_mode_validity
68     @topline = @topline.clamp 0, lines - 1
69     @topline = 0 if @topline < 0 # empty 
70     @botline = [@topline + buffer.content_height, lines].min
71   end
72
73 protected
74
75   def draw_line ln, opts={}
76     case(s = self[ln])
77     when String
78       buffer.write ln - @topline, 0, s[@leftcol .. -1],
79                    :highlight => opts[:highlight]
80     when Array
81       xpos = 0
82       s.each do |color, text|
83         raise "nil text for color '#{color}'" if text.nil?
84         if xpos + text.length < @leftcol
85           buffer.write ln - @topline, 0, "", :color => color,
86                        :highlight => opts[:highlight]
87           xpos += text.length
88           ## nothing
89         elsif xpos < @leftcol
90           ## partial
91           buffer.write ln - @topline, 0, text[(@leftcol - xpos) .. -1],
92                        :color => color,
93                        :highlight => opts[:highlight]
94           xpos += text.length
95         else
96           buffer.write ln - @topline, xpos - @leftcol, text,
97                        :color => color, :highlight => opts[:highlight]
98           xpos += text.length
99         end
100
101       end
102     end
103   end
104 end
105
106 end