Thread: Pre-Populating Input Prompt Using Libedit

  1. #16
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    Quote Originally Posted by anduril462 View Post
    Doh! I totally misread your post. Don't know what I was thinking. Maybe I should step out for a bit.
    It's ok . Here is a better explaination:

    My whole goal here is to programmatically enter text into the same prompt the user will be modifying. This will happen as soon as the prompt is displayed. The user can then choose to backspace the value and enter a new value of their own or press enter to use what is already at the prompt. I am still unclear as to how I am going to actually write to the prompt programmatically. Also, how will I know when to parse the terminal for the <enter> character to determine that the user has finished entering information? I don't think I fully understand this approach. Perhaps my steps should have been:

    - Change terminal settings to allow raw input when shell launches.

    1. Display the prompt - "[Prompt]:"
    2. Programmatically insert a value beyond the prompt - "[Prompt]: value"
    3. Alow user to use existing value or backspace and use their own input (or modify existing value) - "[Prompt]: value|user input|modified value"
    4. Process the prompt data

    My previous post was unclear in terms of what I am trying to do. I apoligize for this.

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yeah, makes sense now. A little time out of the office, a little food in my belly, and I think I'm (mostly) up and running. grumpy showed you a way using ungetc, but there's no guarantee that you can push back more than 1 character. Here's another hack alternative:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
        int i;
        char *p;
        char ans[80];
        char *prompt = "Enter some text";
        char *prev_ans = "foo";
    
    
        printf("%s: %s", prompt, prev_ans);
        for (i = 0; i < strlen(prev_ans); i++) {
            putchar('\b');
        }
    
    
        fgets(ans, sizeof(ans), stdin);
        if (strcmp(ans, "\n") == 0) {
            // empty string, use prev_ans
            strcpy(ans, prev_ans);
        }
        else {
            // trim trailing new line from ans
            p = strchr(ans, '\n');
            if (p != NULL) {
                *p = '\0';
            }
        }
    
    
        printf("The answer entered was: %s\n", ans);
    
    
        return 0;
    }
    I'm not sure how standard or portable the behavior of printing those \b chars will be. There's also no way to clear the previous answer from the prompt once they start typing, unless you use raw terminal mode, but it's somewhat close. If you have to do a lot of this, or want it to be more professional, then perhaps look into the curses/ncurses library, or go back to the termios stuff.

    Hopefully I'm on the right page this time.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You know, we might be way overthinking this. How many times have I seen things like this from my computer:
    Code:
    Do you really want to do this?
    [no]
    where if you just hit enter it takes the [bracketed] default answer. That might be exactly what you want, and requires nothing special in terms of buffering (and also doesn't require the user to backspace over the pre-filled answer, which may not even be possible with ungetc).

  4. #19
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    Quote Originally Posted by anduril462 View Post
    Yeah, makes sense now. A little time out of the office, a little food in my belly, and I think I'm (mostly) up and running. grumpy showed you a way using ungetc, but there's no guarantee that you can push back more than 1 character. Here's another hack alternative:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    int main(void)
    {
        int i;
        char *p;
        char ans[80];
        char *prompt = "Enter some text";
        char *prev_ans = "foo";
    
    
        printf("%s: %s", prompt, prev_ans);
        for (i = 0; i < strlen(prev_ans); i++) {
            putchar('\b');
        }
    
    
        fgets(ans, sizeof(ans), stdin);
        if (strcmp(ans, "\n") == 0) {
            // empty string, use prev_ans
            strcpy(ans, prev_ans);
        }
        else {
            // trim trailing new line from ans
            p = strchr(ans, '\n');
            if (p != NULL) {
                *p = '\0';
            }
        }
    
    
        printf("The answer entered was: %s\n", ans);
    
    
        return 0;
    }
    I'm not sure how standard or portable the behavior of printing those \b chars will be. There's also no way to clear the previous answer from the prompt once they start typing, unless you use raw terminal mode, but it's somewhat close. If you have to do a lot of this, or want it to be more professional, then perhaps look into the curses/ncurses library, or go back to the termios stuff.

    Hopefully I'm on the right page this time.
    This is much closer to what I am looking for, but as you explained you can't backspace. It also overwrites the answer as you type rather than inserting characters.

    Just out of curiosity, when I hit the right arrow key at the prompt I get ^[[C displayed instead of a shift in the position of the cursor. Do you know how to change this behavior such that it only shifts the cursor? This is another problem I was having when parsing the user commands myself using the fgets method you listed above. It is also the reason I have begun using libedit. It seems to handle this behavior for you.

  5. #20
    Registered User
    Join Date
    Jun 2008
    Posts
    93
    Quote Originally Posted by tabstop View Post
    You know, we might be way overthinking this. How many times have I seen things like this from my computer:
    Code:
    Do you really want to do this?
    [no]
    where if you just hit enter it takes the [bracketed] default answer. That might be exactly what you want, and requires nothing special in terms of buffering (and also doesn't require the user to backspace over the pre-filled answer, which may not even be possible with ungetc).
    I had thought of this previously. I was just hoping that I could find a way to populate the prompt with a previous response so they could alter it without having to reenter the whole thing. I have probably already spent too much time on this. I think what I am going to do is display the bracketed answer before they are prompted for input. Then I will insert their previous answer at the top of their history. If they press the up arrow key it will populate the previous answer and they can then modify it. This seems to be a good compromise. What do you guys think?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Populating 2-D arrays
    By fgg in forum C Programming
    Replies: 4
    Last Post: 10-13-2010, 12:39 PM
  2. command prompt input, write into file
    By angela in forum C Programming
    Replies: 3
    Last Post: 03-17-2010, 09:00 AM
  3. populating 2-D array
    By edster in forum C Programming
    Replies: 4
    Last Post: 10-22-2009, 02:42 PM
  4. Program hangs up at first input prompt.
    By MikeR in forum C Programming
    Replies: 3
    Last Post: 10-14-2007, 07:45 PM
  5. Replies: 5
    Last Post: 05-06-2004, 09:14 AM