pprint.m4 7.3 KB

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