3 # Pretty printing macros.
7 # Copyright (c) 2016 Philippe Proulx <pproulx@efficios.com>
9 # This program is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by the
11 # Free Software Foundation; either version 2 of the License, or (at your
12 # option) any later version.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
17 # Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program. If not, see <https://www.gnu.org/licenses/>.
22 # As a special exception, the respective Autoconf Macro's copyright owner
23 # gives unlimited permission to copy, distribute and modify the configure
24 # scripts that are the output of Autoconf when processing the Macro. You
25 # need not follow the terms of the GNU General Public License when using
26 # or distributing such scripts, even though portions of the text of the
27 # Macro appear in them. The GNU General Public License (GPL) does govern
28 # all other use of the material that constitutes the Autoconf Macro.
30 # This special exception to the GPL applies to versions of the Autoconf
31 # Macro released by the Autoconf Archive. When you make and distribute a
32 # modified version of the Autoconf Macro, you may extend this special
33 # exception to the GPL to apply to your modified version as well.
37 # PPRINT_INIT(): initializes the pretty printing system.
39 # Use this macro before using any other PPRINT_* macro.
40 AC_DEFUN([PPRINT_INIT], [
41 m4_define([PPRINT_CONFIG_TS], [50])
42 m4_define([PPRINT_CONFIG_INDENT], [2])
46 # find tput, which tells us if colors are supported and gives us color codes
47 AC_PATH_PROG([pprint_tput], [tput])
49 AS_IF([test -n "$pprint_tput"], [
50 AS_IF([test -n "$PS1" && test `"$pprint_tput" colors` -eq 256 && test -t 1], [
51 # interactive shell and colors supported and standard output
52 # file descriptor is opened on a terminal
53 PPRINT_COLOR_TXTBLK="`"$pprint_tput" setaf 0`"
54 PPRINT_COLOR_TXTBLU="`"$pprint_tput" setaf 4`"
55 PPRINT_COLOR_TXTGRN="`"$pprint_tput" setaf 2`"
56 PPRINT_COLOR_TXTCYN="`"$pprint_tput" setaf 6`"
57 PPRINT_COLOR_TXTRED="`"$pprint_tput" setaf 1`"
58 PPRINT_COLOR_TXTPUR="`"$pprint_tput" setaf 5`"
59 PPRINT_COLOR_TXTYLW="`"$pprint_tput" setaf 3`"
60 PPRINT_COLOR_TXTWHT="`"$pprint_tput" setaf 7`"
61 PPRINT_COLOR_BLD=`"$pprint_tput" bold`
62 PPRINT_COLOR_BLDBLK="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTBLK"
63 PPRINT_COLOR_BLDBLU="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTBLU"
64 PPRINT_COLOR_BLDGRN="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTGRN"
65 PPRINT_COLOR_BLDCYN="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTCYN"
66 PPRINT_COLOR_BLDRED="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTRED"
67 PPRINT_COLOR_BLDPUR="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTPUR"
68 PPRINT_COLOR_BLDYLW="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTYLW"
69 PPRINT_COLOR_BLDWHT="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTWHT"
70 PPRINT_COLOR_RST="`"$pprint_tput" sgr0`"
73 PPRINT_YES_MSG="$PPRINT_COLOR_BLDGRN$PPRINT_YES_MSG$PPRINT_COLOR_RST"
74 PPRINT_NO_MSG="$PPRINT_COLOR_BLDRED$PPRINT_NO_MSG$PPRINT_COLOR_RST"
77 PPRINT_COLOR_SUBTITLE="$PPRINT_COLOR_BLDCYN"
82 # PPRINT_SET_INDENT(indent): sets the current indentation.
84 # Use PPRINT_INIT() before using this macro.
85 AC_DEFUN([PPRINT_SET_INDENT], [
86 m4_define([PPRINT_CONFIG_INDENT], [$1])
89 # PPRINT_SET_TS(ts): sets the current tab stop.
91 # Use PPRINT_INIT() before using this macro.
92 AC_DEFUN([PPRINT_SET_TS], [
93 m4_define([PPRINT_CONFIG_TS], [$1])
96 # PPRINT_SUBTITLE(subtitle): pretty prints a subtitle.
98 # The subtitle is put as is in a double-quoted shell string so the user
101 # Use PPRINT_INIT() before using this macro.
102 AC_DEFUN([PPRINT_SUBTITLE], [
103 AS_ECHO(["${PPRINT_COLOR_SUBTITLE}$1$PPRINT_COLOR_RST"])
106 AC_DEFUN([_PPRINT_INDENT], [
107 m4_if(PPRINT_CONFIG_INDENT, 0, [
109 m4_for([pprint_i], [0], m4_eval(PPRINT_CONFIG_INDENT * 2 - 1), [1], [
115 # PPRINT_PROP_STRING(title, value, title_color?): pretty prints a
118 # The title is put as is in a double-quoted shell string so the user
121 # The $PPRINT_CONFIG_INDENT variable must be set to the desired indentation
124 # Use PPRINT_INIT() before using this macro.
125 AC_DEFUN([PPRINT_PROP_STRING], [
126 m4_pushdef([pprint_title], [$1])
127 m4_pushdef([pprint_value], [$2])
128 m4_pushdef([pprint_title_color], m4_default([$3], []))
129 m4_pushdef([pprint_title_len], m4_len(pprint_title))
130 m4_pushdef([pprint_spaces_cnt], m4_eval(PPRINT_CONFIG_TS - pprint_title_len - (PPRINT_CONFIG_INDENT * 2) - 1))
132 m4_if(m4_eval(pprint_spaces_cnt <= 0), [1], [
133 m4_define([pprint_spaces_cnt], [1])
136 m4_pushdef([pprint_spaces], [])
138 m4_for([pprint_i], 0, m4_eval(pprint_spaces_cnt - 1), [1], [
139 m4_append([pprint_spaces], [ ])
144 AS_ECHO_N(["pprint_title_color""pprint_title$PPRINT_COLOR_RST:pprint_spaces"])
145 AS_ECHO(["${PPRINT_COLOR_BLD}pprint_value$PPRINT_COLOR_RST"])
147 m4_popdef([pprint_spaces])
148 m4_popdef([pprint_spaces_cnt])
149 m4_popdef([pprint_title_len])
150 m4_popdef([pprint_title_color])
151 m4_popdef([pprint_value])
152 m4_popdef([pprint_title])
155 # PPRINT_PROP_BOOL(title, value, title_color?): pretty prints a boolean
158 # The title is put as is in a double-quoted shell string so the user
161 # The value is evaluated at shell runtime. Its evaluation must be
162 # 0 (false) or 1 (true).
164 # Uses the PPRINT_PROP_STRING() with the "yes" or "no" string.
166 # Use PPRINT_INIT() before using this macro.
167 AC_DEFUN([PPRINT_PROP_BOOL], [
168 m4_pushdef([pprint_title], [$1])
169 m4_pushdef([pprint_value], [$2])
171 test pprint_value -eq 0 && pprint_msg="$PPRINT_NO_MSG" || pprint_msg="$PPRINT_YES_MSG"
174 PPRINT_PROP_STRING(pprint_title, [$pprint_msg], $3)
176 PPRINT_PROP_STRING(pprint_title, [$pprint_msg])
179 m4_popdef([pprint_value])
180 m4_popdef([pprint_title])
183 # PPRINT_PROP_BOOL_CUSTOM(title, value, no_msg, title_color?): pretty prints a boolean
186 # The title is put as is in a double-quoted shell string so the user
189 # The value is evaluated at shell runtime. Its evaluation must be
190 # 0 (false) or 1 (true).
192 # Uses the PPRINT_PROP_STRING() with the "yes" or "no" string.
194 # Use PPRINT_INIT() before using this macro.
195 AC_DEFUN([PPRINT_PROP_BOOL_CUSTOM], [
196 m4_pushdef([pprint_title], [$1])
197 m4_pushdef([pprint_value], [$2])
198 m4_pushdef([pprint_value_no_msg], [$3])
200 test pprint_value -eq 0 && pprint_msg="$PPRINT_NO_MSG (pprint_value_no_msg)" || pprint_msg="$PPRINT_YES_MSG"
203 PPRINT_PROP_STRING(pprint_title, [$pprint_msg], $4)
205 PPRINT_PROP_STRING(pprint_title, [$pprint_msg])
208 m4_popdef([pprint_value_no_msg])
209 m4_popdef([pprint_value])
210 m4_popdef([pprint_title])
213 # PPRINT_WARN(msg): pretty prints a warning message.
215 # The message is put as is in a double-quoted shell string so the user
218 # Use PPRINT_INIT() before using this macro.
219 AC_DEFUN([PPRINT_WARN], [
220 m4_pushdef([pprint_msg], [$1])
223 AS_ECHO(["${PPRINT_COLOR_TXTYLW}WARNING:$PPRINT_COLOR_RST ${PPRINT_COLOR_BLDYLW}pprint_msg$PPRINT_COLOR_RST"])
225 m4_popdef([pprint_msg])
228 # PPRINT_ERROR(msg): pretty prints an error message and exits.
230 # The message is put as is in a double-quoted shell string so the user
233 # Use PPRINT_INIT() before using this macro.
234 AC_DEFUN([PPRINT_ERROR], [
235 m4_pushdef([pprint_msg], [$1])
237 AC_MSG_ERROR([${PPRINT_COLOR_BLDRED}pprint_msg$PPRINT_COLOR_RST])
239 m4_popdef([pprint_msg])