Comments, optimizations, ideas, fixes etc. welcome (actually another
set of functions for my wannabe-IRC-client)...

/PeO

REBOL [
  Title: "ANSI colors"
]

escape: func [parm [string!]][join "^(1B)[" parm]

get-scr-dims: func [
        "Stores the console dimensions in screen-size block (height, width)"
        /local cons
][
        cons: open/binary [scheme: 'console]
        prin escape "7n" parse to-string copy cons [thru "[" copy screen-size to "R"]
        screen-size: parse (to-string screen-size) ";"
        close cons
]

comment {
irc color                               ansi color (EPIC)
         0: white                       white bold
         1: black                       black
         2: navy                        blue
         3: green                       green
         4: red                         red
         5: maroon              yellow!
   6: purple            purple
   7: orange            red bold
         8:     yellow          yellow bold
   9: lime                      green bold
        10: teal                        cyan
        11: aqua                        aqua bold
        12: blue                        blue bold
        13: fuchsia             purple bold
        14: grey                        black bold
        15:     silver          white plain
}

ansi: func [
        attributes [block!]
] [
        bg: "4"                                                                        
 fg: "3"
        reset: bold_off: underline_off: reverse_off: blink_off:"0"
        bold_on: bold: "1"              underline_on: underline: "4"
        blink_on: blink: "5"    reverse_on: reverse: "7"

        black: "0"              red: "1"                green: "2"      yellow: "3"    
 blue: "4"
        magenta: "5"    purple: "5"     cyan: "6"               aqua: "6"              
 white: "7"

        if error? try [
                return rejoin["^(1B)[" (get first attributes) (get second attributes) 
"m"]
        ][
                return rejoin["^(1B)[" (get first attributes) "m"]
        ]
]

strip-ansi: func[ansitext /local strippedtext] [
        strippedtext: copy ansitext
        ansi-rule: [to "^(1B)[" copy ansicode thru "m" (replace strippedtext ansicode 
"")]
        parse/all ansitext [some ansi-rule to end]
        return strippedtext
]

ansitext: join "Normal " [ansi[underline_on] "underline" ansi[reset] ansi[fg red] " 
Red " ansi[bg white] " Red on White " ansi[reset]]
ansitext: join ansitext [ansi[bold_on] "Bold" ansi[fg blue] " Blue bold " ansi[fg 
purple] ansi[bg green] " Purple bold on green " ansi[reset]]

strippedtext: strip-ansi ansitext
print length? strip-ansi ansitext
print length? strippedtext
print strippedtext
print length? ansitext
print ansitext

Reply via email to