I have a C++ function to be called by some thread; the function inserts a line of text (using ANSI ) into the console, without abrupting current user input.
This works fine until the bottom of the console is reached, where the user input gets overwritten, since the console is not auto-scrolling, like so:
The code:
void log_insert(std::string& line)
{
static int num_of_lines = 1;
// save horizontal cursor position
std::cout << "\033[s";
// insert <X> number of lines (\033[<X>L)
std::cout << "\033[" << num_of_lines + 1 << "L";
// move cursor to beginning
std::cout << "\033[G";
// print text
std::cout << line << "\n";
// restore horizontal cursor position
std::cout << "\033[u";
// move down <X> number of lines
std::cout << "\033[" << num_of_lines + 1 << "B";
}
I know there are libraries out there for CLI's, but I figured this would be a pretty simple thing to fix. I have tried using ANSI scroll, but without any luck.
You would solve your problem by setting the console cursor to the beginning by using this method:
void setCursor(int x, int y) {
HANDLE hOut;
COORD position;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
position.X = x;
position.Y = y;
SetConsoleCursorPosition(hOut, position);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.