Skip to content

Instantly share code, notes, and snippets.

@duckinator
Created November 28, 2011 04:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duckinator/1399138 to your computer and use it in GitHub Desktop.
Save duckinator/1399138 to your computer and use it in GitHub Desktop.
/**
* Set text colors and attributes for VT100 compatible terminals
* @author eagle2com
*/
Attr: enum {
/* text attribute codes */
/* Reset All Attributes (return to normal mode) */
reset = 0
/* Bright (Usually turns on BOLD) */
bright = 1
/* Dim */
dim = 2
/* Underline */
under = 4
/* Blink (Does this really work?????) */
blink = 5
/* Reverse (swap background and foreground colors) */
reverse = 7
/* Hidden */
hidden = 8
}
Color: enum {
/* Foreground color codes */
black = 30
red = 31
green = 32
yellow = 33
blue = 34
magenta = 35
cyan = 36
grey = 37
white = 38
}
// this should be a constant but gcc cant find the symbol o0
COLOR_FORMAT_STRING := "\033[%dm"
version (unix || apple) {
import unistd
Terminal: class {
/* Background color codes are the same as Foreground + 10
* example: background blue = 34 + 10 = 44
*/
/** Output a terminal code to stdout **/
output: static func(fmt : String, ...) {
if (isatty(STDOUT_FILENO)) {
va : VaList
va_start(va, fmt)
vprintf(fmt toCString(), va)
va_end(va)
}
fflush(stdout)
}
/** Set foreground and background color */
setColor: static func(f,b: Int) {
setFgColor(f)
setBgColor(b)
}
/** Set foreground color */
setFgColor: static func(c: Int) {
if(c >= 30 && c <= 37) {
output(COLOR_FORMAT_STRING, c)
}
}
/** Set background color */
setBgColor: static func(c: Int) {
if(c >= 30 && c <= 37) {
output(COLOR_FORMAT_STRING, c + 10)
}
}
/** Set text attribute */
setAttr: static func(att: Attr) {
if(att >= 0 && att <= 8) {
output(COLOR_FORMAT_STRING, att)
}
}
/* Set reset attribute =) */
/** Reset the terminal colors and attributes */
reset: static func() {
setAttr(Attr reset)
}
}
}
version (!(unix || apple)) {
Terminal: class {
/* Background color codes are the same as Foreground + 10
* example: background blue = 34 + 10 = 44
*/
/** Output a terminal code to stdout **/
output: static func(fmt : String, ...) {}
/** Set foreground and background color */
setColor: static func(f,b: Int) {}
/** Set foreground color */
setFgColor: static func(c: Int) {}
/** Set background color */
setBgColor: static func(c: Int) {}
/** Set text attribute */
setAttr: static func(att: Int) {}
/* Set reset attribute =) */
/** Reset the terminal colors and attributes */
reset: static func() {}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment