-1

I'm trying to delete the characters from the cursor to the end of line, the problem is when I print a new line ('\n') after, the deleted characters reappear, I also tried to print a null character before the new line and it does the same. Minimal code:

#include <ncurses.h>
#include <term.h>
#include <unistd.h>
#include <stdlib.h>

int ft_putchar(int ch)
{
    char c = (char)ch;
    return (write(1, &c, 1));
}

int main(void)
{
    tgetent(getenv("TERM"), NULL);
    char * LE = tgetstr("LE", NULL); // termcap for cursor left
    char * kL = tgetstr("kL", NULL); // termcap for cursor right

    write(1, "Hello World", 11);
    tputs(tparm(LE, 5), 1, ft_putchar); // move the cursor 5 cases left
    tputs(kL, 1, ft_putchar); // delete to end of line
    write(1, "\n", 1);
    return (0);
}

Output: Hello World

And without the last write(1, "\n", 1)

Output: Hello

I spent hours to discover that the new line was causing that, now I can't figure out how to do it.

And I also tried write(1, "\0\n", 2) and it does the same.

Any clues of how to avoid that?

Fayeure
  • 1,181
  • 9
  • 21

1 Answers1

0

Look at the ce termcap entry, that means clear from cursor to end of line.

codegen
  • 118
  • 5
  • Yes, I know that, what I want to do is delete characters in current line then print a new line feed and print something else without the text reappearing, I tried with `dm` (enter delete mode) then move the cursor then `ed` (exit delete mode) and when I print a new line the characters still reappear – Fayeure Mar 31 '21 at 20:51
  • So you need to output the ce string to clear the rest if the current line, then output a newline to go to the next line, then output whatever you want on the next line. – Chris Dodd Mar 31 '21 at 23:12
  • Your example code only uses the LE and kL entries. You have to do a tgetstr("ce") to get the ce entry from the termcap. This is the control sequence to delete to the end of the line. Then you write the newline. – codegen Apr 01 '21 at 21:12