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