NoNameY0
NoNameY0

Reputation: 632

Backspace not erasing character, similar to BASH

I can parse input, traverse up and down in the history but my backspace does not work. Why is that?

void printPrompt(void)
{ // prints working directory prompt
    char cwd[256];
    getcwd(cwd, 255);
    printf("%s> ", cwd);
    fflush(stdout);
}

Upvotes: 0

Views: 200

Answers (2)

DrC
DrC

Reputation: 7698

The backspace is working. The problem is the redisplay of the line after the backspace. You should probably print a carriage return (\r) and then reprint the line including the prompt.

Edit: I'm not certain that my suggestion is 100% portable across terminals either. You may need to use something like curses to handle the portability issues.

Upvotes: 0

Xymostech
Xymostech

Reputation: 9850

You're trying to print out a DEL (dec 127) when you should be sending a BS (dec 8). You'll also probably then want to send a space and another BS to clear out that character (BS just moves the cursor). So, when you get buf[0] == 8, print out 8, 32, 8.

else if (buf[0] == 8)
{ // Backspace
    if (charsRead > 0)
    {
        line[charsRead - 1] = '\0';
        charsRead--;
        printf("%c%c%c", 8, 32, 8); // CHANGE HERE
    }
}

Upvotes: 5

Related Questions