How to Update a Printed Message in Terminal Without Reprinting

How to update a printed message in terminal without reprinting

try using \r instead of \n when printing the new "version".

for(int i=0;i<=100;++i) printf("\r[%3d%%]",i);
printf("\n");

How to update a output field in terminal without output a new line?

The trick used for this is to return to the first position in the current line instead of progressing to the next line.

This is done by writing the \r character (carriage return) to the terminal/stdout.

How to let terminal refresh without printing new stuff in C

The simpler choice, if you want to keep it on a single line, would be to use \r.
Printing \r will move your cursor to the beginning of the line, giving you the ability to print hover the old characters.

A solution would look something like

for (i = 0; i < 100; ++i)
{
printf("\r%3i%%", i);
fflush(stdout);
/* ... */
}

If you need more advanced control over the terminal, you can use termcaps.

How to overwrite the previous print to stdout?

Simple Version

One way is to use the carriage return ('\r') character to return to the start of the line without advancing to the next line.

Python 3

for x in range(10):
print(x, end='\r')
print()

Python 2.7 forward compatible

from __future__ import print_function
for x in range(10):
print(x, end='\r')
print()

Python 2.7

for x in range(10):
print '{}\r'.format(x),
print

Python 2.0-2.6

for x in range(10):
print '{0}\r'.format(x),
print

In the latter two (Python 2-only) cases, the comma at the end of the print statement tells it not to go to the next line. The last print statement advances to the next line so your prompt won't overwrite your final output.

Line Cleaning

If you can’t guarantee that the new line of text is not shorter than the existing line, then you just need to add a “clear to end of line” escape sequence, '\x1b[1K' ('\x1b' = ESC):

for x in range(75):
print('*' * (75 - x), x, end='\x1b[1K\r')
print()

Update printf value on same line instead of new one

You should add \r to your printf as others have said.
Also, make sure you flush stdout, as stdout stream is buffered & will only display what's in the buffer after it reaches a newline.

In your case:

for (int i=0;i<10;i++){
//...
printf("\rValue of X is: %d", x/114);
fflush(stdout);
//...
}

Java: Updating text in the command-line without a new line

First when you write, don't use writeln(). Use write(). Second, you can use a "\r" to Carriage Return without using \n which is a New line. The carriage return should put you back at the beginning of the line.

C++ Update console output

Standard C++ does not support setting individual characters at positions in the console without re-printing. This is OS-specific, and there are comments that address this.

Otherwise, the correct solution is to encapsulate your game board logic into a class. We can use a nested std::vector to handle a dynamically-sized board, and provide functions for getting and setting cells. A separate Print function allows us to print the board to the console as often as we'd like.

class Grid
{
public:
Grid(int size) : myGrid(size, std::vector<int>(size, 0)) // initialize grid to be correctly sized and all zeros
{
Randomize();
}

void Randomize()
{
for (size_t i=0;i<myGrid.size();i++)
{
for (size_t j=0;j<myGrid[i].size();j++)
{
myGrid[i][j] = rand() % 9 + 1;
}
}
}

void Print(std::ostream& out) const
{
out<<"\n\tPuzzle\n\t";
for(size_t i=0;i<myGrid.size();i++)
{
out<<i<<" ";
}
out << "\n\n";
for(size_t i=0;i<myGrid.size();i++)
{
out<<i<<"\t";
for(size_t j=0;j<myGrid[i].size();j++)
{
out<<myGrid[i][j]<<" ";
}
out<<"\n";
}
}

int GetValue(size_t row, size_t col) const
{
// use wraparound for too-large values
// alternatively you could throw if row and/or col are too large
return myGrid[row % myGrid.size()][col % myGrid.size()];
}

void SetValue(size_t row, size_t col, int val)
{
myGrid[row % myGrid.size()][col % myGrid.size()] = val;
}

private:
std::vector<std::vector<int>> myGrid;
};

Now you can write your main like so:

int main()
{
srand(time(NULL));
Grid board(10);
size_t xValue = 0;
size_t yValue = 0;

// game loop. You could even abstract this behavior into another class
while(true)
{
board.Print(std::cout);
std::cout<<"\nEnter x value: ";
if (!std::cin) // check for no input
break;
std::cin>>xValue;
if (!std::cin) // check for end of input
break;
std::cout<<"Enter y value: ";
std::cin>>yValue;
if (!std::cin)
break;
board.SetValue(xValue, yValue, 0);

// other game logic...
}

// print board one last time before exit
std::cout << "Game over. Final board: \n";
board.Print(std::cout);
}

Live Demo

Game in linux terminal: Changing images: printing over already printed text

Ncurses creates its own character window in the terminal and allows you to acces and overwrite characters at specific positions. This looks perfect for your game, where you can draw character-art cards anywhere on the screen. Way better than emulating the same by scrolling () the terminal, anyway. – M Oehm

Curses is the best option, as compatible libraries are available for all operating systems (including Windows command line); ncurses is the best one for Linux. For Linux and Unix terminals, you can also use ANSI escape codes, often called terminal codes. For example, printing "\033[2;3H" will move the cursor to the third column on the second row, counting from top. Anyway, I do recommend using Curses anyway. – Nominal Animal

Print to the same line and not a new line?

It's called the carriage return, or \r

Use

print i/len(some_list)*100," percent complete         \r",

The comma prevents print from adding a newline. (and the spaces will keep the line clear from prior output)

Also, don't forget to terminate with a print "" to get at least a finalizing newline!



Related Topics



Leave a reply



Submit