代码之家  ›  专栏  ›  技术社区  ›  Romuald Brunet

有没有办法确定终端的背景色?

  •  15
  • Romuald Brunet  · 技术社区  · 16 年前

    我想知道有没有办法确定终端的背景色?


    这可能很重要,因为要由终端应用程序来绘制其窗口的背景,甚至可能不是普通的颜色。

    5 回复  |  直到 7 年前
        1
  •  10
  •   blueyed    11 年前

    我想到了以下几点:

    #!/bin/sh
    #
    # Query a property from the terminal, e.g. background color.
    #
    # XTerm Operating System Commands
    #     "ESC ] Ps;Pt ST"
    
    oldstty=$(stty -g)
    
    # What to query?
    # 11: text background
    Ps=${1:-11}
    
    stty raw -echo min 0 time 0
    # stty raw -echo min 0 time 1
    printf "\033]$Ps;?\033\\"
    # xterm needs the sleep (or "time 1", but that is 1/10th second).
    sleep 0.00000001
    read -r answer
    # echo $answer | cat -A
    result=${answer#*;}
    stty $oldstty
    # Remove escape at the end.
    echo $result | sed 's/[^rgb:0-9a-f/]\+$//'
    

    来源/回购/要点: https://gist.github.com/blueyed/c8470c2aad3381c33ea3

        2
  •  16
  •   efdee    5 年前

    有一个 xterm control sequence

    \e]11;?\a
    

    ( \e \a 分别是ESC和BEL字符。)

    X11 color name ,例如。 rgb:0000/0000/0000 为了黑人。

        3
  •  4
  •   Albert    7 年前

    一些链接:

    例如,来自 Neovim issue 2764 :

    /*
     * Return "dark" or "light" depending on the kind of terminal.
     * This is just guessing!  Recognized are:
     * "linux"         Linux console
     * "screen.linux"   Linux console with screen
     * "cygwin"        Cygwin shell
     * "putty"         Putty program
     * We also check the COLORFGBG environment variable, which is set by
     * rxvt and derivatives. This variable contains either two or three
     * values separated by semicolons; we want the last value in either
     * case. If this value is 0-6 or 8, our background is dark.
     */
    static char_u *term_bg_default(void)
    {
      char_u      *p;
    
      if (STRCMP(T_NAME, "linux") == 0
          || STRCMP(T_NAME, "screen.linux") == 0
          || STRCMP(T_NAME, "cygwin") == 0
          || STRCMP(T_NAME, "putty") == 0
          || ((p = (char_u *)os_getenv("COLORFGBG")) != NULL
              && (p = vim_strrchr(p, ';')) != NULL
              && ((p[1] >= '0' && p[1] <= '6') || p[1] == '8')
              && p[2] == NUL))
        return (char_u *)"dark";
      return (char_u *)"light";
    }
    

    颜色FGBG 环境,从 Gnome BugZilla 733423 :

    在我刚刚在linux上试过的很多终端中,只有urxvt和konsole设置了它(那些没有设置的:xterm、st、terminology和pterm)。Konsole和Urxvt使用不同的语法和语义,即对我来说,Konsole将其设置为“0;15”(尽管我使用的是“黑对淡黄”的配色方案-所以为什么不使用“default”而不是“15”?),而我的urxvt将其设置为“0;违约;“它实际上是黑白相间的,但为什么是三个区域呢?”。所以在这两种情况下,值都不符合您的规范。

    via ):

    def is_dark_terminal_background():
        """
        :return: Whether we have a dark Terminal background color, or None if unknown.
            We currently just check the env var COLORFGBG,
            which some terminals define like "<foreground-color>:<background-color>",
            and if <background-color> in {0,1,2,3,4,5,6,8}, then we have some dark background.
            There are many other complex heuristics we could do here, which work in some cases but not in others.
            See e.g. `here <https://stackoverflow.com/questions/2507337/terminals-background-color>`__.
            But instead of adding more heuristics, we think that explicitly setting COLORFGBG would be the best thing,
            in case it's not like you want it.
        :rtype: bool|None
        """
        if os.environ.get("COLORFGBG", None):
            parts = os.environ["COLORFGBG"].split(";")
            try:
                last_number = int(parts[-1])
                if 0 <= last_number <= 6 or last_number == 8:
                    return True
                else:
                    return False
            except ValueError:  # not an integer?
                pass
        return None  # unknown (and bool(None) == False, i.e. expect light by default)
    
        4
  •  1
  •   Albert    7 年前

    apparently rxvt-only $COLORFGBG ,我不知道还有什么东西存在。大多数人似乎 referring to 怎样 vim does it ,即使这样,也只是一个有根据的猜测。

        5
  •  -1
  •   goose_wh    16 年前

    这里有一篇关于终端颜色设置的文章: http://www.ibm.com/developerworks/linux/library/l-tip-prompt/