Colorful terminal: the R package "xterm256"
By romain francois on Saturday, April 18 2009, 13:02 - xterm256 - Permalink
One of the goal of my forthcoming highlight package for R is to provide syntax highlighting based on evidence gathered from the output of the R parser directly into the R console (more on this later)
While writing the renderer targetting the console, I realized that support for colored text in the console is something that might be useful outside of the highlighter, and then decided to make it an independent package : xterm256
The idea is to use the The 256 color mode of xterm to wrap some text between escape sequences so that when it is cat to the console, the text appears with a background and/or a foreground color. Here is a screenshot from my console:
The package exposes only one function with three arguments: the style
function with arguments :
x
: the text we want to stylebg
: the background color we want to usefg
: the foreground color we want to use
so if you want to print hello world in yellow with a black background, you can do:
cat( style( "hello world", bg = "black", fg = "yellow"), "\n" )
Comments
So useful! Thanks a lot! The function below returns the built-in color names printed in the matching color:
colors2 <- function(col=NULL){
require(xterm256)
cl <- colors()
if(length(col)==0){
for (i in 1:length(cl)){
cat(style(cli, fg=cli), "\n")
}
}
else if(col%in%cl){
cat(style(col, fg=col), "\n")
}
else {
warning(call.=FALSE, "Type either colors() or colors2() to see all the available color names.")
}
}
HIH!
Hi,
Or you can so that:
or
There are also a few functions that are not exposed currently but that might be of interest:
Romain
Your solution is much more elegant than mine, very neat! I think this package will be very useful, among other things, in the quick selection of pleasant colors for EDA plots! Well done!
This package really has a lot of potential. Thanks for your work. I am anxious to see highlight package as well.
Cool. Any way this can be used to change the color of *all* R output? It's quite annoying working on a terminal with everything being the same color. Just having a different color for any output would be extremely helpful, being able to separate the code from the output. Even better would be yet another color for errors.
why do i get the following error?
Warning: dependency âhighlightâ is not available
platform i486-pc-linux-gnu
arch i486
os linux-gnu
system i486, linux-gnu
status
major 2
minor 10.1
year 2009
month 12
day 14
svn rev 50720
language R
version.string R version 2.10.1 (2009-12-14)
> install.packages("xterm256")
Warning in install.packages("xterm256") :
Warning: dependency âhighlightâ is not available
trying URL 'http://rm.mirror.garr.it/mirrors/CR...
Content type 'application/x-gzip' length 11886 bytes (11 Kb)
opened URL
==================================================
downloaded 11 Kb
Error : package 'highlight' required by 'xterm256' could not be found
ERROR: lazy loading failed for package âxterm256â
The downloaded packages are in
Warning message:
In install.packages("xterm256") :
Hi,
is this package intended to give colored R code when programming in the terminal?