A CMake module providing ANSI text formatting capabilities for colored and styled terminal output.
- Text formatting: Bold, dim, italic, underline, blink, reverse, hidden, strike
- Foreground colors: 16 standard colors (8 normal + 8 bright)
- Background colors: 16 standard colors (8 normal + 8 bright)
- Combined formatting: Mix multiple styles and colors
- Chained output: Build formatted strings from multiple segments
- Cross-platform: Works on both Unix and Windows (with ANSI support)
- Copy
formatText.cmake
to your project's CMake modules directory - Include it in your CMakeLists.txt:
include(formatText.cmake)
# Single formatted message
MessageWithFormat(FORM ${FORM_BOLD} FORE ${FORE_RED} "Error: Something went wrong!")
# Store formatted text in variable
formatText(FORM ${FORM_UNDERLINE} FORE ${FORE_BLUE} "Important notice")
message("${formatted_text}")
# Bold and italic red text
formatText(FORM ${FORM_BOLD} ${FORM_ITALIC} FORE ${FORE_RED} "Critical warning!")
message("${formatted_text}")
# Build a message with multiple formats
formatTextChain(FORM ${FORM_BOLD} FORE ${FORE_GREEN} "Status: ")
formatTextChain(FORM ${FORM_ITALIC} FORE ${FORE_YELLOW} "Running")
formatTextChain(FORM ${FORM_DEFAULT} FORE ${FORE_DEFAULT} " (")
formatTextChain(FORM ${FORM_BOLD} FORE ${FORE_CYAN} "OK")
formatTextChain(FORM ${FORM_DEFAULT} FORE ${FORE_DEFAULT} ")")
message("${formatted_text_chained}")
# Text with background color
formatText(FORE ${FORE_WHITE} BACK ${BACK_RED} " ALERT ")
message("${formatted_text}")
Use 'formatTextChainBreak' to break the format chain and reset to default.
formatTextChainBreak()
formatTextchain(FORM ${FORM_REVERSE} ${FORM_BOLD} FORE ${FORE_YELLOW} "Warning: ")
message("${formatted_text_chained}")
The output will be like this:
MIT License - See LICENSE file for details.