Like Tree3Likes
  • 1 Post By Matticus
  • 1 Post By brewbuck
  • 1 Post By Matticus

Dividing the terminal

This is a discussion on Dividing the terminal within the Linux Programming forums, part of the Platform Specific Boards category; I am doing a quiz in c for project. I can manage most of the code but the basic thing ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    2

    Dividing the terminal

    I am doing a quiz in c for project. I can manage most of the code but the basic thing is i want my output screen(the terminal) to be divided into sections 1 for instructions,others for timer and score the score will be incremented/decremented according to users reply but i want it to be printed in the score box constantly, like throughout the program instru,timer and score should be visible in upper half and lower half i want to change the questions? how can i divide this terminal so?
    Last edited by kulkarnipooja; 10-10-2012 at 01:13 PM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    2
    I will have to download the ncurses.h library for this right?Isn't there any other simpler way?

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    That depends on what you mean by simpler. It's not that hard to download and install ncurses. If you haven't done such a thing before, it's good practice. It's not the "ncurses.h" library. That's just the header file. It also comes with the actual library which contains the compiled object code (or you can get the source code and compile it yourself).

    What OS are you using? If it's windows, then you can look into the Console Functions
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,061
    If you don't mind a quick and dirty approach, you could also try this: Create a 2D character array that will contain everything to be displayed on your "screen." Then just update the appropriate indices and re-print the whole array.

    simplified example:

    Code:
    char screen[5][14];
    
    // load array with values
    // print entire array
    //
    //  01234567890123
    // 0
    // 1 +-----------+
    // 2 | SCORE: 16 |
    // 3 +-----------+
    // 4

    Code:
    // update score to 24
    
    screen[2][10] = '2';
    screen[2][11] = '4';
    
    // print entire array again
    //
    //  01234567890123
    // 0
    // 1 +-----------+
    // 2 | SCORE: 24 |
    // 3 +-----------+
    // 4
    For extra fun, use the extended ASCII codes to create "frames" around your data, if supported by your system.
    Last edited by Matticus; 10-12-2012 at 10:43 AM.
    oogabooga likes this.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by oogabooga View Post
    What OS are you using? If it's windows, then you can look into the Console Functions
    I just realized what a stupid question this is on a Linux forum!

    Matticus has a good idea for a "simpler" approach.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,158
    Quote Originally Posted by kulkarnipooja View Post
    I will have to download the ncurses.h library for this right?Isn't there any other simpler way?
    Why do people ask if there is a simple way to do something that is obviously not simple?
    Salem likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    2,061
    Laziness. That's why there's, as one example, "Learn to Play Guitar In One Day!" videos. It takes many years to hone demanding skills, but many people simply aren't interested in the journey to take them to the desired destination.

    [edit] Did I just answer a rhetorical question? =)
    Last edited by Matticus; 10-17-2012 at 05:34 PM.
    AndiPersti likes this.

  9. #9
    Left
    Join Date
    Oct 2011
    Posts
    816
    Since you are using Linux, you can use ANSI escape codes to control your output. You simply add control sequences to your outputs to clear the screen, clear the end of the line, move to a specific row or column, and so on. The CSI mentioned in the link above is just the string "\033[".

    At the start of your program, you'll want to clear the terminal window. For example,
    Code:
    printf("\033[H\033[2J");
    fflush(stdout);
    Note that since there is no newline at the end of the string we just printed, the C library is likely to cache it. To make sure it is actually emitted, you need to call fflush(stdout); . I won't show it in the following examples, but you need to remember to call it at the point you wish to have all changes actually visible.

    When you wish to refresh the screen, you redraw everything. To avoid flashing, you don't clear the screen, just move to the upper left corner,
    Code:
    printf("\033[H");
    Then, you print the contents of each line. You can either use an array, or a helper function. The important point is that instead of just printing a newline, you print clear the end of this line followed by a newline:
    Code:
    printf("whatever-is-on-this-line\033[K\n");
    After you have printed everything you want, you clear the rest of the screen using
    Code:
    printf("\033[s\033[2J\033[u");
    You can move to any row and column (both start at 1) using
    Code:
    printf("\033[%d;%dH", row, column);
    Some other useful codes:
    Code:
    printf("\033[?25l"); /* Hide cursor */
    printf("\033[?25h"); /* Show cursor */
    printf("\033[s"); /* Save current cursor position */
    printf("\033[u"); /* Restore cursor position */
    I've listed the available color codes here.

    Portability note: These codes work on basically everywhere that has a color-capable command line terminal, except current Windows systems. For details, see the ISO/IEC 6429 and ECMA-48 standards, which describe these. For some reason, Microsoft decided to omit the support from the cmd.exe shell; it used to provide optional support for command.com in ANSI.SYS.

  10. #10
    Registered User
    Join Date
    Nov 2012
    Location
    Some rock floating in space...
    Posts
    32

    Red face

    Based on Nominal Animals posts, I've managed to put together some cheesey macros for ANSI control sequences. They're a bit rough. I plan on spending more time with it and fleshing them out a bit. But here's a first draft for anyone that might find them useful.

    NOTE: I borrowed the listing of ANSI Color codes from a post by Nominal Animal. He should get most of the credit for the codes in this header I've created.

    Code:
    #ifndef __ANSI_H__
    #define __ANSI_H__
    
    /*
     * Some ansi control character sequence macros by Twiki
     *
     * CREDITS: Special thanks to Nominal Animal and Wikipedia for explaining ANSI control codes
     * and a special thanks again to Nominal Animal, without whom this collection of ANSI macros
     * would not be possible.
     *
     * DATE: Thursday, November 15th 2012
     * LICENSE: Public Domain (MMXII)
     */
    
    
    /*
     * Make sure we're compiling on a UNIX or workalike operating system
     *
     * One of these symbols should be defined somewhere either internally by the compiler
     * or in the common includes typical of a c program... tested on OpenBSD and Debian Linux
     * I'm not aware of a compiler system for any *NIX variant in which one of these aren't defined
     */
    #if !(defined(unix) || defined(_unix) || defined(__unix) || defined(__unix__) || defined(_unix_) \
    || defined(UNIX) || defined(_UNIX) || defined(__UNIX) || defined(__UNIX__) || defined(_UNIX_))
    #error "The macros in ansi.h require a unix or workalike operating system to function properly!"
    #endif
    
    /* NOTE: The following color values come from a listing posted on cboard by the user Nominal Animal
     * I've copied them here to complete my ansi.h header which I intend to post on cboard for the purpose
     * of providing others a simple set of macros to use in order to manipulate color ansi terminal displays
     *
     * All credit for the following color values goes to Nominal Animal without whom, this collection of macros
     * would not be possible.
     */
    
    /* Uses Web color names, see http://en.wikipedia.org/wiki/Web_colors */
     
    #define  ALL_NORMAL "\033[0m"
     
    #define  FG_BLACK   "\033[22;30m"
    #define  FG_MAROON  "\033[22;31m"     /* Dark red */
    #define  FG_GREEN   "\033[22;32m"
    #define  FG_OLIVE   "\033[22;33m"     /* Brown */
    #define  FG_NAVY    "\033[22;34m"     /* Dark blue */
    #define  FG_PURPLE  "\033[22;35m"
    #define  FG_TEAL    "\033[22;36m"     /* Dark cyan */
    #define  FG_SILVER  "\033[22;37m"     /* Light gray */
     
    #define  FG_GRAY    "\033[1;30m"      /* Dark gray */
    #define  FG_RED     "\033[1;31m"      /* Light red */
    #define  FG_LIME    "\033[1;32m"      /* Light green */
    #define  FG_YELLOW  "\033[1;33m"
    #define  FG_BLUE    "\033[1;34m"
    #define  FG_FUCHSIA "\033[1;35m"      /* Light purple */
    #define  FG_AQUA    "\033[1;36m"      /* Light cyan */
    #define  FG_WHITE   "\033[1;37m"
     
    #define  BG_BLACK   "\033[40m"
    #define  BG_MAROON  "\033[41m"
    #define  BG_GREEN   "\033[42m"
    #define  BG_OLIVE   "\033[43m"
    #define  BG_NAVY    "\033[44m"
    #define  BG_PURPLE  "\033[45m"
    #define  BG_TEAL    "\033[46m"
    #define  BG_SILVER  "\033[47m"
    
    /* NOTE: Credit for the following macros goes to Nominal Animal, without whom these macros would not
     * be possible. Many of them come directly from Nominal Animal's posts on cboard
     */
    
    /* SETCURPOS(x,y)
     * GOTOXY(x,y)
     * XY(x,y)
     * SETXY(x,y)
     *
     * All of these macros do the same thing... They set the cursor position to the x and y coordinates
     * Where x is the column, and y is the row
     */
    #if defined(GOTOXY)
    #undef GOTOXY
    #endif
    #if defined(XY)
    #undef XY
    #endif
    #if defined(SETXY)
    #undef SETXY
    #endif
    #if defined(SETCURPOS)
    #undef SETCURPOS
    #endif
    #define SETCURPOS(x,y)        printf("\033[%d;%dH",y,x)
    #define SETXY(x,y)        printf("\033[%d;%dH",y,x)
    #define XY(x,y)            printf("\033[%d;%dH",y,x)
    #define GOTOXY(x,y)        printf("\033[%d;%dH",y,x)
    /* CURUP(count)
     * Moves the cursor up count lines
     */
    #if defined(CURUP)
    #undef CURUP
    #endif
    #define CURUP(count)        printf("\033[%dA",count)
    /* CURDOWN(count)
     * Moves the cursor down count lines
     */
    #if defined(CURDOWN)
    #undef CURDOWN
    #endif
    #define CURDOWN(count)        printf("\033[%dB",count)
    /* INCCUR(count)
     * Move the cursor forward by count cells
     */
    #if defined(INCCUR)
    #undef INCCUR
    #endif
    #define INCCUR(count)        printf("\033[%dC",count)
    /* DECCUR(count)
     * Move cursor backward by count cells 
     */
    #if defined(DECCUR)
    #undef DECCUR
    #endif
    #define DECCUR(count)        printf("\033[%dD", count)
    /* DECLINE(n)
     *
     * Decrement cursor n lines up
     */
    #if defined(DECLINE)
    #undef DECLINE
    #endif
    #define DECLINE(n)        printf("\033[%dE",n)
    /* INCLINE(n)
     *
     * Increment cursor n lines down
     */
    #if defined(INCLINE)
    #undef INCLINE
    #endif
    #define INCLINE(n)        printf("\033[%dF",n)
    /* SETCOLUMN(n)
     * SETX(n)
     *
     * Set the current cursor column ( x coordinate ) position to n 
     */
    #if defined(SETCOLUMN)
    #undef SETCOLUMN
    #endif
    #if defined(SETX)
    #undef SETX
    #endif
    #define SETCOLUMN(n)        printf("\033[%dG")
    #define SETX(n)            printf("\033[%dG")
    /* HIDECUR()
     * Takes no arguments... Hides the cursor
     */
    #if defined(HIDECUR)
    #undef HIDECUR
    #endif
    #define HIDECUR()        printf("\033[?25l")
    /* SHOWCUR()
     * Takes no arguments... Shows a previously hidden cursor
     */
    #if defined(SHOWCUR)
    #undef SHOWCUR
    #endif
    #define SHOWCUR()        printf("\033[?25h")
    /* SAVECURPOS()
     * Takes no arguments... Saves the cursor position internally
     */
    #if defined(SAVECURPOS)
    #undef SAVECURPOS
    #endif
    #define SAVECURPOS()        printf("\033[s")
    /* RESTORECURPOS()
     * Takes no arguments... Restores a previously saved cursor position
     */
    #if defined(RESTORECURPOS)
    #undef RESTORECURPOS
    #endif
    #define RESTORECURPOS()        printf("\033[u")
    /* RESETCUR()
     * Takes no arguments... Resets the cursor position to the top left of the screen
     */
    #if defined(RESETCUR)
    #undef RESETCUR
    #endif
    #define RESETCUR()    printf("\033[H")
    /* CLEARSCR()
     * CLS()
     * CLEAR()
     *
     * All three of these macros do the same thing... They clear the screen
     * They don't take any arguments
     *
     */
    #ifdef CLEARSCR
    #undef CLEARSCR
    #endif
    #ifdef CLS
    #undef CLS
    #endif
    #ifdef CLEAR
    #undef CLEAR
    #endif
    #define CLEARSCR()        printf("\033[H\033[2J"); fflush(stdout)
    #define CLS()            printf("\033[H\033[2J"); fflush(stdout)
    #define CLEAR()            printf("\033[H\033[2J"); fflush(stdout)
    /* CLEARTOEOS()
     * CLEOS()
     *
     * Clear from cursor position to end of screen
     */
    #if defined(CLEARTOEOS)
    #undef CLEARTOEOS
    #endif
    #if defined(CLEOS)
    #undef CLEOS
    #endif
    #define CLEARTOEOS()        printf("\033[0J")
    #define CLEOS()            printf("\033[0J")
    /* CLEARTOBOS()
     * CLBOS()
     *
     * Clear from cursor to beginning of screen
     */
    #if defined(CLEARTOBOS)
    #undef CLEARTOBOS
    #endif
    #if defined(CLBOS)
    #undef CLBOS
    #endif
    #define CLEARTOBOS()        printf("\033[1J")
    #define CLBOS()            printf("\033[1J")
    /* CLEARLINE()
     * CLN()
     *
     * Clear the entire contents of the current line
     */
    #if defined(CLEARLINE)
    #undef CLEARLINE
    #endif
    #if defined(CLN)
    #undef CLN
    #endif
    #define CLEARLINE()        printf("\033[2K")
    #define CLN()            printf("\033[2K")
    /* CLEARTOEOL()
     * CLEOL()
     *
     * Clear from cursor position to end of line
     */
    #if defined(CLEARTOEOL)
    #undef CLEARTOEOL
    #endif
    #if defined(CLEOL)
    #undef CLEOL
    #endif
    #define CLEARTOEOL()        printf("\033[0K")
    #define CLEOL()            printf("\033[0K")
    /* CLEARTOBOL()
     * CLBOL()
     *
     * Clear from cursor position to beginning of line
     */
    #if defined(CLEARTOBOL)
    #undef CLEARTOBOL
    #endif
    #if defined(CLBOL)
    #undef CLBOL
    #endif
    #define CLEARTOBOL()        printf("\033[1K")
    #define CLBOL()            printf("\033[1K")
    /* SETFG(color)
     * Sets the foreground text color to "color" specified. "color" must be a string literal containing the 
     * appropriate foreground ANSI color code for the desired color. Above are a listing of FG_XXXX color
     * literals defined to make things easier on the user. e.g. SETFG(FG_GREEN) sets the text color to dark green.
     */
    #if defined(SETFG)
    #undef SETFG
    #endif
    #define SETFG(color)        printf("%s",color)
    /* SETBG(color)
     * Sets the background text color to "color" specified. "color" must be a string literal containing the 
     * appropriate background ANSI color code for the desired color. Above are a listing of BG_XXXX color
     * literals defined to make things easier on the user. e.g. SETBG(FG_NAVY) sets the background color to navy blue.
     */
    #if defined(SETBG)
    #undef SETBG
    #endif
    #define SETBG(color)        printf("%s",color)
    /* SETCOLOR(fg,bg)
     *
     * Sets the foreground and background color to fg (foreground) and bg (background) colors specified. fg and bg must
     * be a string literal containing the appropriate foreground and background ANSI color codes for the desired colors.
     * Above are a listing of FG_XXXX and BG_XXXX color literals defined to make things easier on the user.
     * e.g. SETCOLOR(FG_WHITE,BG_NAVY) sets the foreground color to white and the background color to Navy Blue
     */
    #if defined(SETCOLOR)
    #undef SETCOLOR
    #endif
    #define SETCOLOR(fg,bg)        printf("%s%s",fg,bg)
    /* SCROLLUP(n)
     * SCRUP(n)
     *
     * Scrolls the screen up by n lines.
     */
    #if defined(SCROLLUP)
    #undef SCROLLUP
    #endif
    #if defined(SCRUP)
    #undef SCRUP
    #endif
    #define SCROLLUP(n)        printf("\033[%dS",n)
    /* SCROLLDOWN(n)
     * SCRDOWN(n)
     * SCRDWN(n)
     *
     * Scrolls the screen down by n lines
     */
    #if defined(SCROLLDOWN)
    #undef SCROLLDOWN
    #endif
    #if defined(SCRDOWN)
    #undef SCRDOWN
    #endif
    #if defined(SCRDWN)
    #undef SCRDWN
    #endif
    #define SCROLLDOWN(n)        printf("\033[%dT",n)
    #define SCRDOWN(n)        printf("\033[%dT",n)
    #define SCRDWN(n)        printf("\033[%dT",n)
    
    
    #endif
    /* __ANSI_H__ */
    If you find them useful, then great... if not, then perhaps some suggestions?

    Twiki

  11. #11
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Cool macros - handy for some light-weight CLIs. However I would still recommend you check out ncurses if you ever do anything like this again. As mentioned above, if you're unable to install and use that library, learning to do so will give you some very crucial Linux / C programming skills and is probably simpler than you think. Ncurses also makes it easier to handle much more complicated interactions with the terminal and deals with any inconsistencies between different terminals/emulators that you might come across. These macros seem simple to you now, but if you do much more interaction they will quickly become more complicated to maintain than just using nurses.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Location
    Some rock floating in space...
    Posts
    32

    Exclamation

    Quote Originally Posted by sean View Post
    Cool macros - handy for some light-weight CLIs. However I would still recommend you check out ncurses if you ever do anything like this again. As mentioned above, if you're unable to install and use that library, learning to do so will give you some very crucial Linux / C programming skills and is probably simpler than you think. Ncurses also makes it easier to handle much more complicated interactions with the terminal and deals with any inconsistencies between different terminals/emulators that you might come across. These macros seem simple to you now, but if you do much more interaction they will quickly become more complicated to maintain than just using nurses.
    Yeah. ncurses is probably the best option. These macros are definitely limited. I've actually got a slightly improved version of this header which should compile on non-unix targets.

    One limitation I've already encountered is not being able to determine the terminal dimensions which makes more advanced use of these macros very difficult to say the least...

    And as you mentioned, there's the differences in implementations among ANSI terminal emulators.

    NOTE: One macro which I didn't include in this version which is an absolute must is the following... For anyone using my macros, be sure to add the following to your copy
    Code:
    /* RESET()
     * RESTORE()
     *
     * Reset all attributes to the default 
     */
    #define RESET()        printf("\033[0m")
    #define RESTORE()    printf("\033[0m")
    You'll want to call that macro after setting any colors to restore the terminal defaults ( seems to work )

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 08-28-2011, 09:01 PM
  2. Console, Terminal and Terminal Emulator
    By lehe in forum C Programming
    Replies: 4
    Last Post: 02-15-2009, 08:59 PM
  3. dividing Doubles
    By yukapuka in forum C++ Programming
    Replies: 9
    Last Post: 06-13-2008, 05:39 PM
  4. Please help - dividing doubles
    By hunterdude in forum C++ Programming
    Replies: 4
    Last Post: 08-05-2004, 12:06 AM
  5. Dividing and EOF
    By Tride in forum C Programming
    Replies: 7
    Last Post: 09-27-2003, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21