[m-rev.] for review: options to control the use of color

Peter Wang novalazy at gmail.com
Thu May 2 18:16:00 AEST 2024


On Fri, 26 Apr 2024 06:47:20 +1000 "Zoltan Somogyi" <zoltan.somogyi at runbox.com> wrote:
> +init_color_db(OptionTable) = ColorDb :-
> +    getopt.lookup_bool_option(OptionTable, use_color_diagnostics, UseColors),
> +    (
> +        UseColors = no,
> +        ColorDb = no_color_db
> +    ;
> +        UseColors = yes,
> +        MaybeColorSpecs = convert_color_spec_options(OptionTable),
> +        (
> +            MaybeColorSpecs = error1(_Specs),
> +            % This should not happen, because handle_options.m should
> +            % report _Specs, and not let execution proceed any further.
> +            % But just in case ...
> +            ColorDb = no_color_db
> +        ;
> +            MaybeColorSpecs = ok1(ColorSpecs),
> +            ColorSpecs = color_specs(MaybeCorrect, MaybeIncorrect, MaybeCause),
> +            (
> +                MaybeCorrect = yes(Correct)
> +            ;
> +                MaybeCorrect = no,
> +                Correct = color_8bit(40u8)          % This is green.
> +            ),
> +            (
> +                MaybeIncorrect = yes(Incorrect)
> +            ;
> +                MaybeIncorrect = no,
> +                Incorrect = color_8bit(203u8)       % This is red.
> +            ),
> +            (
> +                MaybeCause = yes(Cause)
> +            ;
> +                MaybeCause = no,
> +                Cause = color_8bit(11u8)            % This is yellow.
> +            ),

I think the compiler should stick to the original 16 colors by default,
(1) for compatibility, and (2) the original 16 colors are the ones that
the user might tweak to taste.

>      % The terminal control codes we use here are described by
>      % https://en.wikipedia.org/wiki/ANSI_escape_code#Colors.
>      %
>  :- func color_to_string(color) = string.
>  
> -color_to_string(color_8bit(ColourNum)) = Str :-
> -    string.format("\033\[38;5;%dm", [i(uint8.cast_to_int(ColourNum))], Str).
> -color_to_string(color_reset) = Str :-
> -    Str =  "\033\[39;49m".
> +color_to_string(SetOrReset) = Str :-
> +    (
> +        SetOrReset = color_set(Color),
> +        Color = color_8bit(ColorNum),
> +        string.format("\033\[38;5;%dm", [i(uint8.cast_to_int(ColorNum))], Str)
> +    ;
> +        SetOrReset = color_reset,
> +        Str =  "\033\[39;49m"
> +    ).

It MIGHT be better to stick to the control codes 30-37, and use the bold
attribute for bright colors, for compatibility with older terminal
emulators.

Peter


More information about the reviews mailing list