Re: enhancing prompt() functions
- Posted by euphoric (admin) Sep 15, 2011
- 1061 views
I would like to enhance the prompt_string() function to allow the user to supply a default or initial string to be used.
For example:
uname = "Steve Jones" -- could be retrieved from a database, prefs file, or whatever uname = prompt_string( "Enter username: ", uname )
Or is there another/a better way to go about this?
This approach is quite difficult, because we just use gets(0). Even if we fall back to C code, to handle fgets() we'd have to use ungetc() to push back the characters in the initial string - and ungetc() only guarantees one push back (thus one character).
So, we have to drop gets() and use wait_key() and puts() and handle the edit processing ourselves. This is a lot more complicated than the existing prompt_string() function. OTOH, ed.ex already does this.
I'm not sure why we'd need to do all of that. Using the example above, it might look like:
Enter username: [Steve Jones]Then, if you simply press enter, it's as though you entered Steve Jones. Otherwise, whatever the user enters is returned.
I don't see an easy way to get "Steve Jones" into the buffer, which I think is what would have to happen.
I'm going to try a puts(0,default) and see if that works. :)
Edit: It doesn't work. :/