Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing only part of the console output

What I want to do:

The cursor is initially blinking on the top left corner of the screen:

160 characters remaining
_

When I press 'i':

159 characters remaining

i

When I press 'a':

158 characters remaining

ia

When I press 'm':

157 characters remaining

iam

and so on.

What needs to be done(According to me):

  1. Need to clear the only the first three characters of the screen.
  2. Update the newly pressed key on the screen

What I have tried:

I tried to clear the whole screen and write everything that was there previously back on it.

Why I am not happy with what I did:

Because it gives a jerky appearance. And the entry is not smooth.

What I need you to help me with:

Some inbuilt function or some other technique to clear only part of the screen.

My Specs:

Windows XP SP 3

IDE: Visual C++ 2010 Express

like image 805
IcyFlame Avatar asked Feb 18 '23 10:02

IcyFlame


1 Answers

The first thing to understand is that C++ has no conception of a screen, as a standard part of the language. Standard output might be a file, a printer and cout doesn't know the difference.

The screen "device" itself, however, is typically a little smarter, and recognizes some commands. The most widely-implemented of these are '\r' - the carriage return and '\n' - the line feed. '\r' moves the cursor to the beginning of the line, and '\n' advances to the next line, but that's not fit into your needs as you've already tried.

It seems the only way forward here is to use curses (of which ncurses is only one implementation, though the standard one in Linux). It presents you with a virtual screen with various commands to update them. It then takes only the changed portions, and updates the terminal in an optimized way.

It's just an example of the typical C program using ncurses, could be worth to take a look:

#include <ncurses.h>

int main()
{   
    int ch;

    initscr();              /* Start curses mode        */
    raw();                  /* Line buffering disabled  */
    keypad(stdscr, TRUE);   /* We get F1, F2 etc..      */
    noecho();               /* Don't echo() while we do getch */

    printw("Type any character to see it in bold\n");
    ch = getch();           /* If raw() hadn't been called
                             * we have to press enter before it
                             * gets to the program      */

    printw("The pressed key is ");
    attron(A_BOLD);
    printw("%c", ch);
    attroff(A_BOLD);

    refresh();          /* Print it on to the real screen */
    getch();            /* Wait for user input */
    endwin();           /* End curses mode        */

    return 0;
}

The printw() function writes to an "imaginary" screen. It puts stuff into a buffer and updates some flags and does some other internal ncurses bookkeeping. It doesn't actually write anything to your real screen (the console window).

You can do as much printw() writing as you want to, but the stuff doesn't show up on the real screen until your program does something else to cause the "imaginary" screen buffer contents to go to the real screen.

One thing that causes the real screen to be updated from the printw() buffer is refresh() (as the source code example above does).

like image 170
Mikhail Kalashnikov Avatar answered Feb 27 '23 10:02

Mikhail Kalashnikov