Uses backend instead of the system console, and forgets any colors already pushed.
#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.
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#
Goes back to the system console.
How many colors are active on a standard stream: pushed and not yet popped.
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.
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.
Undoes the latest push_console_color on a stream.
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();↗Puts the console back to its normal colors after set_console_color.
Runs code with the console set to color, and resets it afterwards.
with_console_color(.RED, #code print("warning\n"), .BOLD);↗Like print, with the text in color.
print_color("done: %\n", 3, color = .CYAN);
print_color("careful\n", color = .YELLOW, style = .BOLD, to_standard_error = true);↗Types#
How set_console_color draws the text: .NORMAL, .BOLD or .UNDERLINE.
- NORMAL
- BOLD
- UNDERLINE
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
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#
Foreground and background of a console, plus the two modifiers it can show.