terminal-color
A Racket library to output colored text to the terminal on any platform, including Windows.
1 Introduction
Racket provides several procedures for outputting data, namely display, displayln, print and write. This library provides a corresponding procedure for each of them with the ability to specify what foreground and background color to use.
The signature for these procedures is compatible with the standard ones, as the foreground and background colors are specified using optional keyword arguments. This means any existing call to the standard procedures can easily be modified to use this library as only the name changes.
See the API section for what is provided and further usage instructions.
2 Requirements
This library is compatible with Racket 5.3.6, 6.x and can be installed using the normal raco pkg commands on any platform.
3 Examples
Examples: | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
4 API
(require terminal-color) | package: terminal-color |
procedure
(output-color-mode? v) → boolean?
v : any/c
Valid modes are
'off
'ansi
- 'win32
NOTE: This ’win32 is deprecated; use ’windows, instead.
'windows
parameter
(current-output-color-mode mode) → void? mode : output-color-mode?
= (guess-output-color-mode)
parameter
(current-display-color-mode mode) → void? mode : output-color-mode?
= (guess-output-color-mode)
NOTE: This current-display-color-mode is deprecated; use current-output-color-mode, instead.
parameter
(current-output-color-fg mode) → void? mode : terminal-color?
= 'default
parameter
(current-output-color-bg mode) → void? mode : terminal-color?
= 'default
procedure
If the output is to a terminal then the operating system is checked: unix-like will use ANSI codes ('ansi) and Windows will use Win32 API calls ('windows). Everything else will do nothing ('off).
procedure
NOTE: This guess-display-color-mode is deprecated; use guess-output-color-mode, instead.
procedure
(terminal-color? v) → boolean?
v : any/c
Valid colors are
'default
'black
'white
'red
'green
'blue
'cyan
'magenta
'yellow
procedure
(display-color datum [out #:fg fg #:bg bg]) → void?
datum : any/c out : output-port? = (current-output-port) fg : terminal-color? = (current-output-color-fg) bg : terminal-color? = (current-output-color-bg)
procedure
(displayln-color datum [out #:fg fg #:bg bg]) → void?
datum : any/c out : output-port? = (current-output-port) fg : terminal-color? = (current-output-color-fg) bg : terminal-color? = (current-output-color-bg)
It’s recommended to use this instead of display-color for strings that end with a new line. This is because it will reset the terminal color before the new line as it can be significant on some terminals.
procedure
(print-color datum [ out quote-depth #:fg fg #:bg bg]) → void? datum : any/c out : output-port? = (current-output-port) quote-depth : (or/c 0 1) = 0 fg : terminal-color? = (current-output-color-fg) bg : terminal-color? = (current-output-color-bg)
procedure
(write-color datum [out #:fg fg #:bg bg]) → void?
datum : any/c out : output-port? = (current-output-port) fg : terminal-color? = (current-output-color-fg) bg : terminal-color? = (current-output-color-bg)