lib/sup/textfield.rb in sup-0.12.1 vs lib/sup/textfield.rb in sup-0.13.0

- old
+ new

@@ -1,10 +1,10 @@ module Redwood ## a fully-functional text field supporting completions, expansions, ## history--everything! -## +## ## writing this fucking sucked. if you thought ncurses was some 1970s ## before-people-knew-how-to-program bullshit, wait till you see ## ncurses forms. ## ## completion comments: completion is done emacs-style, and mostly @@ -118,10 +118,13 @@ set_cursed_value cursed_value_after_point Ncurses::Form.form_driver @form, Ncurses::Form::REQ_END_FIELD nop Ncurses::Form::REQ_BEG_FIELD when ?\C-w.ord + while action = remove_extra_space + Ncurses::Form.form_driver @form, action + end Ncurses::Form.form_driver @form, Ncurses::Form::REQ_PREV_CHAR Ncurses::Form.form_driver @form, Ncurses::Form::REQ_DEL_WORD when Ncurses::KEY_UP, Ncurses::KEY_DOWN unless !@i || @history.empty? value = get_cursed_value @@ -162,9 +165,43 @@ ## cursor <= end of text if x - @question.length - v.length <= 0 v else # trailing spaces v + (" " * (x - @question.length - v.length)) + end + end + + def remove_extra_space + return nil unless @field + + Ncurses::Form.form_driver @form, Ncurses::Form::REQ_VALIDATION + x = Ncurses.curx + v = @field.field_buffer(0).gsub(/^\s+|\s+$/, "") + v_index = x - @question.length + + # at start of line + if v_index < 1 + nil + ## cursor <= end of text + elsif v_index < v.length + # is the character before the cursor a space? + if v[v_index-1] == ?\s + # if there is a non-space char under cursor then go back + if v[v_index] != ?\s + Ncurses::Form::REQ_PREV_CHAR + # otherwise delete the space + else + Ncurses::Form::REQ_DEL_PREV + end + else + nil + end + elsif v_index == v.length + # at end of string, with non-space before us + nil + else + # trailing spaces + Ncurses::Form::REQ_PREV_CHAR end end def set_cursed_value v @field.set_field_buffer 0, v