REBOL [
Title: "colorebol"
Date: 14-Jul-2013
File: %colorebol.reb
Version: 1.0.0
Purpose: "Enable switching of terminal font colors and backgrounds etc"
Note: "Includes the clr func for clearing the screen"
]
clr: does [prin "^(page)"]
coloreb: func [
{Use Fore/Red /Green /Yellow /Blue /Magenta /Cyan /White /Reset and even /Black. Viola! Font-color
Similarly Background/Blue etc..., then Style/bright /dim /normal /reset_all and finally Cyclor, which
randomly picks a font color. It needs some polishing}
][cyclor print ["this is all i do. that, and provide a help doc-string"] cyclor]
Fore: make object! [
Colors: ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white" "reset"]
BLACK: does [prin "^[[30m"]
RED: does [prin "^[[31m"]
GREEN: does [prin "^[[32m"]
YELLOW: does [prin "^[[33m"]
BLUE: does [prin "^[[34m"]
MAGENTA: does [prin "^[[35m"]
CYAN: does [prin "^[[36m"]
WHITE: does [prin "^[[37m"]
RESET: does [prin "^[[39m"]
]
Background: make object! [
Colors: ["black" "red" "green" "yellow" "blue" "magenta" "cyan" "white" "reset"]
BLACK: does [prin "^[[40m"]
RED: does [prin "^[[41m"]
GREEN: does [prin "^[[42m"]
YELLOW: does [prin "^[[43m"]
BLUE: does [prin "^[[44m"]
MAGENTA: does [prin "^[[45m"]
CYAN: does [prin "^[[46m"]
WHITE: does [prin "^[[47m"]
RESET: does [prin "^[[49m"]
]
Style: make object! [
Styles: ["bright" "dim" "normal" "reset_all"]
BRIGHT: does [prin "^[[1m"]
DIM: does [prin "^[[2m"]
NORMAL: does [prin "^[[22m"]
RESET_ALL: does [prin "^[[0m"]
]
cyclor: func [] [fore/(to-word fore/colors/(random/only [2 3 4 5 6 7 8]))]
将其放入其他脚本文件中:
do %colorebol.reb
然后像这样使用:
col: has [
"Wrap the colorebol.reb wrappers to reduce visual clutter"
color /red /green /blue /yellow /cyan /magenta /black /white][
if red [color: 'red]
if green [color: 'green]
if blue [color: 'blue]
if yellow [color: 'yellow]
if cyan [color: 'cyan]
if magenta [color: 'magenta]
if black [color: 'black]
if white [color: 'white]
if unixy-os? [fore/(color)]
]
;test it:
col/magenta print "magenta" ;(it works). Maybe just mod /%colorebol.reb?
我对Rebol不是那么流利——我相信还有一种更简洁的方法。但这在GNU/Linux上对我来说非常有效。为了保持脚本的可移植性,我有一个操作系统检测功能,着色代码依赖于它。