Clear specific part of ncurses screen c++ - c++

I know clrtoeol() can clear the entire line however I want to clear the screen from (y0, x0) to (y0, x1).
For example I have a code something like this:
mvprintw(6, 22, "Press any key to continue...");
getch();
//and here I want to print another line at the same place and this line is shorter.
mvprintw(6, 22, "shorter line");
If I try to print a string(or a character array I think) that is longer then it is fine but when I print a shorter one then the part of the first sentence is still seen.
If I use clrtoeol(); the entire line is cleared however I have some things at the end of that line which I don't want to be cleared.

Just add this after the getch() call:
move(6, 22);
clrtoeol();
ncurses will optimize this so that it will write the (shorter) text-string and then clear the remainder of the line (assuming that you do not add another refresh call, or functions such as getch which cause a refresh).

Related

Is it possible to modify the terminal's previously printed and buffered characters?

I will start by saying: I am already aware that I can use '\b' and ansi escape codes to get the cursor position, move the cursor, and get the terminal size.
However, once the-text-I-am-interested-in has scrolled past the terminal's view, it seems impossible to modify said text.
For instance, imagine that the terminal has the following displayed on it:
first line [...]
a
b
c
...
last line
And when printing a new line, let's imagine that "first line [...]" scrolls out of screen, disappearing above "a":
a
b
c
...
last line
inserted line
Would it still be possible to modify the line ('first line [...]'), perhaps using the terminal's own buffer, in C++?
The idea would be to modify the line, such that if you do scroll back up, you would see the modified version (bonus points if you can also still print ansi codes for formatting out-of-screen).

Why does using the '\b' not work when it gets to newline?

I've been trying to read from a file to console and then clear it to read another file.
So I found out you can remove characters from console by using '\b'. However when I try to remove previous content from console it only removes the last line and then stops.
Something like this:
std::cout<<"Something boring"<<'\n'<<"Something boring on new line";
while(1){
std::cout<<'\b';
std::cout<<' ';
std::cout<<'\b';
}
This will only remove "Something boring on new line", and as far as I can see the cursor is on the first position of line 2.
Why does it get stuck and can something be done about it?
Thanks.

Is there a way to set the console height(in lines)?

Is it possible to set how many lines the console will print before it starts to erase the top ones? For example, is it possible to set it to 3 and only make the last 3 lines be visible? So:
std::cout<<"line 1!"<<std::endl;
std::cout<<"line 2!"<<std::endl;
std::cout<<"line 3!"<<std::endl;
std::cout<<"line 4!"<<std::endl;
system("pause");
Would output:
line 3!
line 4!
Press any key to continue...
^without creating a scroll bar on the side.
I've been trying to use Console::BufferHeight but I can't seem to get it to work. This is the only thing I've been able to find that seems to be close to what I want to do: http://msdn.microsoft.com/en-us/library/system.console.bufferheight.aspx But It just shows how to read it, not how to set the size. And for some reason typing just std::cout<<System::Console::BufferHeight; gives me scope errors. Any help would be greatly appreciated. Thanks!
I think you want this:
SetConsoleWindowInfo
Example,
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SMALL_RECT rect = {0,0, 100, 100};
SetConsoleWindowInfo(hConsole, TRUE, &rect)
Have a look at these as well (experiment with them):
SetConsoleScreenBufferSize()
SetConsoleScreenBufferInfoEx
There is an example on MSDN here.
Im new to programming also, but I tried:
while(!cin.get())
{
}
and it worked,try to place it in the bottom of the code. It will work wonders on your console!
Try changing its position (put it in the middle of the code)
I'd suggest keeping the lines yourself, in a quick class, say that cycles where to put the next line by using an iterator that is always set to point at the next line to be input into.
Next, use FillConsoleOutputCharacter() to print blanks over the lines you previously had printed there.
Then, use SetConsoleCursorPosition() to four(or however many lines you wanted) lines above where you want your input to start, and output each line in you cycle, starting at the one after your iterator. This prints all the lines in order from eldest to youngest.It's been a while, so my knowledge of C++ is kinda hazy, but this should be pretty simple with the standard library and win32 library.

Undo a newline (\n) printed to command line

printf("Error %d\n", 1);
printf("\nStatus: %d%%", 50);
prints
Error 1
Status: 50%
In this set up, is there any chance to insert Error 2\n between Error 1\n and \nStatus: 50%. I understand that \r and \b can be used to change printed text in the same line (e.g., if there is a single \n between Error 1 and Status: 50%), but can I change text in a previous line?
Thanks!
What #Ryan said.
Explanation why: stdout is some abstract stream that doesn't have to be the terminal. It may be a file, a pipe, a socket, a printer, a text to speech device or whatever. In many cases there is no sense to what you asked to do. Hence you need some library that works with the terminal specifically.
Sorry, you cannot.
But you may issue system calls to clear the whole screen instead, like system("clear") (OS-dependent).
Or use ncurses just as Kos mentioned in the comment.
You could use ANSI Escapesequences to move your "cursor" one line up:
void cursorOnLineUp(void) { printf("\033[1A"); }
Or set it to a specific position:
void setCursor(int column, int row) { printf("\033[%d;%dH", row, column) }
Haven't tried it for C++, but succesfully used it for a simple game in ANSI-C!

Implementing the clrscr() function to understand its working

I am trying to make the copies of the builtin functions and adding a x to their name so i can understand each functions working.While writing a function for clrscr() i am confused about how it works.Does it use 2 nested loops and print (" ") i.e space all over the screen or it prints("\n") over the screen?Or what?
I tried this:
#include<stdio.h>
#include<conio.h>
void main(void)
{
printf("press any key to make clrscr() work");
getch();
for(int i=0;i<50;i++)
{
printf("\n");
}
// to make the screen come to 1,1
gotoxy(1,1);
getch();
}
clrscr() implementation may depend on the environment your console application runs. Usually it sends the ClearScreen control character (0x0C) to the console driver, that actually clears the screen.
The driver knows about character space to clear as well as all attributes (blink, underline,...) to reset.
If you dont want the driver to handle 0x0C, you can mimic this with 50 times calling printf("\n"). but calling 50x80 calling poutchar(' ') is not similar to calling clrsrc(), since the cursor will be advanced by one what may put it in the next line after scrolling the screen content.
Further you should regard, that the behaviour of the screen depends on the implementation. When the cursor position is in the right column and you output one character the cursor position may stay at the right edge or it may cause a new line. Whe you cursor position is in the lower right corner the next character may cause a new line including scrolling the screen content by one line.
The best way would be to imaging what clrscr() would do and let it make it's job.