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