5 ## a fully-functional text field supporting completions, expansions,
6 ## history--everything!
8 ## completion is done emacs-style, and mostly depends on outside
9 ## support, as we merely signal the existence of a new set of
10 ## completions to show (#new_completions?) or that the current list
11 ## of completions should be rolled if they're too large to fill the
12 ## screen (#roll_completions?).
14 ## in sup, completion support is implemented through BufferManager#ask
15 ## and CompletionMode.
17 def initialize window, y, x, width
18 @w, @x, @y = window, x, y
23 @completion_block = nil
24 reset_completion_state
27 bool_reader :new_completions, :roll_completions
28 attr_reader :completions
30 ## when the user presses enter, we store the value in @value and
31 ## clean up all the ncurses cruft. before @value is set, we can
32 ## get the current value from ncurses.
33 def value; @field ? get_cur_value : @value end
35 def activate question, default=nil, &block
38 @completion_block = block
39 @field = Ncurses::Form.new_field 1, @width - question.length,
40 @y, @x + question.length, 0, 0
41 @form = Ncurses::Form.new_form [@field]
43 @history[@i = @history.size] = default || ""
44 Ncurses::Form.post_form @form
45 set_cur_value @history[@i]
49 @w.attrset Colormap.color_for(:none)
50 @w.mvaddstr @y, 0, @question
52 Ncurses::Form.form_driver @form, Ncurses::Form::REQ_END_FIELD
53 Ncurses::Form.form_driver @form, Ncurses::Form::REQ_NEXT_CHAR if @history[@i] =~ / $/ # fucking RETARDED!!!!
57 reset_completion_state
66 ## short-circuit exit paths
68 when Ncurses::KEY_ENTER # submit!
69 @value = @history[@i] = get_cur_value
71 when Ncurses::KEY_CANCEL # cancel
73 @i = @history.empty? ? nil : (@i - 1) % @history.size
76 when Ncurses::KEY_TAB # completion
77 return true unless @completion_block
78 if @completions.empty?
80 c = @completion_block.call v
82 set_cur_value c.map { |full, short| full }.shared_prefix
86 @new_completions = true
87 @roll_completions = false
90 @new_completions = false
91 @roll_completions = true
96 reset_completion_state
100 when Ncurses::KEY_LEFT
101 Ncurses::Form::REQ_PREV_CHAR
102 when Ncurses::KEY_RIGHT
103 Ncurses::Form::REQ_NEXT_CHAR
104 when Ncurses::KEY_BACKSPACE
105 Ncurses::Form::REQ_DEL_PREV
107 Ncurses::Form::REQ_BEG_FIELD
109 Ncurses::Form::REQ_END_FIELD
111 @history[@i] = @field.field_buffer 0
112 @i = (@i - 1) % @history.size
113 set_cur_value @history[@i]
114 when Ncurses::KEY_DOWN
115 @history[@i] = @field.field_buffer 0
116 @i = (@i + 1) % @history.size
117 set_cur_value @history[@i]
122 Ncurses::Form.form_driver @form, d
128 def reset_completion_state
130 @new_completions = @roll_completions = @clear_completions = false
133 ## ncurses inanity wrapper
135 Ncurses::Form.form_driver @form, Ncurses::Form::REQ_VALIDATION
136 @field.field_buffer(0).gsub(/^\s+|\s+$/, "").gsub(/\s+/, " ")
139 ## ncurses inanity wrapper
141 @field.set_field_buffer 0, v
142 Ncurses::Form.form_driver @form, Ncurses::Form::REQ_END_FIELD