factor/library/io/ansi.factor

61 lines
1.3 KiB
Factor
Raw Normal View History

! 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
USING: lists kernel namespaces stdio streams strings
presentation generic ;
2004-07-16 02:26:21 -04: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-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 -- )
"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 )
[ ansi-attrs , reset , ] make-string ;
2004-07-16 02:26:21 -04:00
WRAPPER: ansi-stream
2004-11-28 21:56:58 -05:00
M: ansi-stream fwrite-attr ( string style stream -- )
>r [ default-style ] unless* ansi-attr-string r>
ansi-stream-delegate fwrite ;
IN: shells
: ansi
stdio [ <ansi-stream> ] change tty ;