Using the “alternate screen” in a bash script

后端 未结 3 1340
深忆病人
深忆病人 2020-11-30 21:23

The alternate screen is used by many \"user-interactive\" terminal applications like vim, htop, screen, alsamixer, less, ... It is like a different buffer of the te

相关标签:
3条回答
  • 2020-11-30 21:50

    smcup/rmcup are used, but only for the side effect: the escape sequence which switches between normal/alternate screens is usually embedded in those terminfo capabilities -- not always.

    Some background is in the xterm faq Why doesn't the screen clear when running vi?

    0 讨论(0)
  • 2020-11-30 22:09

    You can switch to the alternate screen using this command:

    $ tput smcup
    

    And back with:

    $ tput rmcup
    

    These commands just output the appropriate escape sequences for your terminal. If it is an XTERM they will be equivalent to the (more known but less elegant or portable):

    $ echo -e "\e[?1049h"
    

    And:

    $ echo -e "\e[?1049l"
    

    For more terminal control commands see man 5 terminfo.

    0 讨论(0)
  • 2020-11-30 22:12

    For C console application:

    ncurses

    Wikipedia:

    ncurses (new curses) is a programming library that provides an API which allows the programmer to write text-based user interfaces in a terminal-independent manner.

    less uses this library.

    A hello world program from here, to compile it in gcc, flag -lncurses is needed.

    #include <ncurses.h>
    
    int main()
    {   
        initscr();          /* Start curses mode          */
        printw("Hello World !!!");  /* Print Hello World          */
        refresh();          /* Print it on to the real screen */
        getch();            /* Wait for user input */
        endwin();           /* End curses mode        */
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题