2005-01-30 15:57:25 -05:00
|
|
|
! Copyright (C) 2004, 2005 Slava Pestov.
|
|
|
|
! See http://factor.sf.net/license.txt for BSD license.
|
2004-07-16 02:26:21 -04:00
|
|
|
IN: ansi
|
2005-01-30 15:57:25 -05:00
|
|
|
USING: lists kernel namespaces stdio streams strings
|
|
|
|
presentation generic ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-01-30 15:57:25 -05:00
|
|
|
! <ansi-stream> raps the given stream in an ANSI stream. ANSI
|
|
|
|
! streams support the following character attributes:
|
|
|
|
! bold - if not f, text is boldface.
|
|
|
|
! ansi-fg - foreground color
|
|
|
|
! ansi-bg - background color
|
2004-07-22 19:48:50 -04:00
|
|
|
|
2004-12-10 21:39:27 -05:00
|
|
|
! black 0
|
|
|
|
! red 1
|
|
|
|
! green 2
|
|
|
|
! yellow 3
|
|
|
|
! blue 4
|
|
|
|
! magenta 5
|
|
|
|
! cyan 6
|
|
|
|
! white 7
|
2004-07-16 02:26:21 -04:00
|
|
|
|
|
|
|
: clear ( -- code )
|
|
|
|
#! Clear screen
|
|
|
|
"\e[2J\e[H" ; inline
|
|
|
|
|
|
|
|
: reset ( -- code )
|
|
|
|
#! Reset ANSI color codes.
|
|
|
|
"\e[0m" ; inline
|
|
|
|
|
|
|
|
: bold ( -- code )
|
|
|
|
#! Switch on boldface.
|
|
|
|
"\e[1m" ; inline
|
|
|
|
|
|
|
|
: fg ( color -- code )
|
|
|
|
#! Set foreground color.
|
|
|
|
"\e[3" swap "m" cat3 ; inline
|
|
|
|
|
|
|
|
: bg ( color -- code )
|
|
|
|
#! Set foreground color.
|
|
|
|
"\e[4" swap "m" cat3 ; inline
|
|
|
|
|
2004-08-21 02:55:37 -04:00
|
|
|
: ansi-attrs ( style -- )
|
2004-11-11 15:15:43 -05:00
|
|
|
"bold" over assoc [ bold , ] when
|
|
|
|
"ansi-fg" over assoc [ fg , ] when*
|
|
|
|
"ansi-bg" over assoc [ bg , ] when*
|
2004-08-21 02:55:37 -04:00
|
|
|
drop ;
|
2004-11-28 21:56:58 -05:00
|
|
|
|
2004-08-21 02:55:37 -04:00
|
|
|
: ansi-attr-string ( string style -- string )
|
2004-11-11 15:15:43 -05:00
|
|
|
[ ansi-attrs , reset , ] make-string ;
|
2004-07-16 02:26:21 -04:00
|
|
|
|
2005-01-30 15:57:25 -05:00
|
|
|
WRAPPER: ansi-stream
|
2004-11-28 21:56:58 -05:00
|
|
|
|
|
|
|
M: ansi-stream fwrite-attr ( string style stream -- )
|
2005-01-30 15:57:25 -05:00
|
|
|
>r [ default-style ] unless* ansi-attr-string r>
|
|
|
|
ansi-stream-delegate fwrite ;
|
2004-12-23 16:37:16 -05:00
|
|
|
|
2004-12-29 03:35:46 -05:00
|
|
|
IN: shells
|
|
|
|
|
|
|
|
: ansi
|
|
|
|
stdio [ <ansi-stream> ] change tty ;
|