how to get current terminal color pair in bash

后端 未结 3 2132
长情又很酷
长情又很酷 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:37

    You can't; there is no standard control sequence to report the current cursor attributes.

    What does exist, however, is a sequence to save and restore the current cursor position and attributes:

    • \e7 (DECSC) will save the cursor position and attributes.
    • \e8 (DECRC) will restore the saved cursor position and attributes.

    There is no standard way to restore only the cursor attributes; however, as rici mentioned, you can get a report of the current position using \e[6n (DSR), then use the response to manually "un-restore" the cursor position after restoring its position and attributes.

    Again, though, it's probably easier (and better) to just keep track of the colors in your application, rather than making the terminal responsible for that.

提交回复
热议问题