diagnostics: use libtextstyle for colored output

Bruno Haible released libtextstyle, a library for colored output based
on CSS.  Let's use it to generate colored diagnostics, provided
libtextstyle is available.

See
https://lists.gnu.org/archive/html/bug-gnulib/2019-01/msg00176.html
https://lists.gnu.org/archive/html/bison-patches/2019-02/msg00073.html
https://lists.gnu.org/archive/html/bison-patches/2019-02/msg00084.html
https://lists.gnu.org/archive/html/bison-patches/2019-03/msg00007.html

* bootstrap.conf (gnulib_modules): Use libtextstyle when possible.
* data/diagnostics.css: New.
* src/complain.c (begin_use_class, end_use_class, flush)
(severity_style, complain_init_color): New.
Use them.
* src/getargs.c (getargs_colors): New.
(getargs): Use it.
Skip --color and --style.
* src/location.h, src/location.c (location_print): Use a style.

* tests/bison.in: Force --color=yes when stderr is a tty.
* tests/local.at: Disable colors during the test suite.
* tests/input.at: Adjust expectations to the extra options passed on
the command line.
This commit is contained in:
Akim Demaille
2019-02-14 06:49:29 +01:00
parent 855fbf1c11
commit f6e38d7ac9
13 changed files with 203 additions and 25 deletions

View File

@@ -31,6 +31,10 @@
#include <progname.h>
#include <quote.h>
#if HAVE_LIBTEXTSTYLE
# include <textstyle.h>
#endif
#include "complain.h"
#include "files.h"
#include "muscle-tab.h"
@@ -501,10 +505,12 @@ static char const short_options[] =
/* Values for long options that do not have single-letter equivalents. */
enum
{
LOCATIONS_OPTION = CHAR_MAX + 1,
PRINT_LOCALEDIR_OPTION,
COLOR_OPTION = CHAR_MAX + 1,
LOCATIONS_OPTION,
PRINT_DATADIR_OPTION,
REPORT_FILE_OPTION
PRINT_LOCALEDIR_OPTION,
REPORT_FILE_OPTION,
STYLE_OPTION
};
static struct option const long_options[] =
@@ -531,7 +537,9 @@ static struct option const long_options[] =
{ "verbose", no_argument, 0, 'v' },
/* Hidden. */
{ "trace", optional_argument, 0, 'T' },
{ "trace", optional_argument, 0, 'T' },
{ "color", optional_argument, 0, COLOR_OPTION },
{ "style", optional_argument, 0, STYLE_OPTION },
/* Output. */
{ "defines", optional_argument, 0, 'd' },
@@ -575,11 +583,33 @@ command_line_location (void)
}
/* Handle the command line options for color support. Do it early, so
that error messages from getargs be also colored as per the user's
request. This is consistent with the way GCC and Clang behave. */
static void
getargs_colors (int argc _GL_UNUSED, char *argv[] _GL_UNUSED)
{
#if HAVE_LIBTEXTSTYLE
for (int i = 1; i < argc; i++)
{
const char *arg = argv[i];
if (STRPREFIX_LIT ("--color=", arg))
handle_color_option (arg + strlen ("--color="));
else if (STRPREFIX_LIT ("--style=", arg))
handle_style_option (arg + strlen ("--style="));
}
complain_init_color ();
#endif
}
void
getargs (int argc, char *argv[])
{
int c;
getargs_colors (argc, argv);
int c;
while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
!= -1)
switch (c)
@@ -723,6 +753,10 @@ getargs (int argc, char *argv[])
yacc_loc = command_line_location ();
break;
case COLOR_OPTION:
/* Handled in getargs_colors. */
break;
case LOCATIONS_OPTION:
muscle_percent_define_ensure ("locations",
command_line_location (), true);
@@ -741,6 +775,10 @@ getargs (int argc, char *argv[])
spec_verbose_file = xstrdup (AS_FILE_NAME (optarg));
break;
case STYLE_OPTION:
/* Handled in getargs_colors. */
break;
default:
usage (EXIT_FAILURE);
}