ANSI
The rsp/output
modules comes with support for ANSI, which will
allow you to format and style your console output.
Table of Contents
How to enable / disable
To enable ANSI output, set the RSP_ENABLE_ANSI
option to true
, when importing this project.
By default, the option is set to false
.
CPMAddPackage(
NAME rsp-cmake-scripts
GITHUB_REPOSITORY "https://github.com/rsps/cmake-scripts.git"
VERSION x.y.z
OPTIONS
"RSP_ENABLE_ANSI true"
# ...remaining not shown ...
)
Example
Once you have enabled ANSI for your project, then you can use a series of predefined properties (ANSI escape sequences) are available throughout your cmake scripts. These can be used to format your console output messages.
message("${COLOR_GREEN}${TEXT_ITALIC}Build success${RESTORE}, for ${PROJECT_NAME}")
The above shown example should output a message, where the first part is in green coloured, and italic styled text. The second part should be in normal styled text.
Build success, for < your-cmake-project-name >
Restore
Use the RESTORE
(ESC[0m
)
property if you wish to reset any previous set colour or style.
message("${TEXT_BOLD}Component:${RESTORE} ready")
Component: ready
Text Styles
The following shows the default preset text styles:
Property | Name | Control Sequence Code | Example |
---|---|---|---|
TEXT_BOLD |
bold | 1 | My style |
TEXT_BOLD_RESTORE |
bold (restore) | 22 | My style |
TEXT_DIM |
dim | 2 | My style |
TEXT_DIM_RESTORE |
dim (restore) | 22 | My style |
TEXT_ITALIC |
italic | 3 | My style |
TEXT_ITALIC_RESTORE |
italic (restore) | 23 | My style |
TEXT_UNDERLINE |
underline | 4 | My style |
TEXT_UNDERLINE_RESTORE |
underline (restore) | 24 | My style |
TEXT_BLINK |
blink | 5 | My style |
TEXT_BLINK_RESTORE |
blink (restore) | 25 | My style |
TEXT_INVERSE |
inverse | 7 | My style |
TEXT_INVERSE_RESTORE |
inverse (restore) | 27 | My style |
TEXT_HIDDEN |
hidden | 8 | |
TEXT_HIDDEN_RESTORE |
inverse (restore) | 28 | My style |
TEXT_STRIKETHROUGH |
strikethrough | 9 | My style |
TEXT_STRIKETHROUGH_RESTORE |
strikethrough (restore) | 29 | My style |
Note
Depending on your terminal, not all text styles might be supported!
Colours
The following default foreground and background colours are supported:
Property | Name | Control Sequence Code | Example |
---|---|---|---|
COLOR_BLACK |
black | 30 | My Style |
COLOR_BG_BLACK |
black (background) | 40 | My Style |
COLOR_BRIGHT_BLACK |
black bright | 90 | My Style |
COLOR_BRIGHT_BG_BLACK |
black bright (background) | 100 | My Style |
COLOR_RED |
red | 31 | My Style |
COLOR_BG_RED |
red (background) | 41 | My Style |
COLOR_BRIGHT_RED |
red bright | 91 | My Style |
COLOR_BRIGHT_BG_RED |
red bright (background) | 101 | My Style |
COLOR_GREEN |
green | 32 | My Style |
COLOR_BG_GREEN |
green (background) | 42 | My Style |
COLOR_BRIGHT_GREEN |
green bright | 92 | My Style |
COLOR_BRIGHT_BG_GREEN |
green bright (background) | 102 | My Style |
COLOR_YELLOW |
yellow | 33 | My Style |
COLOR_BG_YELLOW |
yellow (background) | 43 | My Style |
COLOR_BRIGHT_YELLOW |
yellow bright | 93 | My Style |
COLOR_BRIGHT_BG_YELLOW |
yellow bright (background) | 103 | My Style |
COLOR_BLUE |
blue | 34 | My Style |
COLOR_BG_BLUE |
blue (background) | 44 | My Style |
COLOR_BRIGHT_BLUE |
blue bright | 94 | My Style |
COLOR_BRIGHT_BG_BLUE |
blue bright (background) | 104 | My Style |
COLOR_MAGENTA |
magenta | 35 | My Style |
COLOR_BG_MAGENTA |
magenta (background) | 45 | My Style |
COLOR_BRIGHT_MAGENTA |
magenta bright | 95 | My Style |
COLOR_BRIGHT_BG_MAGENTA |
magenta bright (background) | 105 | My Style |
COLOR_CYAN |
cyan | 36 | My Style |
COLOR_BG_CYAN |
cyan (background) | 46 | My Style |
COLOR_BRIGHT_CYAN |
cyan bright | 96 | My Style |
COLOR_BRIGHT_BG_CYAN |
cyan bright (background) | 106 | My Style |
COLOR_WHITE |
white | 37 | My Style |
COLOR_BG_WHITE |
white (background) | 47 | My Style |
COLOR_BRIGHT_WHITE |
white bright | 97 | My Style |
COLOR_BRIGHT_BG_WHITE |
white bright (background) | 107 | My Style |
COLOR_DEFAULT |
default (reset foreground) | 39 | My Style |
COLOR_BG_DEFAULT |
default (reset background) | 49 | My Style |
Note
Depending on your terminal, colours might be rendered different from the above shown examples!
Your own styles
To customise the default preset, set the RSP_ANSI_PRESET
list property, before including the rsp/output
module, and
before enabling ANSI. Use the following format for declaring your own styles and colours:
"<variable> <code>[|<code>]..."
<variable>
: name of your style or colour.<code>
: control sequence code.|
: separator (when you define a style that uses multiple codes)
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# Define your own ANSI preset...
set(RSP_ANSI_PRESET
"RESTORE 0"
"MY_STYLE 1|31|23" # Set style to bold, italic, and red foreground.
"TEXT_BOLD 1"
"TEXT_DIM 2"
# ... remaining not shown ...
# RECOMMENDED: you should cache your preset!
CACHE STRING "My custom ANSI preset"
)
include("rsp/output")
enable_ansi()
endif()
Tip: disable and re-enable
If you have customised the default provided ANSI preset, and your styles and colours have not taken effect, it could be caused of by previous cached preset.
To resolve such an issue, disable and then re-enable ANSI. Doing so will clear previous cached properties and cache your custom preset.