PreviousUpNext

15.4.861  src/lib/src/ansi-term.pkg

## ansi-term.pkg
## All rights reserved.

# Compiled by:
#     src/lib/std/standard.lib

# Support for ANSI terminal control codes.  Currently, this support
# is just for display attributes.

###              "It would appear that we have reached the limits
###               of what it is possible to achieve with computer
###               technology, although one should be careful with
###               such statements, as they tend to sound pretty
###               silly in 5 years."
###
###                                  -- Johnny von Neuman, 1949


stipulate
    package fil =  file__premicrothread;                                # file__premicrothread  is from   src/lib/std/src/posix/file--premicrothread.pkg
herein

    package ansi_terminal: (weak)
    api {

        Color = BLACK | RED | GREEN | YELLOW | BLUE | MAGENTA | CYAN | WHITE;

        Style
            = FG  Color #  foreground color 
            | BG  Color #  Background color 
            | BF                #  Bold/bright 
            | UL                #  underline 
            | BLINK
            | REV               #  reverse video 
            | INVIS;    #  invisible 

        # Return the command string for the given styles; the empty list is "normal" 
        to_string:  List( Style ) -> String;

        # Output commands to set the given styles; the empty list is "normal" 
        set_style:  (fil::Output_Stream, List(Style)) -> Void;

    }
    {
        Color = BLACK | RED | GREEN | YELLOW | BLUE | MAGENTA | CYAN | WHITE;

        Style
            = FG  Color #  foreground color 
            | BG  Color #  Background color 
            | BF                #  Bold 
            | UL                #  underline 
            | BLINK
            | REV               #  reverse video 
            | INVIS;            #  invisible 

        # Basic color codes 
        #
        fun color_to_cmd BLACK   => 0;
            color_to_cmd RED     => 1;
            color_to_cmd GREEN   => 2;
            color_to_cmd YELLOW  => 3;
            color_to_cmd BLUE    => 4;
            color_to_cmd MAGENTA => 5;
            color_to_cmd CYAN    => 6;
            color_to_cmd WHITE   => 7;
        end;

        # Convert style to integer command 
        #
        fun style_to_cmd (FG c) =>  30 + color_to_cmd c;
            style_to_cmd (BG c) =>  40 + color_to_cmd c;
            style_to_cmd BF     =>   1;
            style_to_cmd UL     =>   4;
            style_to_cmd BLINK  =>   5;
            style_to_cmd REV    =>   7;
            style_to_cmd INVIS  =>   8;
        end;

        fun cmd_string []
                =>
                "";

            cmd_string (cmd ! r)
                =>
                {   fun f (cmd, l)
                        =
                        ";" ! int::to_string cmd ! l;

                    cat ("\027[" ! int::to_string cmd ! list::fold_backward f ["m"] r);
                };
        end;

        fun to_string []   =>  cmd_string [0, 30];
            to_string stys =>  cmd_string (list::map style_to_cmd stys);
        end;

        fun set_style (out_strm, stys)
            =
            fil::write (out_strm, to_string stys);

    };
end;

## COPYRIGHT (c) 2005 John Reppy (http://www.cs.uchicago.edu/~jhr)
## Subsequent changes by Jeff Prothero Copyright (c) 2010-2013,
## released per terms of SMLNJ-COPYRIGHT.


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext