PDA

View Full Version : Set console width in c++



Harry.k
May 22nd, 2012, 05:36 AM
In *******, the console width was always 80, so it was easy to make console-guis like cmus and links. In linux, however the width can be changed. This really messed up console programs i ported from windows to linux. is there any way to set the console width? even system() commands would do. I know about "stty cols", but it doesnt resize the window. It would've been fine, but it doesn't work if the terminal window's actual width is less than 80, or whatever you set it to.

Harry.k
May 22nd, 2012, 06:05 AM
Just so i'm clear, i want to set the width of the console window, or at least make sure the width doesnt go below 80 columns. The title and the menu items tend to spread over multiple lines if it is too small.

roelforg
May 22nd, 2012, 06:06 AM
I dunno, i wouldn't like it if an app kept resizing my console....
But have you tried fetching the size and adjusting your offsets with that?

Harry.k
May 22nd, 2012, 10:51 AM
Well, i suppose thats better than resizing the window. How do i get the consoie size? Also, it would be nice to know how to resize the console and restore on exiting.

roelforg
May 22nd, 2012, 10:54 AM
Well, i suppose thats better than resizing the window. How do i get the consoie size? Also, it would be nice to know how to resize the console and restore on exiting.

Ncurses can do this.
http://stackoverflow.com/questions/1811955/ncurses-terminal-size

Harry.k
May 22nd, 2012, 12:18 PM
I know ncurses. I cant use it coz i use cout extensively. It'll totally mess up the display. Is there an alternative?

dwhitney67
May 22nd, 2012, 01:19 PM
I know ncurses. I cant use it coz i use cout extensively. It'll totally mess up the display. Is there an alternative?

A few posts have offered you suggestions. I will not repeat these.

A simple answer to your question is "No"; C++ in itself does not offer any means to control the size of the console (terminal).

Now, using a C++ application, you could launch a terminal, using exec or something similar, or even system(), in which you could then run your "real" application. I do not know if there is a way to prevent the user from resizing the console once it has been launched.

Your application would need to know when to launch a terminal, and when to just run (ie. display menus, output, etc).

Harry.k
May 22nd, 2012, 03:23 PM
Launching a new terminal! That seems like a good idea. I'll google for more info. Other than that, is there any non-ncurses way to get console dimensions?

roelforg
May 22nd, 2012, 03:24 PM
Launching a new terminal! That seems like a good idea. I'll google for more info. Other than that, is there any non-ncurses way to get console dimensions?

The $COLUMNS and $LINES env. vars?

Harry.k
May 23rd, 2012, 01:49 AM
Not sure how to read the environment variables, but i'm sure google does. Thanks, roelfrog and dewhitney67.

Harry.k
November 9th, 2012, 11:20 AM
After months of searching, I've had it. Even a simple bash script to launch a custom sized terminal would do. Anyone?

Vaphell
November 9th, 2012, 11:35 AM
gnome-terminal --geometry 80x24