how to get current terminal color pair in bash

后端 未结 3 2122
长情又很酷
长情又很酷 2021-02-05 15:25

I would like to query and store the current terminal color pair in BASH e.g.:

#!/bin/bash

#some ANSI colour escape sequences
red=\"\\033[0;31m\"
grn=\"\\033[0;3         


        
3条回答
  •  清歌不尽
    2021-02-05 15:31

    It's important to understand that the terminal state has nothing to do with bash. Bash doesn't care. It simply reads from stdin and writes to stdout and stderr. (See Note 1)

    All terminal effects are implemented by the terminal emulator you happen to be using, of which there are many. In a graphical environment, you might be using, for example, xterm or konsole. You'll need to search the documentation for those emulators for specific terminal control codes which they interpret.

    As far as I know, there is no standard code to get a report of the current terminal state, other than the cursor position (ESC[6n).

    So your best bet is to remember the changes you made when you make them.

    You can find a list of the standard codes implemented by the Linux console using man console_codes (although few people use the Linux console these days); most of those are also interpreted by xterm and other graphical consoles. There's an list of xterm sequences in Thomas Dickey's xterm site; it's a more or less de facto standard for terminal emulators but, as I said, you'll need to search in each emulator's documentation for idiosyncratic control sequences.

    Notes

    1. In interactive mode, bash uses a library called readline to help it handle some terminal effects. In particular, readline tries to maintain the current console cursor position, although it is easy to fool it. In PS1 you need to surround console control sequences with \[ and \] precisely because readline does not know that they are control sequences.

提交回复
热议问题