]> gitweb.factorcode.org Git - factor.git/commitdiff
io.streams.ansi: faster by caching styles
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 9 Aug 2022 23:04:14 +0000 (16:04 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 9 Aug 2022 23:04:14 +0000 (16:04 -0700)
extra/io/streams/ansi/ansi.factor

index 6a0231bae451cdfcc416a5f7585bb00156a92b25..1717f1b6bdab2693dc6298ae3104fa3f72b191aa 100644 (file)
@@ -41,23 +41,23 @@ CONSTANT: colors H{
     colors [ keys swap infimum-by ] [ at ] bi
     dup 8 >= [ 8 - t ] [ f ] if ;
 
     colors [ keys swap infimum-by ] [ at ] bi
     dup 8 >= [ 8 - t ] [ f ] if ;
 
-: color>foreground ( color -- string )
+MEMO: color>foreground ( color -- string )
     color>ansi [ 30 + ] [ "m" ";1m" ? ] bi* "\e[%d%s" sprintf ;
 
     color>ansi [ 30 + ] [ "m" ";1m" ? ] bi* "\e[%d%s" sprintf ;
 
-: color>background ( color -- string )
+MEMO: color>background ( color -- string )
     color>ansi [ 40 + ] [ "m" ";1m" ? ] bi* "\e[%d%s" sprintf ;
 
 TUPLE: ansi < filter-writer ;
 
 C: <ansi> ansi
 
     color>ansi [ 40 + ] [ "m" ";1m" ? ] bi* "\e[%d%s" sprintf ;
 
 TUPLE: ansi < filter-writer ;
 
 C: <ansi> ansi
 
-M: ansi 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:: ansi 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: ansi make-span-stream
     swap <style-stream> <ignore-close-stream> ;
 
 M: ansi make-span-stream
     swap <style-stream> <ignore-close-stream> ;