]> git.cworth.org Git - sup/blob - lib/sup/modes/text-mode.rb
moved evertying to devel
[sup] / lib / sup / modes / text-mode.rb
1 module Redwood
2
3 class TextMode < ScrollMode
4   attr_reader :text
5
6   def initialize text=""
7     @text = text
8     update_lines
9     buffer.mark_dirty if buffer
10     super()
11   end
12
13   def text= t
14     @text = t
15     update_lines
16     if buffer
17       ensure_mode_validity
18       buffer.mark_dirty 
19     end
20   end
21
22   def << line
23     @lines = [0] if @text.empty?
24     @text << line
25     @lines << @text.length
26     if buffer
27       ensure_mode_validity
28       buffer.mark_dirty 
29     end
30   end
31
32   def lines
33     @lines.length - 1
34   end
35
36   def [] i
37     return nil unless i < @lines.length
38     @text[@lines[i] ... (i + 1 < @lines.length ? @lines[i + 1] - 1 : @text.length)]
39 #    (@lines[i] ... (i + 1 < @lines.length ? @lines[i + 1] - 1 : @text.length)).inspect
40   end
41
42 private
43
44   def update_lines
45     pos = @text.find_all_positions("\n")
46     pos.push @text.length unless pos.last == @text.length - 1
47     @lines = [0] + pos.map { |x| x + 1 }
48   end
49 end
50
51 end