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

时间:2017-09-27 18:21:47

标签: console-application rebol red

在某些语言中,我可以在文本周围添加Esc codes,使其在Linux控制台/终端上着色。但是,这似乎不适用于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打印彩色文本输出吗?

2 个答案:

答案 0 :(得分:1)

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

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

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

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

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 "

答案 1 :(得分:0)

   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非常有用。为了保持脚本的可移植性,我有一个操作系统检测功能,着色代码依赖于它。