Grioip
Grioip

Reputation: 41

Obtain cursor color in terminal

Is there any way to determine what the color of the cursor is in a terminal running something like vim? I know that you can use tput cols and tput rows to determine the height and width of the terminal, are there similar tools for cursor color/obtaining the ansi standard foreground/background color of any character location in the current terminal?

Upvotes: 3

Views: 604

Answers (2)

Thomas Dickey
Thomas Dickey

Reputation: 54515

Short answer: no

Long answer: the feature, if widely available would be another capability of tput, which allows you to retrieve any of the terminal capabilities for scripting. Those are documented in the terminfo manual page. None of those deal with cursor color, only with the (rather vague) cvvis (very visible), cnorm (normal) and civis (invisible) cursor attributes.

That is, most terminals do not

  • provide a way to set the cursor color or
  • provide a way to retrieve the cursor color

xterm is a rare exception, providing both. But the feature is not often supported in terminals imitating xterm. It is documented in XTerm Control Sequences as part of the dynamic colors feature:

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.

The command-line program xtermcontrol uses these escape sequences to set and get the cursor color:

--cursor=COLOR
    Set cursor color to COLOR.

--get-cursor
    Report cursor color.

For example

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

For what it's worth, it is supported by VTE (e.g., gnome-terminal).

Upvotes: 3

ghoti
ghoti

Reputation: 46856

The answer to your question is "No, there is no standard way to do that."

Consider that your terminal is modelled after ancient text terminals (like DEC VT100 and the like) which communicated with a server over a serial port or modem. And those terminals were in turn modelled after TeleTYpe (tty) devices which were connected to computers back in the 1960s.

Teletype machines ("dumb" terminals) provided no data back to the server that was not typed in to the keyboard. Devices like the VT100 ("smart" terminals) provided VERY LITTLE back to the server, but the list of what's available hasn't changed in many years.

Other resources:

Note that not all terminals are VT100/VT220, and your system may have locak extensions that provide what you need in a non-standard way.

For extra reading, have a look at man termcap and man terminfo. Check out the references in the "SEE ALSO" section of those pages.

Upvotes: 2

Related Questions