better way to terminal reset in c++????

1. Not allowed to use curses libraries. Which I don't know if it would work anyway.

2. Scrolling upward in the terminal should go nowhere - unless the information requested at the time is to big to fit inside one screen. But once you select the next menu you should once again have no space above the visible screen.

3. Now so far in my console c++ program I have managed to keep all the needed information per screen small enough to where it all fits on the view-able screen. When the user goes to move onto the next menu I use the ansi escape char's to blank the screen so that when the next screen comes up there is nothing you can scroll up and see from the past screen. I know you can also use the clear command from system but have been made aware that it is frowned upon. This means that at any given time a user that attempts to scroll upwards through the terminal goes nowhere.

4. Now I have a screen where there is just to much info for one screen and when it displays you can see the data scroll upward as needed. Now I inform the user at this point that they need to scroll upward so they can see all the data.

5. So in keeping with no scrolling upward for old screens - I have found myself using the hated reset system command after displaying screens with so much data that they force a scroll upward to display it all. If anyone reading this is unaware clearing the screen as far as i know with system clear or using ansi escape characters only clears the visible screen and not the data that was forced upward on display. So if you use these after a screen has scrolled upward you then can still access that info visually by scrolling upward which breaks rule 2.

Any ideas that I can use to stay away from system commands that will clear the screen completely. I could have missed an ansi option and I am new to c++.

And scrolling up into blank space is not accepted either.
Last edited on
http://www.cplusplus.com/articles/4z18T05o/

There are a few more (OS specific) ways listed in this article.
should have mentioned were on linux.

the site you referenced me to, which I had already reviewed before, only mentions ways to clear the visible screen and ignores any data above or pushed upward. These methods still allow the user to scroll up in the terminal - I am new so if I am misunderstanding please let me know - thanks

I am guessing that there is no way in C++ other then using the system reset command to blank the contents of the entire terminal and not just the area visible at the time of a clearing effort?

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <unistd.h>
#include <term.h>

void ClearScreen()
  {
  if (!cur_term)
    {
    int result;
    setupterm( NULL, STDOUT_FILENO, &result );
    if (result <= 0) return;
    }

  putp( tigetstr( "clear" ) );
  }


This doesn't erase the current contents of the terminal? I don't use Linux, so I can't check.
I think you have to link to a curses library package for the above function - I looked for the terminfo library and what I got was it was in the ncurses package.

Rule1 violation - unless the above code will work from the libraries already installed.

I speak out of only new knowledge so if I confuse or misunderstand anything - Sorry
Last edited on
I just want to clarify a couple of things.

(1) Your requirements are:
  - You are not allowed to use curses terminal control routines
  - You have to perform terminal control to prevent scrolling information

(2) Who gave you these requirements?

(3) What is your project for?


The problem is that the two requirements are contradictory. The only way around them that I can think of is to use stty to get the terminal height (before your program starts running, and pass it as an argument), then tailor all your output to scroll with exactly n-1 lines of output (much like the more and less commands do).

Also, the idea of just outputting so-called "ANSI codes" to the display is prone to far more failure than using curses.


Is this just because you are trying to do something pretty for a homework assignment? (Because if it is, you are wasting your time. Just let the screen scroll. Your professor can handle it.)
not allowed to use libraries that did not come with the install of g++ and codeblocks.

Going to stick with my RESET for now since there is no other way I can find, and it gets the job done.





Last edited on
You are refusing to use the library designed to do what you want, for the system you want, and instead farming out to a system utility which uses that same library to do what you want.


To get it working, just get your package manager to install the ncurses-dev library. If you have installed your GCC properly then you'll be ready to go. If not, you'll have to copy the curses.h header from the system include path into wherever you stuck your GCC.

Next, in C::B, go to the options menu and tell it to link with libncurses.

That's it. Compile happy. Frankly, this is all explained nicely in the
http://www.tldp.org/HOWTO/NCURSES-Programming-HOWTO/


As for dealing with the backscroll buffer... that is something the terminal emulator handles, and the way to fix that is to start your terminal emulator with no backscroll lines allowed. For example, with xterm, you'll need to start

    xterm -sl 0 ...

Otherwise, if you can deal with resetting the terminal emulator, use the "rs1" terminal capability.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <unistd.h>
#include <term.h>

void ResetTerminal()
  {
  if (!cur_term)
    {
    int result;
    setupterm( NULL, STDOUT_FILENO, &result );
    if (result <= 0) return;
    }
  putp( tigetstr( "rs1" ) );
  }

This may work fine on your system (it works fine in the default term on Fedora 19, BTW), but it may not work like you expect on your user's systems.

Good luck!
So I figured out linking and got the above code to run
-
A command prints 1-100, one number per line, and after it prints these numbers on the screen you see


33
34
35
.
.
.till say 100

and at the bottom it says "scroll up to see more data - Press Enter to continue"

if you scroll up you see 1-32. when I press enter I now see
the new screen but now when I try to scroll up there is still one line # 33- only 33 though and not 1-33 like it is with system clear. also this does not have the delay of system reset.
ok i used

cout << "\x1b[2J\x1b[1;1H" << flush; or system("clear");

before ResetTerminal();

screen now clears quickly with nothing to scroll afterwards. Perfect


wonder why the one line stayed with just the ResetTerminal?

Last edited on
Because the people who wrote the terminal emulator made a mistake. Resetting the terminal is a pretty severe operation, and the fact that you are using a nice GUI emulator only masks some of the ramifications.

Also, I don't know why you refuse to use the terminfo database and instead put out your own VT-100 codes. Once you try to ship your program it will break on other people's terminals. Not to mention that you are asking for trouble using the standard streams instead of putp() to write the escape codes to the terminal.

Why not just put both in one routine?
12
13
  putp( tigetstr( "clear" ) );
  putp( tigetstr( "rs1" ) );

Good luck!
I am following you completely and actually replaced all my ANSI code with the Clear_Screen function that uses the headers from the ncurses library as shown above.

One question as being new to libraries - I have only done a little bit of reading on them. When I make an executable to put to another Linux machine will it contain everything it needs to run by itself or will it need to go out and find something for these ncurses dependents?

Also since when working with limited hardware I understand that more efficient code is preferred.
As suggested above I should put both commands in one routine
1
2
putp( tigetstr( "clear" ) );
  putp( tigetstr( "rs1" ) );


I declared it like this
 
void Clear_Screen(bool RESET = false);


I then call it as
Clear_Screen();

except for the two cases where I need the reset
Clear_Screen(true);

actual function -
1
2
3
4
5
6
7
8
9
10
11
void Clear_Screen(bool RESET){
  if (!cur_term)
        {
            int result;
            setupterm(NULL,STDOUT_FILENO,&result);
    if (result <=0) return;
        }
        putp(tigetstr( "clear"));
        if (RESET == true)
            putp(tigetstr( "rs1"));
}


Now I just need to learn if one way or the other is more efficient.
In this case use the if statement or just reset anyway??

OH yeah Thanks for all the help Duoas
Last edited on
Ah, good.

I don't think there are any efficiency ramifications either way, but your code looks fine. (Line 6's indentation is off...)

Every sane *nix box you'll ever encounter has libncurses.a on it, so all you need to do is compile normally and ship.

If you package it properly, libncurses.a should be listed in the dependencies, so the package manager will install it if, for some oddball reason, it isn't already installed.

Glad to have been of help.
Topic archived. No new replies allowed.