[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Colored output
From: |
Bruno Haible |
Subject: |
Re: Colored output |
Date: |
Tue, 12 Feb 2019 23:46:50 +0100 |
User-agent: |
KMail/5.1.3 (Linux/4.4.0-141-generic; KDE/5.18.0; x86_64; ; ) |
Hi Akim,
> There's something which is not quite clear to me: is it supported to mix
> the ostream based interface with the regular FILE* one?
If you have an ostream_t and a FILE* and they have different destinations
(for example, one writes to stdout and the other one to a file), there is
no problem: you can use them simultaneously.
If you have an ostream_t and a FILE* that have the same destination and
you want to use them simultaneously, then there are two problems:
1) The ostream_t is buffered, and the FILE* stream as well. To avoid
that the output mixed up in ugly ways, you would need to
- flush the ostream_t and its underlying stream, if any (e.g. for
a file_ostream_t, call fflush() on the underlying FILE* stream),
- flush the FILE* stream, through fflush().
2) The term_ostream_t outputs escape sequences for text attributes.
Currently the only ways to force it to go back to the initial state
are: the output of a newline, and ostream_free().
Therefore, in order to avoid that the FILE* stream output gets shown
in an undesired color, you'll need to either output a newline or
close/free the term_ostream_t first, then do the output through the
FILE* stream.
> My initial motivation is to support colored diagnostics, which are built
> by tons of fprintf to stderr. Currently libtextstyle does not support
> formatting primitives, and also, I don't plan to require libtextstyle.
> So I would prefer to stick to fprintf, and which a few well placed
> invocations to libtextstyle.
>
> Is this a supported use case? Or must (or should) everything be moved
> on top of ostreams?
If you are lucky and all your diagnostics end in a newline, you can mix
libtextstyle-enabled diagnostics with fprintf diagnostics without problem.
If you are unlucky, you need to ostream_free() the stream at the end of
every libtextstyle-enabled diagnostic.
> I don't plan to require libtextstyle.
stderr is a global variable. Similarly you may have a global variable,
guarded through #if HAVE_LIBTEXTSTYLE:
#if HAVE_LIBTEXTSTYLE
styled_ostream_t diag_stream;
#endif
and have each of your diagnostic routines be a varargs function that
initially essentially looks like this:
#if HAVE_LIBTEXTSTYLE
char *str = [v]asprintf (...);
styled_stream_write_str (diag_stream, str);
free (str);
#else
[v]fprintf (stderr,...);
#endif
But now, you need to add the CSS classes somewhere. Which means that
you probably need to take that format string apart instead of doing
an asprintf().
Bruno
- Re: Colored output, Bruno Haible, 2019/02/10
- Re: Colored output, Jose E. Marchesi, 2019/02/10
- Re: Colored output, Akim Demaille, 2019/02/11
- Re: Colored output, Bruno Haible, 2019/02/12
- Re: Colored output, Akim Demaille, 2019/02/13
- Re: Colored output, Bruno Haible, 2019/02/13
- Re: Colored output, Akim Demaille, 2019/02/14
- Re: Colored output, Bruno Haible, 2019/02/14
- Re: Colored output, Akim Demaille, 2019/02/15
- Re: Colored output, Bruno Haible, 2019/02/15