How to Erase Printed Characters in a Console Application(Linux)

How do I erase printed characters in a console application(Linux)?

I don't think you need to apologize for the language choice. PHP is a great language for console applications.

Try this out:

<?php
for( $i=0;$i<10;$i++){
print "$i \r";
sleep(1);
}
?>

The "\r" will overwrite the line with the new text. To make a new line you can just use "\n", but I'm guessing you already knew that.

Hope this helps! I know this works in Linux, but I don't know if it works in Windows or other operating systems.

Erase the current printed console line

You can use VT100 escape codes. Most terminals, including xterm, are VT100 aware. For erasing a line, this is ^[[2K. In C this gives:

printf("\33[2K\r");

How to erase characters printed in console

There are functions available for process.stdout:

var i = 0;  // dots counter
setInterval(function() {
process.stdout.clearLine(); // clear current text
process.stdout.cursorTo(0); // move cursor to beginning of line
i = (i + 1) % 4;
var dots = new Array(i + 1).join(".");
process.stdout.write("Waiting" + dots); // write text
}, 300);

It is possible to provide arguments to clearLine(direction, callback)

/**
* -1 - to the left from cursor
* 0 - the entire line // default
* 1 - to the right from cursor
*/

Update Dec 13, 2015: although the above code works, it is no longer documented as part of process.stdin. It has moved to readline

How to delete an already written character in C++ console

Outputting the backspace character '\b' may help to move the output point back.

Specifically, outputting the string "\b \b" should blank out the last character output.

How to remove characters from console output in C

The usual approach to this is to treat either the first or the last printf as a special case (outside of the loop):

for(ii=0; ii<2; ii++) {
jj = 0;
printf("%d", jj); // first number printed without space.
for(jj=1; jj<4; jj++) {
printf(" %d", jj); // include the space before the number printed
}
if(ii<2-1) printf("\n");
}

Obviously I simplified how the loops are constructed and what is printed - for simplicity. You could make the first printf statement

printf("\n%d", jj);

then you have a newline at the start of your output (often a good thing) and then you don't need the if statement later - you just don't have a newline printed at the end of the line (because it will be printed at the start...)

There are marginally more efficient ways of doing this that would involve no if statements at all - but these all come at the expense of less readable code. For example, here is a "no loop unrolling and no additional if statements" version of the code:

http://codepad.org/01qPPtee

#include <stdio.h>
int main(void) {
int ii, jj;
ii = 0;
while(1) {
jj = 0;
while(1) {
printf("%d", jj); // include the space before the number printed
jj++;
if(jj<4) printf("."); else break;
}
ii++;
if(ii<2) printf("*\n"); else break;
}
return 0;
}

Output:

0.1.2.3*
0.1.2.3

Basically I have taken the functionality of the for loop and made it explicit; I also use a . rather than a and "*\n" rather than "\n" to show in the printout that things behave as expected.

It does what you asked without extra evaluation of the condition. Is it more readable? Not really...

How to remove a character from a Linux terminal in C

As I noted in a comment, you need to use backspace instead of '\177' (or '\x7F') to move backwards. You also have to worry about buffering of standard I/O. It's often best not to use a mixture of standard I/O and file descriptor I/O on the same stream — standard output in this example. Use one or the other, but not both.

This works:

#include <unistd.h>

int main(void)
{
char buff1[] = "abc";
char buff2[] = "\b \b";
write(STDOUT_FILENO, buff1, sizeof(buff1) - 1);
sleep(2);
write(STDOUT_FILENO, buff2, sizeof(buff2) - 1);
sleep(2);
write(STDOUT_FILENO, "\n", 1);
return 0;
}

It shows first (for 2 seconds):

abc

then (for another 2 seconds):

ab

then it exits. The cursor is after c at first, then after b.

Delete Characters in Python Printed Line


write('\b')  # <-- backup 1-character

Clearing the screen by printing a character?

If you want to clear the screen, the "ANSI" sequence in a printf

\033[2J

clears the entire screen, e.g.,

printf '\033[2J'

The command-line clear program uses this, along with moving the cursor to the "home" position, again an "ANSI" sequence:

\033[H

The program gets the information from the terminal database. For example, for TERM=vt100, it might see this (using \E as \033):

clear=\E[H\E[J$<50>

(the $<50> indicates padding needed for real VT100s). You might notice that the 2 is absent from this string. That is because the cursor is first moved to the home (upper left) position, and the 2 (entire screen) is not necessary. Eliminating that from the string made VT100s a little faster.

On the other hand, if you just want to reset the terminal, you can use the VT100-style RIS:

\033c

but that has side-effects, besides not being in ECMA-48. These bug reports were for side-effects of \033c:

  • Debian Bug report logs - #60377
    "reset" broken for dumb terminals
  • Debian Bug report logs - #239205
    "reset changes a unicode console to non-unicode"

Further reading:

  • Why doesn't the screen clear when I type control/L?
  • XTerm Control Sequences

CSI Ps J Erase in Display (ED).
Ps = 0 -> Erase Below (default).
Ps = 1 -> Erase Above.
Ps = 2 -> Erase All.
Ps = 3 -> Erase Saved Lines (xterm).
  • ECMA-48: Control Functions for Coded Character Sets


Related Topics



Leave a reply



Submit