prompt_string() replacement with basic editing functions

new topic     » goto parent     » topic index » view thread      » older message » newer message

Here is a prompt_string() replacement with some basic editing functions (I've tested it with Phix 1.0.1):

Left, right, home, and end keys move the cursor
Backspace deletes character left of cursor
Delete deletes character at cursor position
Any character typed is appended or inserted at cursor position
Enter returns input string regardless of cursor position

No line wrap, max length of input string = screen width minus 1 minus length of promt string

include graphics.e 
 
global function prompt_string_ed(sequence prompt) 
 
  object scrn                      -- for calling video_config() 
  object pos                       -- for calling get_position 
  object in_string                 -- input string 
  integer s                        -- pressed key 
  integer in_c                     -- cursor position in input string 
  integer pl                       -- prompt length 
  integer in_max                   -- input max length 
  integer in_l                     -- current length of input string 
  integer screen_li                -- screen cursor position line 
 
  scrn = video_config() 
  in_max = scrn[10] - length(prompt) - 1 
 
  puts(1, prompt) 
  pl = length(prompt) 
  in_string = "" 
  in_l = 0 
  in_c = 1 
 
  pos = get_position() 
  screen_li = pos[1] 
 
  while 1 do                               -- loop exited when return key pressed 
    s = wait_key() 
 
    if s < 32 then                         -- ctrl characters 
      if s = 13 then                       -- return 
        exit 
      elsif s = 8 then                     -- backspace 
        if in_l > 0 then 
          if in_l = in_c - 1 then 
            in_string = in_string[1..in_l-1] 
            in_l -= 1 
            in_c -= 1 
            position (screen_li, in_c + pl) 
            puts(1, " ") 
            position (screen_li, in_c + pl) 
          elsif in_c > 1 then 
            in_string = in_string[1..in_c-2] & in_string[in_c..$] 
            in_l -= 1 
            in_c -= 1 
            position (screen_li, in_c + pl) 
            puts(1, in_string[in_c..$] & " ") 
            position (screen_li, in_c + pl) 
          end if 
        end if 
      end if 
 
    elsif s = 339 then                    -- delete 
      if in_l > 0 then 
        if in_c < in_l + 1 then 
          in_string = in_string[1..in_c-1] & in_string[in_c+1..$] 
          in_l -= 1 
          position (screen_li, in_c + pl) 
          puts(1, in_string[in_c..$] & " ") 
          position (screen_li, in_c + pl) 
        end if 
      end if 
 
    elsif s = 327 then                    -- home 
      in_c = 1 
      position (screen_li, in_c + pl) 
    elsif s = 335 then                    -- end 
      in_c = in_l + 1 
      position (screen_li, in_c + pl) 
    elsif s = 331 then                    -- arrow left 
      if in_c > 1 then 
        in_c -= 1 
        position (screen_li, in_c + pl) 
      end if 
    elsif s = 333 then                    -- arrow right 
      if in_c < in_l + 1 then 
        in_c += 1 
        position (screen_li, in_c + pl) 
      end if 
 
    elsif s < 128 then                    -- printable characters, change to 256 to allow 8-bit characters 
      if in_l < in_max then               -- max length of input string not yet reached 
        if in_l = in_c - 1 then           -- cursor at line end 
          puts(1, s) 
          in_string = in_string & s 
        else 
          in_string = in_string[1..in_c-1] & s & in_string[in_c..$] 
          position (screen_li, in_c + pl) 
          puts(1, in_string[in_c..$]) 
          position (screen_li, in_c + pl + 1) 
        end if 
        in_l += 1 
        in_c += 1 
      end if 
    end if 
  end while 
 
  puts(1, "\n") 
  return in_string 
end function 

Comments welcome, should anyone actually be interested in this.
Robert

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu