[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
As a programmer, enabling styling consists of the following tasks:
styled_ostream_begin_css_class
,
styled_ostream_end_css_class
around each run of text with a
specific text role.
libtextstyle
. If your package is using GNU autoconf,
you can use the libtextstyle.m4
macro from Gnulib.
The following sections go into more detail.
Source code that makes use of GNU libtextstyle needs an include statement:
#include <textstyle.h> |
Basic use of GNU libtextstyle consists of statements like these:
styled_ostream_t stream = styled_ostream_create (STDOUT_FILENO, "(stdout)", TTYCTL_AUTO, style_file_name); ... styled_ostream_begin_use_class (stream, css_class); ... ostream_write_str (stream, string); ... styled_ostream_end_use_class (stream, css_class); ... styled_ostream_free (stream); |
Before this snippet, your code needs to determine the name of the style
file to use (style_file_name
). If no styling is desired – the
precise condition depends on the value of color_mode
but also on
your application logic –, you should set style_file_name
to
NULL
.
An object of type styled_ostream_t
is allocated. The function
styled_ostream_create
allocates it; the function
styled_ostream_free
deallocates it.
Such styled_ostream_t
supports output operations
(ostream_write_str
), interleaved with adding and removing CSS
classes. The CSS class in effect when an output operation is performed
determines, through the style file, the text attributes associated with
that piece of text.
Text output may contain hyperlinks. These hyperlinks are encoded through
an escape sequence, specified at
Hyperlinks in terminal emulators. Currently (as of 2019), they are
displayed only in gnome-terminal
version 3.26 or above. More
terminal emulators will support hyperlinks in the future. Terminal
emulators which don't support hyperlinks ignore it, except for a few
terminal emulators, for which users may need to disable the hyperlinks
(see The environment variable NO_TERM_HYPERLINKS
) if the heuristic built into
libtextstyle
does not already disable them.
To emit a hyperlink, use code like this:
styled_ostream_t stream = ... ... /* Start a hyperlink. */ styled_ostream_set_hyperlink (stream, url, NULL); ... /* Emit the anchor text. This can be styled text. */ ostream_write_str (stream, "Click here!"); ... /* End the current hyperlink. */ styled_ostream_set_hyperlink (stream, NULL, NULL); |
The anchor text can be styled. But the hyperlinks themselves cannot be styled; they behave as implemented by the terminal emulator.
The include file <textstyle.h>
declares all facilities defined by
the library.
The library to link with is called libtextstyle
, with a
system-dependent suffix. You link with it though link options of the
form -ltextstyle
for a library installed in system locations, or
-Llibdir -ltextstyle
for a static library installed in other
locations, or -Llibdir -ltextstyle -Wl,-rpath,libdir
for a shared library installed in other locations (assuming a GCC
compatible compiler and linker and no libtool
), or
-Llibdir -ltextstyle -Rlibdir
for a shared library
installed in other locations (with libtool
). Additionally, the
link options may need to include the dependencies: -lm
, and
-lncurses
or (on NetBSD) -ltermcap
or (on AIX)
-lxcurses
or (on HP-UX) -lcurses
, and on some systems also
-liconv
.
It is a bit complicated to determine the right link options in a portable
way. Therefore an Autoconf macro is provided in the file
libtextstyle.m4
in Gnulib, that makes this task easier. Assuming
the build system of your package is based on GNU Autoconf, you invoke it
through gl_LIBTEXTSTYLE
. It searches for an installed
libtextstyle
. If found, it sets and AC_SUBSTs
HAVE_LIBTEXTSTYLE=yes
and the LIBTEXTSTYLE
and
LTLIBTEXTSTYLE
variables, and augments the CPPFLAGS
variable, and #defines HAVE_LIBTEXTSTYLE
to 1. Otherwise, it sets
and AC_SUBSTs HAVE_LIBTEXTSTYLE=no
and LIBTEXTSTYLE
and
LTLIBTEXTSTYLE
to empty. In link commands that use libtool
,
use LTLIBTEXTSTYLE
; in link commands that don't use libtool
,
use LIBTEXTSTYLE
.
If you use GNU Automake, the proper place to use the link options is
program_LDADD
for programs and library_LIBADD
for libraries.
While you are free to provide any command-line option to enable the
styling of the output, it is good if different GNU programs use the same
command-line options for this purpose. These options are described in
the sections The --color
option and The --style
option. To
achieve this, use the following API (declared in <textstyle.h>
):
True if a --color
option with value test
has been seen.
Stores the value of the --color
option.
Stores the value of the --style
option.
Note: These variables, like any variables exported from shared libraries, can only be used in executable code. You cannot portably use their address in initializers of global or static variables. This is a restriction that is imposed by the Windows, Cygwin, and Android platforms.
You invoke this function when, during argument parsing, you have
encountered a --color
or --color=...
option. The return
value is an error indicator: true
means an invalid option.
You invoke this function when, during argument parsing, you have
encountered a --style
or --style=...
option.
Prints a color test page. You invoke this function after argument
parsing, when the color_test_mode
variable is true.
Assigns a default value to style_file_name
if necessary. You
invoke this function after argument parsing, when color_test_mode
is false.
style_file_envvar
is an environment variable that, when set
to a non-empty value, specifies the style file to use. This environment
variable is meant to be set by the user.
stylesdir_envvar
is an environment variable that, when set
to a non-empty value, specifies the directory with the style files, or
NULL
. This is necessary for running the testsuite before
‘make install’.
stylesdir_after_install
is the directory with the style
files after ‘make install’.
default_style_file
is the file name of the default style
file, relative to stylesdir.
There are various classes of output streams, some of them with styling support. These “classes” are defined in an object-oriented programming style that resembles C++ or Java, but are actually implemented in C with a little bit of object orientation syntax. These definitions are preprocessed down to C. As a consequence, GNU libtextstyle is a C library and does not need to link with the C++ standard library.
All these classes are declared in <textstyle.h>
.
The base output stream type is ‘ostream_t’. It is a pointer type to a (hidden) implementation type. Similarly for the subclasses.
When we say that ‘some_ostream_t’ is a subclass of ‘ostream_t’, what we mean is:
ostream
class The base output stream type is ‘ostream_t’.
It has the following methods:
Writes a sequence of bytes to a stream.
Writes a string's contents to a stream.
Writes formatted output to a stream.
These functions return the size of formatted output, or a negative value in case of an error.
Brings buffered data to its destination.
Closes and frees a stream.
styled_ostream
class The type for a styled output stream is ‘styled_ostream_t’. It is a subclass of ‘ostream_t’ that adds the following methods:
Starts a run of text belonging to classname
. The
classname
is the name of a CSS class. It can be chosen
arbitrarily and customized through the CSS file.
Ends a run of text belonging to classname
. The
styled_ostream_begin_use_class
/
styled_ostream_end_use_class
calls must match properly.
Returns the referred URL of the currently set hyperlink, or NULL
if no hyperlink attribute is currently set.
Note: The returned string is only valid up to the next invocation of
styled_ostream_set_hyperlink
.
Returns the id of the currently set hyperlink, or NULL
if no
hyperlink attribute is currently set.
Note: The returned string is only valid up to the next invocation of
styled_ostream_set_hyperlink
.
Sets or removes a hyperlink attribute.
To set a hyperlink attribute, pass a non-NULL
ref.
ref is an URL; it should be at most 2083 bytes long. Non-ASCII
characters should be URI-escaped (using the %nn syntax). id is
an optional identifier. On terminal output, multiple hyperlinks with
the same id will be highlighted together. If specified, id
should be at most 250 bytes long.
To remove a hyperlink attribute, pass NULL
for ref and id.
Hyperlinks don't nest. That is, a hyperlink attribute is enabled only
up to the next invocation of styled_ostream_set_hyperlink
.
This function acts like ostream_flush (stream, FLUSH_THIS_STREAM)
,
except that it leaves the destination with the current text style enabled,
instead of with the default text style.
After calling this function, you can output strings without newlines(!) to the
underlying stream, and they will be rendered like strings passed to
ostream_write_mem
, ostream_write_str
, or ostream_printf
.
file_ostream
class The file_ostream
class supports output to an <stdio.h>
FILE
stream. Its type is ‘file_ostream_t’. It is a subclass
of ‘ostream_t’ that adds no methods.
It can be instantiated through this function:
Creates an output stream referring to fp
.
Note: The resulting stream must be closed before fp
can be
closed.
fd_ostream
class The file_ostream
class supports output to a file descriptor. Its
type is ‘fd_ostream_t’. It is a subclass of ‘ostream_t’ that
adds no methods.
It can be instantiated through this function:
Creates an output stream referring to the file descriptor fd
.
filename
is used only for error messages.
Note: The resulting stream must be closed before fd
can be
closed.
term_ostream
class The term_ostream
class supports output to a file descriptor that
is connected to a terminal emulator or console. Its type is
‘term_ostream_t’. It is a subclass of ‘ostream_t’.
It can be instantiated through this function:
Creates an output stream referring to the file descriptor fd
.
filename
is used only for error messages.
tty_control
specifies the amount of control to take over the
underlying tty.
The resulting stream will be line-buffered.
Note: The resulting stream must be closed before fd
can be
closed.
The class adds the following methods:
Converts an RGB value
(red
, green
, blue
in [0..255]) to
a color, valid for this stream only.
Gets/sets the text color.
Gets/sets the background color.
Gets/sets the font weight.
Gets/sets the font posture.
Gets/sets the text underline decoration.
Returns the referred URL of the currently set hyperlink, or NULL
if no hyperlink attribute is currently set.
Note: The returned string is only valid up to the next invocation of
term_ostream_set_hyperlink
.
Returns the id of the currently set hyperlink, or NULL
if no
hyperlink attribute is currently set.
Note: The returned string is only valid up to the next invocation of
term_ostream_set_hyperlink
.
Sets or removes a hyperlink attribute.
To set a hyperlink attribute, pass a non-NULL
ref.
ref is an URL; it should be at most 2083 bytes long. Non-ASCII
characters should be URI-escaped (using the %nn syntax). id is
an optional identifier. Multiple hyperlinks with the same id
will be highlighted together. If specified, id should be at most
250 bytes long.
To remove a hyperlink attribute, pass NULL
for ref and id.
Hyperlinks don't nest. That is, a hyperlink attribute is enabled only
up to the next invocation of styled_ostream_set_hyperlink
.
This function acts like ostream_flush (stream, FLUSH_THIS_STREAM)
,
except that it leaves the terminal with the current text attributes enabled,
instead of with the default text attributes.
After calling this function, you can output strings without newlines(!) to the
underlying file descriptor, and they will be rendered like strings passed to
ostream_write_mem
, ostream_write_str
, or ostream_printf
.
html_ostream
class The html_ostream
class supports output to any destination, in HTML
syntax. Its type is ‘html_ostream_t’. It is a subclass of
‘ostream_t’.
It can be instantiated through this function:
Creates an output stream that takes input in the UTF-8 encoding and
writes it in HTML form on destination
.
This stream produces a sequence of lines. The caller is responsible for
opening the <body><html>
elements before and for closing them
after the use of this stream.
Note: The resulting stream must be closed before destination
can be closed.
The class adds the following methods:
Starts a <span class="classname">
element. The
classname
is the name of a CSS class. It can be chosen
arbitrarily and customized through the CSS file.
Ends a <span class="classname">
element.
The html_ostream_begin_span
/ html_ostream_end_span
calls
must match properly.
Returns the referred URL of the currently set hyperlink, or NULL
if no hyperlink attribute is currently set.
Note: The returned string is only valid up to the next invocation of
html_ostream_set_hyperlink_ref
.
Sets or removes a hyperlink attribute.
To set a hyperlink attribute, pass a non-NULL
ref.
ref is an URL; it should be at most 2083 bytes long. Non-ASCII
characters should be URI-escaped (using the %nn syntax).
To remove a hyperlink attribute, pass NULL
for ref.
Hyperlinks don't nest. That is, a hyperlink attribute is enabled only
up to the next invocation of html_ostream_set_hyperlink_ref
.
This function acts like ostream_flush (stream, FLUSH_THIS_STREAM)
,
except that it leaves the destination with the current text style enabled,
instead of with the default text style.
After calling this function, you can output strings without newlines(!) to the
underlying stream, and they will be rendered like strings passed to
ostream_write_mem
, ostream_write_str
, or ostream_printf
.
memory_ostream
class The memory_ostream
class supports output to an in-memory buffer.
Its type is ‘memory_ostream_t’. It is a subclass of
‘ostream_t’.
It can be instantiated through this function:
Creates an output stream that accumulates the output in a memory buffer.
The class adds the following method:
Returns a pointer to the output accumulated so far and its size. It
stores them in *bufp
and *buflenp
, respectively.
Note: These two return values become invalid when more output is done to the stream or when the stream is freed.
iconv_ostream
class The iconv_ostream
class supports output to any destination. Its
type is ‘iconv_ostream_t’. It is a subclass of ‘ostream_t’
that adds no methods.
It can be instantiated through this function:
Creates an output stream that converts from from_encoding
to
to_encoding
, writing the result to destination
.
Note: The resulting stream must be closed before destination
can be closed.
styled_ostream
subclasses term_styled_ostream
class The term_styled_ostream
class supports styled output to a file
descriptor that is connected to a terminal emulator or console. Its type
is ‘term_styled_ostream_t’. It is a subclass of
‘styled_ostream_t’.
It can be instantiated through this function:
Creates an output stream referring to the file descriptor fd
,
styled with the file css_filename
.
filename
is used only for error messages.
tty_control
specifies the amount of control to take over the
underlying tty.
Note: The resulting stream must be closed before fd
can be
closed.
Returns NULL
upon failure.
The following is a variant of this function. Upon failure, it does not
return NULL
; instead, it returns a styled fd_stream
on
which the styling operations exist but are no-ops.
Creates an output stream referring to the file descriptor fd
,
styled with the file css_filename
if possible.
filename
is used only for error messages.
tty_control
specifies the amount of control to take over the
underlying tty.
Note: The resulting stream must be closed before fd
can be
closed.
html_styled_ostream
class The html_styled_ostream
class supports styled output to any
destination, in HTML syntax. Its type is ‘html_styled_ostream_t’.
It is a subclass of ‘styled_ostream_t’.
It can be instantiated through this function:
Creates an output stream that takes input in the UTF-8 encoding and
writes it in HTML form on destination
, styled with the file
css_filename
.
Note: The resulting stream must be closed before destination
can be closed.
noop_styled_ostream
class The noop_styled_ostream
class supports the styled output operations
to any destination. The text is output to the given destination; the
styling operations, however, do nothing. Its type is
‘noop_styled_ostream_t’. It is a subclass of ‘styled_ostream_t’.
It can be instantiated through this function:
Creates an output stream that delegates to destination
and
that supports the styling operations as no-ops.
If pass_ownership
is true
, closing the resulting
stream will automatically close the destination
.
Note: If pass_ownership
is false
, the resulting stream
must be closed before destination
can be closed.
If you want to understand which output of your program is associated with which CSS classes, the simplest way is as follows:
--color=html
,
redirecting the output to a file.
<span class="css-class">
...</span>
.
To make the text styling support available to the end user of your package, the following need to be documented:
man
pages (if present),
and in the documentation.
style_file_envvar
above,
that, when set to a non-empty value, specifies the style file to use.
[ << ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated by Bruno Haible on July, 26 2020 using texi2html 1.78a.