3 ## a fully-functional text field supporting completions, expansions,
4 ## history--everything!
6 ## writing this fucking sucked. if you thought ncurses was some 1970s
7 ## before-people-knew-how-to-program bullshit, wait till you see
10 ## completion comments: completion is done emacs-style, and mostly
11 ## depends on outside support, as we merely signal the existence of a
12 ## new set of completions to show (#new_completions?) or that the
13 ## current list of completions should be rolled if they're too large
14 ## to fill the screen (#roll_completions?).
16 ## in sup, completion support is implemented through BufferManager#ask
17 ## and CompletionMode.
19 def initialize window, y, x, width
20 @w, @x, @y = window, x, y
25 @completion_block = nil
26 reset_completion_state
29 bool_reader :new_completions, :roll_completions
30 attr_reader :completions
32 def value; @value || get_cursed_value end
34 def activate question, default=nil, &block
36 @completion_block = block
37 @field = Ncurses::Form.new_field 1, @width - question.length,
38 @y, @x + question.length, 0, 0
39 @form = Ncurses::Form.new_form [@field]
41 Ncurses::Form.post_form @form
42 set_cursed_value default if default
46 @w.attrset Colormap.color_for(:none)
47 @w.mvaddstr @y, 0, @question
49 Ncurses::Form.form_driver @form, Ncurses::Form::REQ_END_FIELD
50 Ncurses::Form.form_driver @form, Ncurses::Form::REQ_NEXT_CHAR if @value && @value =~ / $/ # fucking RETARDED!!!!
54 reset_completion_state
63 ## short-circuit exit paths
65 when Ncurses::KEY_ENTER # submit!
66 @value = get_cursed_value
67 @history.push @value unless @value =~ /^\s*$/
69 when Ncurses::KEY_CANCEL # cancel
72 when Ncurses::KEY_TAB # completion
73 return true unless @completion_block
74 if @completions.empty?
76 c = @completion_block.call v
78 @value = c.map { |full, short| full }.shared_prefix(true)
79 set_cursed_value @value
84 @new_completions = true
85 @roll_completions = false
88 @new_completions = false
89 @roll_completions = true
94 reset_completion_state
99 when Ncurses::KEY_LEFT
100 Ncurses::Form::REQ_PREV_CHAR
101 when Ncurses::KEY_RIGHT
102 Ncurses::Form::REQ_NEXT_CHAR
103 when Ncurses::KEY_BACKSPACE
104 Ncurses::Form::REQ_DEL_PREV
106 Ncurses::Form::REQ_BEG_FIELD
108 Ncurses::Form::REQ_END_FIELD
110 Ncurses::Form::REQ_CLR_EOF
111 when Ncurses::KEY_UP, Ncurses::KEY_DOWN
112 unless @history.empty?
113 value = get_cursed_value
115 #Redwood::log "history before #{@history.inspect}"
116 @history[@i] = value #unless value =~ /^\s*$/
117 @i = (@i + (c == Ncurses::KEY_UP ? -1 : 1)) % @history.size
118 @value = @history[@i]
119 #Redwood::log "history after #{@history.inspect}"
120 set_cursed_value @value
121 Ncurses::Form::REQ_END_FIELD
127 Ncurses::Form.form_driver @form, d if d
133 def reset_completion_state
135 @new_completions = @roll_completions = @clear_completions = false
138 ## ncurses inanity wrapper
140 ## DO NOT READ THIS CODE. YOU WILL GO MAD.
142 return nil unless @field
145 Ncurses::Form.form_driver @form, Ncurses::Form::REQ_VALIDATION
146 v = @field.field_buffer(0).gsub(/^\s+|\s+$/, "")
148 ## cursor <= end of text
149 if x - @question.length - v.length <= 0
151 else # trailing spaces
152 v + (" " * (x - @question.length - v.length))
156 def set_cursed_value v
157 @field.set_field_buffer 0, v