代码之家  ›  专栏  ›  技术社区  ›  rnso

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

  •  0
  • rnso  · 技术社区  · 7 年前

    Esc codes

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

    以上代码仅产生黑色(常用颜色)文本输出:

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

    2 回复  |  直到 7 年前
        1
  •  1
  •   rgchris    7 年前

    同样,您可以使用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 "
    
        2
  •  0
  •   klausnrooster    7 年前
       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上对我来说非常有效。为了保持脚本的可移植性,我有一个操作系统检测功能,着色代码依赖于它。