user8266283
user8266283

Reputation:

Deleting a character already printed in the terminal in ncurses

I am writing a text editor in ncurses. The program is initialized in raw mode. So I need to manually do many things like deletion, avoiding printing non printable characters, etc.

For deletion:

void console(ch)
{
    if(ch == 8) //8 = backspace according to asciitables.com
    {
        printw("\b");
        printw(" ");
    }
    else
    {
        addch(ch);
    }
}

For avoiding non-printable characters:

    void console(ch)
    {
       bool safe = TRUE;
       int avoid[] = { 1,2,3,4,5,6,7,8};
       for(int i=0;i<4;i++)
    {
        while(ch==avoid[i])
        {
            safe = false;
        }
    }
    if(safe)
    {
        printw("%c",ch); //Prints the key's characters on the screen.
    }
    else 
    {
        break;
    }
 }

In the deletion, I wanted to deleted the previously printed character in the terminal and insert a blank space and move the cursor back to the place of the previous character. But that doesn't work.

In the avoid non-printable character, I wanted to avoid the non printable characters to get printed and only print the printable character. But that also doesn't seems to work.

It would be very helpful if someone points me where I am wrong and correct me. It would be also helpful if anybody can tell me whether there are any specific functions for this in the ncurses library. I am pretty much new to ncurses.

Upvotes: 0

Views: 2638

Answers (1)

Thomas Dickey
Thomas Dickey

Reputation: 54455

The easiest way in curses to detect "nonprintable" characters is to examine the result from unctrl. If the character is printable, the result is a single-character. Otherwise it is two or more characters:

char *check = unctrl(ch);
int safe = (check != 0 && strlen(check) == 1);

(The manual page goes into some detail).

By the way, addch is more appropriate than printw for printing characters (but keep in mind that its parameter is a chtype, which fits in an int, not char). Again, the manual page would be useful reading to prepare your program.

Upvotes: 0

Related Questions