Skip to content

Get background colour of terminal

3 messages · Jeff Newmiller, Jan van der Laan

#
I am trying to get the background colour of the terminal R is running 
in. There is a xterm control sequence for that: running the following in 
a suitable terminal returns the background colour:

cat("\033]11;?\033\\")

Except that the result is written to stdin and I can't seem to figure 
out how to get that into a variable in R.

Following a shell script example from stackoverflow [1], I had a little 
bit of success using

cat("\033]11;?\033\\")
Sys.sleep(1)
bg <- readLines(n=1)

where one has to press an enter manually, but this is not something that 
can be put into a function.

Wrapping that shell script into a system call works. That does introduce 
some dependencies, but on systems where this is relevant sh and sed are 
probably present anyway. But I still would prefers an R-solution.

Does anyone know how one can get the background colour of the terminal R 
is running in?

Suggestions other than using the xterm control sequence are, of course, 
also welcome. Perhaps a c-routine that can be wrapped into a Rcpp-call?

Thanks for the help.

Best,
Jan



[1] 
https://stackoverflow.com/questions/2507337/how-to-determine-a-terminals-background-color
#
You can capture stdin using system(), but you should be cautious as this whole mechanism is highly OS-and-configuration-specific. R is not always running connected to a terminal, and not all terminals respond to the same escape commands.
On February 21, 2022 1:25:05 AM PST, Jan van der Laan <rhelp at eoos.dds.nl> wrote:

  
    
#
Thanks. I am not completely sure what you mean with "You can capture 
stdin using system()". How? I had another look at the documentation of 
system and could not figure out how to do this with system.

I know this is really system/terminal specific. Finding out how to 
determine the capabilities of the terminal is another thing on my todo 
list, but I first want to get this to work on my own terminal for which 
I know the capabilities.

Jan
On 21-02-2022 11:15, Jeff Newmiller wrote: