Rebol 中的彩色文本控制台输出

Colored text console output in Rebol

在某些语言中,我可以将 Esc codes 放在文本周围,使它们在 Linux console/terminal 上着色。但是,这在 Rebol 中似乎不起作用:

NORMAL: "\e[0m"
RED: "\e[0;31m"

print rejoin["\e[0;31m" "red text" "\e[0m"]

以上代码只产生黑色(通常是彩色)文本输出:

\e[0;31mred text\e[0m

可以在 Linux 终端上使用 Rebol 打印彩色文本输出吗?

您可以类似地在 Rebol/Red 中使用颜色代码。

print "This text is ^[[0;31mred^[[0m text."

#"^["是Rebol/Red中的转义字符。

例如,您可以使用以下代码将提示更改为红色:

system/console/prompt: "^[[31m^[[5D>>^[(B^[[m "
system/console/result: "^[[32m^[[5D==^[(B^[[m"

在 Rebol 3 的 Ren-C 分支中,您可以使用以下(类似)代码更改提示:

system/console/prompt: "^[[31m^[[5D>>^[(B^[[m "
system/console/result: "^[[32m^[[5D==^[(B^[[m "
   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 上非常有效。为了保持脚本的可移植性,我有一个 OS-检测函数,着色代码依赖于它。