Commit | Line | Data |
---|---|---|
0235b0db MJ |
1 | # SPDX-License-Identifier: MIT |
2 | # | |
1a0399f9 MJ |
3 | # Pretty printing macros. |
4 | # | |
5 | # Author: Philippe Proulx <pproulx@efficios.com> | |
6 | ||
7 | # PPRINT_INIT(): initializes the pretty printing system. | |
8 | # | |
9 | # Use this macro before using any other PPRINT_* macro. | |
10 | AC_DEFUN([PPRINT_INIT], [ | |
a090fee6 MJ |
11 | m4_define([PPRINT_CONFIG_TS], [50]) |
12 | m4_define([PPRINT_CONFIG_INDENT], [2]) | |
1a0399f9 MJ |
13 | PPRINT_YES_MSG=yes |
14 | PPRINT_NO_MSG=no | |
15 | ||
16 | # find tput, which tells us if colors are supported and gives us color codes | |
a090fee6 | 17 | AC_PATH_PROG([pprint_tput], [tput]) |
1a0399f9 MJ |
18 | |
19 | AS_IF([test -n "$pprint_tput"], [ | |
2ddc6522 | 20 | AS_IF([test -n "$PS1" && test `"$pprint_tput" colors` -ge 8 && test -t 1], [ |
1a0399f9 MJ |
21 | # interactive shell and colors supported and standard output |
22 | # file descriptor is opened on a terminal | |
a090fee6 MJ |
23 | PPRINT_COLOR_TXTBLK="`"$pprint_tput" setaf 0`" |
24 | PPRINT_COLOR_TXTBLU="`"$pprint_tput" setaf 4`" | |
25 | PPRINT_COLOR_TXTGRN="`"$pprint_tput" setaf 2`" | |
26 | PPRINT_COLOR_TXTCYN="`"$pprint_tput" setaf 6`" | |
27 | PPRINT_COLOR_TXTRED="`"$pprint_tput" setaf 1`" | |
28 | PPRINT_COLOR_TXTPUR="`"$pprint_tput" setaf 5`" | |
29 | PPRINT_COLOR_TXTYLW="`"$pprint_tput" setaf 3`" | |
30 | PPRINT_COLOR_TXTWHT="`"$pprint_tput" setaf 7`" | |
1a0399f9 | 31 | PPRINT_COLOR_BLD=`"$pprint_tput" bold` |
a090fee6 MJ |
32 | PPRINT_COLOR_BLDBLK="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTBLK" |
33 | PPRINT_COLOR_BLDBLU="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTBLU" | |
34 | PPRINT_COLOR_BLDGRN="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTGRN" | |
35 | PPRINT_COLOR_BLDCYN="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTCYN" | |
36 | PPRINT_COLOR_BLDRED="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTRED" | |
37 | PPRINT_COLOR_BLDPUR="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTPUR" | |
38 | PPRINT_COLOR_BLDYLW="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTYLW" | |
39 | PPRINT_COLOR_BLDWHT="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTWHT" | |
40 | PPRINT_COLOR_RST="`"$pprint_tput" sgr0`" | |
1a0399f9 MJ |
41 | |
42 | # colored yes and no | |
43 | PPRINT_YES_MSG="$PPRINT_COLOR_BLDGRN$PPRINT_YES_MSG$PPRINT_COLOR_RST" | |
44 | PPRINT_NO_MSG="$PPRINT_COLOR_BLDRED$PPRINT_NO_MSG$PPRINT_COLOR_RST" | |
45 | ||
46 | # subtitle color | |
a090fee6 | 47 | PPRINT_COLOR_SUBTITLE="$PPRINT_COLOR_BLDCYN" |
1a0399f9 MJ |
48 | ]) |
49 | ]) | |
50 | ]) | |
51 | ||
52 | # PPRINT_SET_INDENT(indent): sets the current indentation. | |
53 | # | |
54 | # Use PPRINT_INIT() before using this macro. | |
55 | AC_DEFUN([PPRINT_SET_INDENT], [ | |
a090fee6 | 56 | m4_define([PPRINT_CONFIG_INDENT], [$1]) |
1a0399f9 MJ |
57 | ]) |
58 | ||
59 | # PPRINT_SET_TS(ts): sets the current tab stop. | |
60 | # | |
61 | # Use PPRINT_INIT() before using this macro. | |
62 | AC_DEFUN([PPRINT_SET_TS], [ | |
a090fee6 | 63 | m4_define([PPRINT_CONFIG_TS], [$1]) |
1a0399f9 MJ |
64 | ]) |
65 | ||
66 | # PPRINT_SUBTITLE(subtitle): pretty prints a subtitle. | |
67 | # | |
68 | # The subtitle is put as is in a double-quoted shell string so the user | |
69 | # needs to escape ". | |
70 | # | |
71 | # Use PPRINT_INIT() before using this macro. | |
72 | AC_DEFUN([PPRINT_SUBTITLE], [ | |
a090fee6 | 73 | AS_ECHO(["${PPRINT_COLOR_SUBTITLE}$1$PPRINT_COLOR_RST"]) |
1a0399f9 MJ |
74 | ]) |
75 | ||
76 | AC_DEFUN([_PPRINT_INDENT], [ | |
77 | m4_if(PPRINT_CONFIG_INDENT, 0, [ | |
78 | ], [ | |
a090fee6 MJ |
79 | m4_for([pprint_i], [0], m4_eval(PPRINT_CONFIG_INDENT * 2 - 1), [1], [ |
80 | AS_ECHO_N([" "]) | |
1a0399f9 MJ |
81 | ]) |
82 | ]) | |
83 | ]) | |
84 | ||
85 | # PPRINT_PROP_STRING(title, value, title_color?): pretty prints a | |
86 | # string property. | |
87 | # | |
88 | # The title is put as is in a double-quoted shell string so the user | |
89 | # needs to escape ". | |
90 | # | |
91 | # The $PPRINT_CONFIG_INDENT variable must be set to the desired indentation | |
92 | # level. | |
93 | # | |
94 | # Use PPRINT_INIT() before using this macro. | |
95 | AC_DEFUN([PPRINT_PROP_STRING], [ | |
96 | m4_pushdef([pprint_title], [$1]) | |
97 | m4_pushdef([pprint_value], [$2]) | |
a090fee6 | 98 | m4_pushdef([pprint_title_color], m4_default([$3], [])) |
1a0399f9 MJ |
99 | m4_pushdef([pprint_title_len], m4_len(pprint_title)) |
100 | m4_pushdef([pprint_spaces_cnt], m4_eval(PPRINT_CONFIG_TS - pprint_title_len - (PPRINT_CONFIG_INDENT * 2) - 1)) | |
101 | ||
a090fee6 MJ |
102 | m4_if(m4_eval(pprint_spaces_cnt <= 0), [1], [ |
103 | m4_define([pprint_spaces_cnt], [1]) | |
1a0399f9 MJ |
104 | ]) |
105 | ||
106 | m4_pushdef([pprint_spaces], []) | |
107 | ||
a090fee6 | 108 | m4_for([pprint_i], 0, m4_eval(pprint_spaces_cnt - 1), [1], [ |
1a0399f9 MJ |
109 | m4_append([pprint_spaces], [ ]) |
110 | ]) | |
111 | ||
112 | _PPRINT_INDENT | |
113 | ||
a090fee6 MJ |
114 | AS_ECHO_N(["pprint_title_color""pprint_title$PPRINT_COLOR_RST:pprint_spaces"]) |
115 | AS_ECHO(["${PPRINT_COLOR_BLD}pprint_value$PPRINT_COLOR_RST"]) | |
1a0399f9 MJ |
116 | |
117 | m4_popdef([pprint_spaces]) | |
118 | m4_popdef([pprint_spaces_cnt]) | |
119 | m4_popdef([pprint_title_len]) | |
120 | m4_popdef([pprint_title_color]) | |
121 | m4_popdef([pprint_value]) | |
122 | m4_popdef([pprint_title]) | |
123 | ]) | |
124 | ||
125 | # PPRINT_PROP_BOOL(title, value, title_color?): pretty prints a boolean | |
126 | # property. | |
127 | # | |
128 | # The title is put as is in a double-quoted shell string so the user | |
129 | # needs to escape ". | |
130 | # | |
131 | # The value is evaluated at shell runtime. Its evaluation must be | |
132 | # 0 (false) or 1 (true). | |
133 | # | |
134 | # Uses the PPRINT_PROP_STRING() with the "yes" or "no" string. | |
135 | # | |
136 | # Use PPRINT_INIT() before using this macro. | |
137 | AC_DEFUN([PPRINT_PROP_BOOL], [ | |
138 | m4_pushdef([pprint_title], [$1]) | |
139 | m4_pushdef([pprint_value], [$2]) | |
140 | ||
141 | test pprint_value -eq 0 && pprint_msg="$PPRINT_NO_MSG" || pprint_msg="$PPRINT_YES_MSG" | |
142 | ||
a090fee6 MJ |
143 | m4_if([$#], [3], [ |
144 | PPRINT_PROP_STRING(pprint_title, [$pprint_msg], $3) | |
145 | ], [ | |
146 | PPRINT_PROP_STRING(pprint_title, [$pprint_msg]) | |
147 | ]) | |
148 | ||
149 | m4_popdef([pprint_value]) | |
150 | m4_popdef([pprint_title]) | |
151 | ]) | |
152 | ||
153 | # PPRINT_PROP_BOOL_CUSTOM(title, value, no_msg, title_color?): pretty prints a boolean | |
154 | # property. | |
155 | # | |
156 | # The title is put as is in a double-quoted shell string so the user | |
157 | # needs to escape ". | |
158 | # | |
159 | # The value is evaluated at shell runtime. Its evaluation must be | |
160 | # 0 (false) or 1 (true). | |
161 | # | |
162 | # Uses the PPRINT_PROP_STRING() with the "yes" or "no" string. | |
163 | # | |
164 | # Use PPRINT_INIT() before using this macro. | |
165 | AC_DEFUN([PPRINT_PROP_BOOL_CUSTOM], [ | |
166 | m4_pushdef([pprint_title], [$1]) | |
167 | m4_pushdef([pprint_value], [$2]) | |
168 | m4_pushdef([pprint_value_no_msg], [$3]) | |
169 | ||
170 | test pprint_value -eq 0 && pprint_msg="$PPRINT_NO_MSG (pprint_value_no_msg)" || pprint_msg="$PPRINT_YES_MSG" | |
171 | ||
172 | m4_if([$#], [4], [ | |
173 | PPRINT_PROP_STRING(pprint_title, [$pprint_msg], $4) | |
1a0399f9 | 174 | ], [ |
a090fee6 | 175 | PPRINT_PROP_STRING(pprint_title, [$pprint_msg]) |
1a0399f9 MJ |
176 | ]) |
177 | ||
a090fee6 | 178 | m4_popdef([pprint_value_no_msg]) |
1a0399f9 MJ |
179 | m4_popdef([pprint_value]) |
180 | m4_popdef([pprint_title]) | |
181 | ]) | |
182 | ||
183 | # PPRINT_WARN(msg): pretty prints a warning message. | |
184 | # | |
185 | # The message is put as is in a double-quoted shell string so the user | |
186 | # needs to escape ". | |
187 | # | |
188 | # Use PPRINT_INIT() before using this macro. | |
189 | AC_DEFUN([PPRINT_WARN], [ | |
190 | m4_pushdef([pprint_msg], [$1]) | |
191 | ||
192 | _PPRINT_INDENT | |
a090fee6 | 193 | AS_ECHO(["${PPRINT_COLOR_TXTYLW}WARNING:$PPRINT_COLOR_RST ${PPRINT_COLOR_BLDYLW}pprint_msg$PPRINT_COLOR_RST"]) |
1a0399f9 MJ |
194 | |
195 | m4_popdef([pprint_msg]) | |
196 | ]) | |
197 | ||
198 | # PPRINT_ERROR(msg): pretty prints an error message and exits. | |
199 | # | |
200 | # The message is put as is in a double-quoted shell string so the user | |
201 | # needs to escape ". | |
202 | # | |
203 | # Use PPRINT_INIT() before using this macro. | |
204 | AC_DEFUN([PPRINT_ERROR], [ | |
205 | m4_pushdef([pprint_msg], [$1]) | |
206 | ||
a090fee6 | 207 | AC_MSG_ERROR([${PPRINT_COLOR_BLDRED}pprint_msg$PPRINT_COLOR_RST]) |
1a0399f9 MJ |
208 | |
209 | m4_popdef([pprint_msg]) | |
210 | ]) |