-require 'curses'
-
module Redwood
## a fully-functional text field supporting completions, expansions,
## in sup, completion support is implemented through BufferManager#ask
## and CompletionMode.
class TextField
- def initialize window, y, x, width
- @w, @x, @y = window, x, y
- @width = width
+ def initialize
@i = nil
@history = []
def value; @value || get_cursed_value end
- def activate question, default=nil, &block
+ def activate window, y, x, width, question, default=nil, &block
+ @w, @y, @x, @width = window, y, x, width
@question = question
@completion_block = block
- @field = Ncurses::Form.new_field 1, @width - question.length,
- @y, @x + question.length, 0, 0
+ @field = Ncurses::Form.new_field 1, @width - question.length, @y, @x + question.length, 256, 0
@form = Ncurses::Form.new_form [@field]
+ @value = default
Ncurses::Form.post_form @form
set_cursed_value default if default
end
@w.mvaddstr @y, 0, @question
Ncurses.curs_set 1
Ncurses::Form.form_driver @form, Ncurses::Form::REQ_END_FIELD
- Ncurses::Form.form_driver @form, Ncurses::Form::REQ_NEXT_CHAR if @value && @value =~ / $/ # fucking RETARDED!!!!
+ Ncurses::Form.form_driver @form, Ncurses::Form::REQ_NEXT_CHAR if @value && @value =~ / $/ # fucking RETARDED
end
def deactivate
case c
when Ncurses::KEY_ENTER # submit!
@value = get_cursed_value
- @history.push @value
+ @history.push @value unless @value =~ /^\s*$/
return false
when Ncurses::KEY_CANCEL # cancel
@value = nil
Ncurses::Form::REQ_PREV_CHAR
when Ncurses::KEY_RIGHT
Ncurses::Form::REQ_NEXT_CHAR
- when Ncurses::KEY_BACKSPACE
+ when Ncurses::KEY_DC
+ Ncurses::Form::REQ_DEL_CHAR
+ when Ncurses::KEY_BACKSPACE, 127 # 127 is also a backspace keysym
Ncurses::Form::REQ_DEL_PREV
- when ?\001
+ when 1 #ctrl-a
Ncurses::Form::REQ_BEG_FIELD
- when ?\005
+ when 5 #ctrl-e
Ncurses::Form::REQ_END_FIELD
- when Ncurses::KEY_UP
- @i ||= @history.size
- @history[@i] = get_cursed_value
- @i = (@i - 1) % @history.size
- @value = @history[@i]
- set_cursed_value @value
- when Ncurses::KEY_DOWN
- @i ||= @history.size
- @history[@i] = get_cursed_value
- @i = (@i + 1) % @history.size
- @value = @history[@i]
- set_cursed_value @value
+ when 11 # ctrl-k
+ Ncurses::Form::REQ_CLR_EOF
+ when Ncurses::KEY_UP, Ncurses::KEY_DOWN
+ unless @history.empty?
+ value = get_cursed_value
+ @i ||= @history.size
+ #Redwood::log "history before #{@history.inspect}"
+ @history[@i] = value #unless value =~ /^\s*$/
+ @i = (@i + (c == Ncurses::KEY_UP ? -1 : 1)) % @history.size
+ @value = @history[@i]
+ #Redwood::log "history after #{@history.inspect}"
+ set_cursed_value @value
+ Ncurses::Form::REQ_END_FIELD
+ end
else
c
end
- Ncurses::Form.form_driver @form, d
+ Ncurses::Form.form_driver @form, d if d
true
end