! See http://factorcode.org/license.txt for BSD license
USING: accessors arrays assocs destructors environment
-formatting fry io io.streams.string io.styles kernel locals
-math math.functions math.ranges math.vectors namespaces
-sequences sequences.extras strings strings.tables ;
+formatting io io.streams.escape-codes io.streams.string
+io.styles kernel math math.functions math.vectors namespaces
+ranges sequences strings strings.tables ;
IN: io.streams.256color
color>rgb '[ _ distance ]
256colors [ keys swap infimum-by ] [ at ] bi ;
-: color>foreground ( color -- string )
+MEMO: color>foreground ( color -- string )
color>256color "\e[38;5;%sm" sprintf ;
-: color>background ( color -- string )
+MEMO: color>background ( color -- string )
color>256color "\e[48;5;%sm" sprintf ;
-: font-styles ( font-style -- string )
- H{
- { bold "\e[1m" }
- { italic "\e[3m" }
- { bold-italic "\e[1m\e[3m" }
- } at "" or ;
-
TUPLE: 256color < filter-writer ;
C: <256color> 256color
-M: 256color stream-format
- [
- [ foreground of [ color>foreground ] [ "" ] if* ]
- [ background of [ color>background ] [ "" ] if* ]
- [ font-style of [ font-styles ] [ "" ] if* ]
- tri 3append [ "\e[0m" surround ] unless-empty
- ] dip stream>> stream-write ;
+M:: 256color stream-format ( str style stream -- )
+ stream stream>> :> out
+ style foreground of [ color>foreground out stream-write t ] [ f ] if*
+ style background of [ color>background out stream-write t ] [ f ] if*
+ style font-style of [ font-styles out stream-write t ] [ f ] if*
+ or or [ "\e[0m" out stream-write ] unless
+ str out stream-write ;
M: 256color make-span-stream
swap <style-stream> <ignore-close-stream> ;