[R] Get background colour of terminal

Jan van der Laan rhe|p @end|ng |rom eoo@@dd@@n|
Mon Feb 21 10:25:05 CET 2022



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



More information about the R-help mailing list