0

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.

  • You are printing to the terminal, not "the stdout". Don't conflate stdout with the terminal, especially if you are writing a curses based program. – William Pursell Dec 02 '17 at 03:11
  • ok. Changed that. But tell me where am I wrong?? –  Dec 02 '17 at 03:27
  • Doesn't work how, exactly? If `printw("\b")` works at all, you'd want `printw("\b \b");` to overwrite with space and leave the cursor backed up by one. – Peter Cordes Dec 02 '17 at 04:51
  • Thanks that worked. But what about avoiding non printable character?? –  Dec 02 '17 at 06:29
  • Have you tried examining each character with: isprint(ch) – TonyB Dec 02 '17 at 07:04

1 Answers1

0

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.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105