Modules

Print_Color

moduleSource

Available on macOS, Linux, Windows, Wasm

#import "Print_Color";

Colored text on the console. print_color prints with a color, with_console_color colors a block of code, and set_console_color with reset_console_color do it by hand. Colors are ANSI codes, which Windows terminals understand too; its older console is driven through the console API instead.

Example↗
print_color("all good\n", color = .GREEN);
with_console_color(.RED, #code print("%\n", "something failed"));

Module parameters#

SAVE_RESTORE_ON_WINDOWS := false
Makes the outermost pop restore the console as it was just before its push, not as it was before the first color ever pushed.
USE_ANSI_CODES_ON_WINDOWS := false
Writes ANSI codes on Windows as well.

Procedures#

(to_standard_error := false) -> s64

How many colors are active on a standard stream: pushed and not yet popped.

(current: u16, color: Console_Color, style: Console_Style = .NORMAL) -> u16

The Windows console attribute word that shows color and style on top of current. A foreground color changes only the foreground and a background color only the background.

(color: Console_Color, style: Console_Style = .NORMAL, to_standard_error := false)

Shows color on a standard stream and remembers what was there. Every push needs a pop; they nest, and each pop restores the enclosing color. A stream that is not a console is not touched, but the push still counts so the pops stay balanced.

(color: Console_Color, style: Console_Style = .NORMAL, to_standard_error := false)
(color: Console_Color, style: Console_Style = .NORMAL, to_standard_error := false)

Switches the text color on standard output, or on standard error with to_standard_error. Everything printed afterwards has it until reset_console_color. Prefer print_color or with_console_color, which can't forget to reset.

set_console_color(.GREEN);
print("ok\n");
reset_console_color();
↗
(to_standard_error := false)
(to_standard_error := false)

Puts the console back to its normal colors after set_console_color.

Types#

:: enum

How set_console_color draws the text: .NORMAL, .BOLD or .UNDERLINE.

NORMAL
BOLD
UNDERLINE
:: enum_flags

A text color, or with a BG_ prefix a background color; HI_ ones are the bright shades. They are the ANSI color codes.

BLACK
RED
GREEN
BLUE
CYAN
MAGENTA
YELLOW
WHITE
HI_BLACK
HI_RED
HI_GREEN
HI_BLUE
HI_CYAN
HI_MAGENTA
HI_YELLOW
HI_WHITE
BG_BLACK
BG_RED
BG_GREEN
BG_BLUE
BG_CYAN
BG_MAGENTA
BG_YELLOW
BG_WHITE
BG_HI_BLACK
BG_HI_RED
BG_HI_GREEN
BG_HI_BLUE
BG_HI_CYAN
BG_HI_MAGENTA
BG_HI_YELLOW
BG_HI_WHITE
:: struct

The console API the colors go through on Windows, or a stand-in for tests: query reads a stream's attributes and says whether it is a console, apply sets them. A stream that is redirected to a file is left alone.

query: (to_standard_error: bool) -> (u16, bool)
apply: (to_standard_error: bool, attributes: u16)

Constants#