在终端中获取光标颜色

Obtain cursor color in terminal

有什么方法可以确定终端 运行 中光标的颜色,例如 vim?我知道你可以使用 tput colstput rows 来确定终端的高度和宽度,是否有类似的工具用于光标 color/obtaining ansi 标准 foreground/background 任何字符的颜色当前终端中的位置?

你的问题的答案是"No, there is no standard way to do that."

考虑一下您的终端是仿照通过串行端口或调制解调器与服务器通信的古代文本终端(如 DEC VT100 等)建模的。这些终端又模仿了 1960 年代连接到计算机的 TeleTYpe (tty) 设备。

电传打字机("dumb" 终端)没有向服务器提供未输入键盘的数据。像 VT100("smart" 终端)这样的设备向服务器提供的反馈很少,但是可用的列表多年来没有改变。

其他资源:

请注意,并非所有终端都是 VT100/VT220,并且您的系统可能具有以非标准方式提供您所需内容的 locak 扩展。

如需额外阅读,请查看 man termcapman terminfo。查看这些页面的 "SEE ALSO" 部分中的参考资料。

简答:否

长答案:该功能,如果广泛可用,将是 tput, which allows you to retrieve any of the terminal capabilities for scripting. Those are documented in the terminfo manual 页面的另一个功能。 None 处理光标颜色,仅具有(相当模糊)cvvis(非常可见)、cnorm(正常)和 civis(不可见)光标属性。

也就是说,大多数终端没有

  • 提供一种方法来设置光标颜色或
  • 提供一种方法来检索光标颜色

xterm 是一个罕见的例外,同时提供了两者。但是模仿 xterm 的终端通常不支持该功能。它记录在 XTerm Control Sequences as part of the dynamic colors 特性中:

OSC Ps ; Pt ST
OSC Ps ; Pt BEL
      Set Text Parameters.  For colors and font, if Pt is a "?", the
      control sequence elicits a response which consists of the con-
      trol sequence which would set the corresponding value.  The
      dtterm control sequences allow you to determine the icon name
      and window title.

      The 10 colors (below) which may be set or queried using 1 0
      through 1 9  are denoted dynamic colors, since the correspond-
      ing control sequences were the first means for setting xterm's
      colors dynamically, i.e., after it was started.  They are not
      the same as the ANSI colors.  These controls may be disabled
      using the allowColorOps resource.  At least one parameter is
      expected for Pt.  Each successive parameter changes the next
      color in the list.  The value of Ps tells the starting point
      in the list.  The colors are specified by name or RGB specifi-
      cation as per XParseColor.

      If a "?" is given rather than a name or RGB specification,
      xterm replies with a control sequence of the same form which
      can be used to set the corresponding dynamic color.  Because
      more than one pair of color number and specification can be
      given in one control sequence, xterm can make more than one
      reply.

        Ps = 1 2  -> Change text cursor color to Pt.

命令行程序xtermcontrol使用这些转义序列来设置和获取光标颜色:

--cursor=COLOR
    Set cursor color to COLOR.

--get-cursor
    Report cursor color.

例如

$ xtermcontrol --get-cursor
rgb:0000/0000/0000
$ xtermcontrol --cursor limegreen
$ xtermcontrol --get-cursor
rgb:3232/cdcd/3232

就其价值而言,VTE(例如 gnome-terminal)支持它。