Thread: system() command /string to maximize the console window

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    system() command /string to maximize the console window

    As the title suggests is there a command to maximise the console window at run time for linux. i managed to find system("@cls || clear") command for clearing the screen again but cant find one for maximizing the window.

    many thanks
    coop

  2. #2
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cooper1200 View Post
    As the title suggests is there a command to maximise the console window at run time for linux. i managed to find system("@cls || clear") command for clearing the screen again but cant find one for maximizing the window.
    There is no easy or portable way to change terminal console window this way, since they are EMULATIONs of terminals.
    About clearing the screen, avoid using system(), for Linux/MacOS console apps use:
    Code:
    fputs( "\x1b[2J\x1b[1;1H", stdout );
    Some terminals supports the sequence (Xterm based):
    Code:
    // change terminal size to 132x50.
    fputs( "\x1b[8;50;132t", stdout );
    See here.

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    Quote Originally Posted by flp1969 View Post

    Code:
    // change terminal size to 132x50.
    fputs( "\x1b[8;50;132t", stdout );
    doesnt seem to work for me but thanks for the clear screen code.

    as a rule should i be writing my code to fit within the standard size console window?

    many thanks
    coop

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cooper1200 View Post
    doesnt seem to work for me but thanks for the clear screen code.
    as a rule should i be writing my code to fit within the standard size console window?
    There is no "standard size console window". You can change the settings wherever you like... In gnome-terminal, for example, choose Edit->Preferences->Unnamed and you'll see.

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by cooper1200 View Post
    doesnt seem to work for me but thanks for the clear screen code.
    In the terminal, try this:
    Code:
    $ echo -e "\e[8;25;80t"
    Here it works! But as I said: If your terminal is not based on XTerm, maybe it doesn't.

  6. #6
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    You should look into libraries like ncurses. It can handle things like clearing the screen, moving the cursor, setting colors, etc in a way that should work on all terminals including Windows terminals I think. Resizing the terminal, however, is not supported on most terminals and is not supported in ncurses. Users also don't really expect programs to resize the terminal either, so it's probably not the best idea to begin with. My terminals are just how I like them, resizing the terminal window would really annoy me.

    That said, to "clear the screen" you can just print 50 or so newlines. It's not the best way, or maybe not even a good way, of doing it, but it's very easy to implement. I used to waste a lot of time making my programs "just right" before they were fully functional and at some point I decided it just wasn't worth it. Make it all work first and worry about details like this later. Add some TODO comments so you can easily search your code for stuff you want to get done but just isn't relevant when you wrote the comment.

  7. #7
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by gaxio View Post
    That said, to "clear the screen" you can just print 50 or so newlines.
    What if there is 60 lines?

    If anybody is worried about WINDOWS, the code below can be used:

    Code:
    #ifndef __WINNT__
      #include <stdio.h>
    
      /* Uses ANSI */
      void clearscreen ( void )
      {
        fputs ( "\x1b[2J\x1b1;1H", stdout );
      }
    #else
      #include <windows.h>
    
      /* Uses Windows API (Windows doest not support ANSI codes) */
      void clearscreen ( void )
      {
        HANDLE hConsole;
        COORD coord = { 0 };
        CONSOLE_SCREEN_BUFFER_INFO csbi;
        DWORD dwConsoleSize, dwWriten;
    
        hConsole = GetStdHandle ( STD_OUTPUT_HANDLE );
        GetConsoleScreenBufferInfo ( hConsole, &csbi );
        dwConsoleSize = csbi.dwSize.X * csbi.dwSize.Y;
    
        FillConsoleOutputCharacter ( hConsole, ( TCHAR ) ' ',
                                     dwConsoleSize, coord, &dwWriten );
        FillConsoleOutputAttribute ( hConsole, csbi.wAttributes,
                                     dwConsoleSize, coord, &dwWriten );
    
        SetConsoleCursorPosition ( hConsole, coord );
      }
    #endif

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    gaxio does ncurses have to be installed on the system running the program? Now you have pointed out that it would annoy you if someone's program messed with how your console was set up i agree it would annoy me too. unfortunatly i hsve drawn my board too big to fit in a 80x25 window (size mine wants to run at) should i try and change the size of the board so it fits or leave it down to the user to maximize the window?
    thanks for your input
    coop

  9. #9
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    flp i have different results to you...
    Code:
    printf("\e[8;50;100t");
    doesnt work if my console is set to xterm but does if its set to xfce

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    62
    I wouldn't stress about displaying anything pretty in a text window. Text user interfaces were obsolete 20 years ago and the only thing most people use them for now is running their shell and a text editor. 80x25 seems to be the default which most terminal windows start at. If someone made their terminal smaller, that's their problem. Just make it fit in 80x25 and don't sweat the little stuff right now. You're not that far away from being able to do graphics in SDL or a GUI in GTK or something. Not that you should run out and attempt that right away, you need to get a bunch more experience first, but it's a goal you should be able to see now.

    As for ncurses, yes. On Linux at least, it'll be compiled as a dynamic library, which means whoever runs your program will also need to have ncurses. That can be as simple as something like sudo apt install libncurses, though. These types of dependencies are why most Linux software comes as packages even if they're just a single binary. Anyone compiling it will also need the header files and stub library, so they'll need to do something like sudo apt install libncurses-dev. So there are extra steps, but that's how things go. This might be a good time to learn how to link to libraries anyway. But pretty much any time you see a terminal program have color and dialog boxes and buttons, or programs like top or vim or less that are interactive and draw things arbitrarily on the screen instead of just with printf, they're almost always using ncurses. If you want to do terminal stuff like that, you can mess around with VT100 escape sequences of just use ncurses, which wraps all that up for you in a relatively easy interface.

    But again, it's not a big deal. I wouldn't sweat it for now. Get checkers working with just printf and scanf and then either try to improve it, or move on. Dwelling on your programs, especially in the beginning, trying to make them perfect and iron out every detail is probably a waste of time. I'm sure you'll look back at the code for this project a month or two from now and cringe. You have so much room for improvement and dwelling on tiny details of your programs won't really help you improve.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command to Maximize Window?
    By litzkrieg in forum C Programming
    Replies: 3
    Last Post: 03-11-2011, 11:58 PM
  2. system() command-line no window
    By yahn in forum C++ Programming
    Replies: 13
    Last Post: 05-05-2008, 11:38 AM
  3. Controlling console command window input
    By DanFraser in forum C# Programming
    Replies: 0
    Last Post: 11-27-2007, 10:34 AM
  4. how to maximize the console window?
    By dongkhoi in forum C Programming
    Replies: 4
    Last Post: 11-09-2006, 09:06 AM
  5. maximize console window
    By detux in forum C Programming
    Replies: 1
    Last Post: 09-30-2006, 10:50 AM

Tags for this Thread