-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformatText.cmake
More file actions
153 lines (141 loc) · 5.39 KB
/
formatText.cmake
File metadata and controls
153 lines (141 loc) · 5.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# MIT License
#
# Copyright (c) 2025 diverger
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
# ---------------------------------------------------------
#!
# \file formatText.cmake
# \date Friday, 2025/02/21 10:33:28
#
# \author diverger <diverger@live.cn>
#
# \brief CMake text formatting utilities
#
# This utility provides a set of functions to format text with ANSI escape sequences. The features include:
#
# 1. Support for text formatting, foreground and background colors.
# 2. Support printing text with different formatting in a single line.
# 3. Support combined text formatting, such as bold and italic with colors.
#
# Last Modified: Saturday, 2025/03/15 13:46:32
#
# Copyright (c) 2025
# ---------------------------------------------------------
# HISTORY:
#
# ANSI Escape Sequence
# string(ASCII 27 ESC)
# ANSI Formatting
set(FORM_DEFAULT "0")
set(FORM_BOLD "1")
set(FORM_DIM "2")
set(FORM_ITALIC "3")
set(FORM_UNDERLINE "4")
set(FORM_BLINK "5")
set(FORM_REVERSE "7")
set(FORM_HIDDEN "8")
set(FORM_STRIKE "9")
# Foreground Colors
set(FORE_DEFAULT "39")
set(FORE_BLACK "30")
set(FORE_RED "31")
set(FORE_GREEN "32")
set(FORE_YELLOW "33")
set(FORE_BLUE "34")
set(FORE_MAGENTA "35")
set(FORE_CYAN "36")
set(FORE_LIGHT_GRAY "37")
set(FORE_DARK_GRAY "90")
set(FORE_LIGHT_RED "91")
set(FORE_LIGHT_GREEN "92")
set(FORE_LIGHT_YELLOW "93")
set(FORE_LIGHT_BLUE "94")
set(FORE_LIGHT_MAGENTA "95")
set(FORE_LIGHT_CYAN "96")
set(FORE_WHITE "97")
# Background Colors
set(BACK_DEFAULT "49")
set(BACK_BLACK "40")
set(BACK_RED "41")
set(BACK_GREEN "42")
set(BACK_YELLOW "43")
set(BACK_BLUE "44")
set(BACK_MAGENTA "45")
set(BACK_CYAN "46")
set(BACK_LIGHT_GRAY "47")
set(BACK_DARK_GRAY "100")
set(BACK_LIGHT_RED "101")
set(BACK_LIGHT_GREEN "102")
set(BACK_LIGHT_YELLOW "103")
set(BACK_LIGHT_BLUE "104")
set(BACK_LIGHT_MAGENTA "105")
set(BACK_LIGHT_CYAN "106")
set(BACK_WHITE "107")
# Formats given string with colors and writes the result in
# the formatted_text variable, which can be used in the parent scope.
# Example: formatText(FORM FORM_BOLD FORE FORE_BLUE "My blue text")
# formatText(FORM FORM_BOLD FORE FORE_RED "My bold red text")
# formatText(FORM FORM_ITALIC FORE FORE_BLUE "My italic blue text")
# formatText(FORM FORM_BOLD FORM_ITALIC FORE FORE_RED "My bold and italic red text")
function(formatText)
set(options)
set(oneValueArgs FORE BACK)
set(multiValueArgs FORM)
cmake_parse_arguments(ARG "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
# set(_options -E cmake_echo_color --color=always --no-newline)
set(_options -E cmake_echo_color --no-newline)
set(setting_list)
foreach(var_name IN ITEMS ARG_FORM ARG_FORE ARG_BACK)
if(${var_name})
list(APPEND setting_list "${${var_name}}")
endif()
endforeach()
string(ASCII 27 ESC)
set(setting_str "${setting_list}")
set(final_str "${ESC}[${setting_str}m${ARG_UNPARSED_ARGUMENTS}${ESC}[0m")
execute_process(COMMAND ${CMAKE_COMMAND} -E env CLICOLOR_FORCE=1 ${CMAKE_COMMAND} ${_options} ${final_str}
OUTPUT_VARIABLE _formatted_text
ECHO_ERROR_VARIABLE)
set(formatted_text ${_formatted_text} PARENT_SCOPE)
endfunction()
# Break the chained formatted text
function(formatTextChainBreak)
set(formatted_text_chained PARENT_SCOPE)
endfunction()
# Formats given string with colors and appends the result to
# the formatted_text_chained variable, which can be used
# in the parent scope.
# Example: formatTextChain(FORM FORM_BOLD FORE FORE_BLUE "My blue text")
# formatTextChain(FORM FORM_BOLD FORE FORE_RED "My bold red text")
# To print: message("${formatted_text_chained}")
function(formatTextChain)
formatText(${ARGN})
set(formatted_text_chained "${formatted_text_chained}${formatted_text}" PARENT_SCOPE)
endfunction()
# Formats given string with colors and writes the result in
# the formatted_text variable, which can be used in the parent scope.
#
# Example: formatText(FORM FORM_BOLD FORE FORE_BLUE "My blue text")
# formatText(FORM FORM_BOLD FORE FORE_RED "My bold red text")
#
function(MessageWithFormat)
formatText(${ARGN})
message(${formatted_text})
endfunction()