lib/sup/modes/completion_mode.rb (1385B) - raw
1 module Redwood
2
3 class CompletionMode < ScrollMode
4 INTERSTITIAL = " "
5
6 def initialize list, opts={}
7 @list = list
8 @header = opts[:header]
9 @prefix_len = opts[:prefix_len]
10 @lines = nil
11 super :slip_rows => 1, :twiddles => false
12 end
13
14 def lines
15 update_lines unless @lines
16 @lines.length
17 end
18
19 def [] i
20 update_lines unless @lines
21 @lines[i]
22 end
23
24 def roll; if at_bottom? then jump_to_start else page_down end end
25
26 private
27
28 def update_lines
29 max_length = @list.max_of { |s| s.length }
30 num_per = [1, buffer.content_width / (max_length + INTERSTITIAL.length)].max
31 @lines = [@header].compact
32 @list.each_with_index do |s, i|
33 if @prefix_len
34 @lines << [] if i % num_per == 0
35 if @prefix_len < s.length
36 prefix = s[0 ... @prefix_len]
37 suffix = s[(@prefix_len + 1) .. -1]
38 char = s[@prefix_len].chr
39
40 @lines.last += [[:text_color, sprintf("%#{max_length - suffix.length - 1}s", prefix)],
41 [:completion_character_color, char],
42 [:text_color, suffix + INTERSTITIAL]]
43 else
44 @lines.last += [[:text_color, sprintf("%#{max_length}s#{INTERSTITIAL}", s)]]
45 end
46 else
47 @lines << "" if i % num_per == 0
48 @lines.last += sprintf "%#{max_length}s#{INTERSTITIAL}", s
49 end
50 end
51 end
52 end
53
54 end