version: Bump version to 0.4.7.10-dev
[tor.git] / m4 / pprint.m4
blob4e8a71c8765a1c7bde79fed6fa76186ebb93cc0c
1 # Pretty printing macros.
3 # Copyright (C) 2019 - Philippe Proulx <pproulx@efficios.com>
5 # This program is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License, version 2 only, as
7 # published by the Free Software Foundation.
9 # This program is distributed in the hope that it will be useful, but WITHOUT
10 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12 # more details.
14 # You should have received a copy of the GNU General Public License along with
15 # this program; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 # As a special exception to the GNU General Public License, if you distribute
19 # this file as part of a program that contains a configuration script
20 # generated by Autoconf, you may include it under the same distribution terms
21 # that you use for the rest of that program.
23 #serial 1
25 # PPRINT_INIT(): initializes the pretty printing system.
27 # Use this macro before using any other PPRINT_* macro.
28 AC_DEFUN([PPRINT_INIT], [
29   m4_define([PPRINT_CONFIG_TS], [50])
30   m4_define([PPRINT_CONFIG_INDENT], [2])
31   PPRINT_YES_MSG=yes
32   PPRINT_NO_MSG=no
34   # find tput, which tells us if colors are supported and gives us color codes
35   AC_PATH_PROG([pprint_tput], [tput])
37   AS_IF([test -n "$pprint_tput"], [
38     AS_IF([test -n "$PS1" && test `"$pprint_tput" colors` -ge 8 && test -t 1], [
39       # interactive shell and colors supported and standard output
40       # file descriptor is opened on a terminal
41       PPRINT_COLOR_TXTBLK="`"$pprint_tput" setaf 0`"
42       PPRINT_COLOR_TXTBLU="`"$pprint_tput" setaf 4`"
43       PPRINT_COLOR_TXTGRN="`"$pprint_tput" setaf 2`"
44       PPRINT_COLOR_TXTCYN="`"$pprint_tput" setaf 6`"
45       PPRINT_COLOR_TXTRED="`"$pprint_tput" setaf 1`"
46       PPRINT_COLOR_TXTPUR="`"$pprint_tput" setaf 5`"
47       PPRINT_COLOR_TXTYLW="`"$pprint_tput" setaf 3`"
48       PPRINT_COLOR_TXTWHT="`"$pprint_tput" setaf 7`"
49       PPRINT_COLOR_BLD=`"$pprint_tput" bold`
50       PPRINT_COLOR_BLDBLK="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTBLK"
51       PPRINT_COLOR_BLDBLU="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTBLU"
52       PPRINT_COLOR_BLDGRN="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTGRN"
53       PPRINT_COLOR_BLDCYN="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTCYN"
54       PPRINT_COLOR_BLDRED="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTRED"
55       PPRINT_COLOR_BLDPUR="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTPUR"
56       PPRINT_COLOR_BLDYLW="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTYLW"
57       PPRINT_COLOR_BLDWHT="$PPRINT_COLOR_BLD$PPRINT_COLOR_TXTWHT"
58       PPRINT_COLOR_RST="`"$pprint_tput" sgr0`"
60       # colored yes and no
61       PPRINT_YES_MSG="$PPRINT_COLOR_BLDGRN$PPRINT_YES_MSG$PPRINT_COLOR_RST"
62       PPRINT_NO_MSG="$PPRINT_COLOR_BLDRED$PPRINT_NO_MSG$PPRINT_COLOR_RST"
64       # subtitle color
65       PPRINT_COLOR_SUBTITLE="$PPRINT_COLOR_BLDCYN"
66     ])
67   ])
70 # PPRINT_SET_INDENT(indent): sets the current indentation.
72 # Use PPRINT_INIT() before using this macro.
73 AC_DEFUN([PPRINT_SET_INDENT], [
74   m4_define([PPRINT_CONFIG_INDENT], [$1])
77 # PPRINT_SET_TS(ts): sets the current tab stop.
79 # Use PPRINT_INIT() before using this macro.
80 AC_DEFUN([PPRINT_SET_TS], [
81   m4_define([PPRINT_CONFIG_TS], [$1])
84 # PPRINT_SUBTITLE(subtitle): pretty prints a subtitle.
86 # The subtitle is put as is in a double-quoted shell string so the user
87 # needs to escape ".
89 # Use PPRINT_INIT() before using this macro.
90 AC_DEFUN([PPRINT_SUBTITLE], [
91   AS_ECHO(["${PPRINT_COLOR_SUBTITLE}$1$PPRINT_COLOR_RST"])
94 AC_DEFUN([_PPRINT_INDENT], [
95   m4_if(PPRINT_CONFIG_INDENT, 0, [
96   ], [
97     m4_for([pprint_i], [0], m4_eval(PPRINT_CONFIG_INDENT * 2 - 1), [1], [
98       AS_ECHO_N([" "])
99     ])
100   ])
103 # PPRINT_PROP_STRING(title, value, title_color?): pretty prints a
104 # string property.
106 # The title is put as is in a double-quoted shell string so the user
107 # needs to escape ".
109 # The $PPRINT_CONFIG_INDENT variable must be set to the desired indentation
110 # level.
112 # Use PPRINT_INIT() before using this macro.
113 AC_DEFUN([PPRINT_PROP_STRING], [
114   m4_pushdef([pprint_title], [$1])
115   m4_pushdef([pprint_value], [$2])
116   m4_pushdef([pprint_title_color], m4_default([$3], []))
117   m4_pushdef([pprint_title_len], m4_len(pprint_title))
118   m4_pushdef([pprint_spaces_cnt], m4_eval(PPRINT_CONFIG_TS - pprint_title_len - (PPRINT_CONFIG_INDENT * 2) - 1))
120   m4_if(m4_eval(pprint_spaces_cnt <= 0), [1], [
121     m4_define([pprint_spaces_cnt], [1])
122   ])
124   m4_pushdef([pprint_spaces], [])
126   m4_for([pprint_i], 0, m4_eval(pprint_spaces_cnt - 1), [1], [
127     m4_append([pprint_spaces], [ ])
128   ])
130   _PPRINT_INDENT
132   AS_ECHO_N(["pprint_title_color""pprint_title$PPRINT_COLOR_RST:pprint_spaces"])
133   AS_ECHO(["${PPRINT_COLOR_BLD}pprint_value$PPRINT_COLOR_RST"])
135   m4_popdef([pprint_spaces])
136   m4_popdef([pprint_spaces_cnt])
137   m4_popdef([pprint_title_len])
138   m4_popdef([pprint_title_color])
139   m4_popdef([pprint_value])
140   m4_popdef([pprint_title])
143 # PPRINT_PROP_BOOL(title, value, title_color?): pretty prints a boolean
144 # property.
146 # The title is put as is in a double-quoted shell string so the user
147 # needs to escape ".
149 # The value is evaluated at shell runtime. Its evaluation must be
150 # 0 (false) or 1 (true).
152 # Uses the PPRINT_PROP_STRING() with the "yes" or "no" string.
154 # Use PPRINT_INIT() before using this macro.
155 AC_DEFUN([PPRINT_PROP_BOOL], [
156   m4_pushdef([pprint_title], [$1])
157   m4_pushdef([pprint_value], [$2])
159   test pprint_value -eq 0 && pprint_msg="$PPRINT_NO_MSG" || pprint_msg="$PPRINT_YES_MSG"
161   m4_if([$#], [3], [
162     PPRINT_PROP_STRING(pprint_title, [$pprint_msg], $3)
163   ], [
164     PPRINT_PROP_STRING(pprint_title, [$pprint_msg])
165   ])
167   m4_popdef([pprint_value])
168   m4_popdef([pprint_title])
171 # PPRINT_PROP_BOOL_CUSTOM(title, value, no_msg, title_color?): pretty prints a boolean
172 # property.
174 # The title is put as is in a double-quoted shell string so the user
175 # needs to escape ".
177 # The value is evaluated at shell runtime. Its evaluation must be
178 # 0 (false) or 1 (true).
180 # Uses the PPRINT_PROP_STRING() with the "yes" or "no" string.
182 # Use PPRINT_INIT() before using this macro.
183 AC_DEFUN([PPRINT_PROP_BOOL_CUSTOM], [
184   m4_pushdef([pprint_title], [$1])
185   m4_pushdef([pprint_value], [$2])
186   m4_pushdef([pprint_value_no_msg], [$3])
188   test pprint_value -eq 0 && pprint_msg="$PPRINT_NO_MSG (pprint_value_no_msg)" || pprint_msg="$PPRINT_YES_MSG"
190   m4_if([$#], [4], [
191     PPRINT_PROP_STRING(pprint_title, [$pprint_msg], $4)
192   ], [
193     PPRINT_PROP_STRING(pprint_title, [$pprint_msg])
194   ])
196   m4_popdef([pprint_value_no_msg])
197   m4_popdef([pprint_value])
198   m4_popdef([pprint_title])
201 # PPRINT_WARN(msg): pretty prints a warning message.
203 # The message is put as is in a double-quoted shell string so the user
204 # needs to escape ".
206 # Use PPRINT_INIT() before using this macro.
207 AC_DEFUN([PPRINT_WARN], [
208   m4_pushdef([pprint_msg], [$1])
210   _PPRINT_INDENT
211   AS_ECHO(["${PPRINT_COLOR_TXTYLW}WARNING:$PPRINT_COLOR_RST ${PPRINT_COLOR_BLDYLW}pprint_msg$PPRINT_COLOR_RST"])
213   m4_popdef([pprint_msg])
216 # PPRINT_ERROR(msg): pretty prints an error message and exits.
218 # The message is put as is in a double-quoted shell string so the user
219 # needs to escape ".
221 # Use PPRINT_INIT() before using this macro.
222 AC_DEFUN([PPRINT_ERROR], [
223   m4_pushdef([pprint_msg], [$1])
225   AC_MSG_ERROR([${PPRINT_COLOR_BLDRED}pprint_msg$PPRINT_COLOR_RST])
227   m4_popdef([pprint_msg])