PreviousUpNext

15.4.955  src/lib/src/make-ansi-terminal-escape-sequence.pkg

## make-ansi-terminal-escape-sequence.pkg
#
# Support for ANSI terminal control codes.
# Currently, this support is just for display attributes.

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


###              "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

    api Ansi_Terminal {
        #
        Color = BLACK | RED | GREEN | YELLOW | BLUE | MAGENTA | CYAN | WHITE;

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

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

        set_texttraits:  (fil::Output_Stream, List(Texttrait)) -> Void;                 # Output commands to set the given texttraits; the empty list is "normal".
    };

    # This package is used (only) in:
    #
    #     src/lib/prettyprint/big/src/out/ansi-terminal-prettyprint-output-stream.pkg
    #
    package  ansi_terminal
    : (weak) Ansi_Terminal
    {
        Color = BLACK | RED | GREEN | YELLOW | BLUE | MAGENTA | CYAN | WHITE;

        Texttrait
            = 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 texttrait to integer command 
        #
        fun texttrait_to_cmd (FG c) =>  30 + color_to_cmd c;
            texttrait_to_cmd (BG c) =>  40 + color_to_cmd c;
            texttrait_to_cmd BF     =>   1;
            texttrait_to_cmd UL     =>   4;
            texttrait_to_cmd BLINK  =>   5;
            texttrait_to_cmd REV    =>   7;
            texttrait_to_cmd INVIS  =>   8;
        end;

        fun ansi_escape_sequence (cmd ! rest)
                =>
                cat (   "\x1b["
                    !   int::to_string cmd
                    !   list::fold_backward  cmd_to_string  ["m"]  rest
                    )
                where
                    fun cmd_to_string (cmd, list)
                        =
                        ";"  !  int::to_string cmd  !  list;
                end;

            ansi_escape_sequence [] =>   "";
        end;

        fun to_string []         =>  ansi_escape_sequence [0, 30];
            to_string texttraits =>  ansi_escape_sequence (list::map texttrait_to_cmd texttraits);
        end;

        fun set_texttraits (out_stream, texttraits)
            =
            fil::write (out_stream, to_string texttraits);

    };
end;

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


Comments and suggestions to: bugs@mythryl.org

PreviousUpNext