[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

3. The programmer's perspective

As a programmer, enabling styling consists of the following tasks:

  1. Define the command-line options and environment variable that the user can use to control the styling.
  2. Define the CSS classes that the user can use in the CSS file. Each CSS class corresponds to a text role; each CSS class can be given a different styling by the user.
  3. Change the output routines so that they take an ‘ostream_t’ object as argument instead of a ‘FILE *’.
  4. Insert paired invocations to styled_ostream_begin_css_class, styled_ostream_end_css_class around each run of text with a specific text role.
  5. Link with libtextstyle. If your package is using GNU autoconf, you can use the libtextstyle.m4 macro from Gnulib.
  6. Prepare a default style file.
  7. Update the documentation of your package.

The following sections go into more detail.

3.1 Basic use of libtextstyle

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.

3.1.1 Hyperlinks

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.

3.2 Include files

The include file <textstyle.h> declares all facilities defined by the library.

3.3 Link options

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.

3.4 Command-line options

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>):

Variable: bool color_test_mode

True if a --color option with value test has been seen.

Variable: enum color_option color_mode

Stores the value of the --color option.

Variable: const char * style_file_name

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.

Function: bool handle_color_option (const char *option)

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.

Function: void handle_style_option (const char *option)

You invoke this function when, during argument parsing, you have encountered a --style or --style=... option.

Function: void print_color_test (void)

Prints a color test page. You invoke this function after argument parsing, when the color_test_mode variable is true.

Function: void style_file_prepare (const char *style_file_envvar, const char *stylesdir_envvar, const char *stylesdir_after_install, const char *default_style_file)

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.

3.5 The output stream hierarchy

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:

3.5.1 The abstract ostream class

The base output stream type is ‘ostream_t’.

It has the following methods:

Function: void ostream_write_mem (ostream_t stream, const void *data, size_t len)

Writes a sequence of bytes to a stream.

Function: void ostream_write_str (ostream_t stream, const char *string)

Writes a string's contents to a stream.

Function: ptrdiff_t ostream_printf (ostream_t stream, const char *format, ...)
Function: ptrdiff_t ostream_vprintf (ostream_t stream, const char *format, va_list args)

Writes formatted output to a stream.

These functions return the size of formatted output, or a negative value in case of an error.

Function: void ostream_flush (ostream_t stream, ostream_flush_scope_t scope)

Brings buffered data to its destination.

Function: void ostream_free (ostream_t stream)

Closes and frees a stream.

3.5.2 The abstract 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:

Function: void styled_ostream_begin_use_class (styled_ostream_t stream, const char *classname)

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.

Function: void styled_ostream_end_use_class (styled_ostream_t stream, const char *classname)

Ends a run of text belonging to classname. The styled_ostream_begin_use_class / styled_ostream_end_use_class calls must match properly.

Function: const char * styled_ostream_get_hyperlink_ref (styled_ostream_t stream)

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.

Function: const char * styled_ostream_get_hyperlink_id (styled_ostream_t stream)

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.

Function: void styled_ostream_set_hyperlink (styled_ostream_t stream, const char *ref, const char *id)

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.

Function: void styled_ostream_flush_to_current_style (styled_ostream_t stream)

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.

3.5.3 Concrete ostream subclasses without styling

3.5.3.1 The 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:

Function: file_ostream_t file_ostream_create (FILE *fp)

Creates an output stream referring to fp.

Note: The resulting stream must be closed before fp can be closed.

3.5.3.2 The 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:

Function: fd_ostream_t fd_ostream_create (int fd, const char *filename, bool buffered)

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.

3.5.3.3 The 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:

Function: term_ostream_t term_ostream_create (int fd, const char *filename, ttyctl_t tty_control)

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:

Function: term_color_t term_ostream_rgb_to_color (term_ostream_t stream, int red, int green, int blue)

Converts an RGB value (red, green, blue in [0..255]) to a color, valid for this stream only.

Function: term_color_t term_ostream_get_color (term_ostream_t stream)
Function: void term_ostream_set_color (term_ostream_t stream, term_color_t color)

Gets/sets the text color.

Function: term_color_t term_ostream_get_bgcolor (term_ostream_t stream)
Function: void term_ostream_set_bgcolor (term_ostream_t stream, term_color_t color)

Gets/sets the background color.

Function: term_weight_t term_ostream_get_weight (term_ostream_t stream)
Function: void term_ostream_set_weight (term_ostream_t stream, term_weight_t weight)

Gets/sets the font weight.

Function: term_posture_t term_ostream_get_posture (term_ostream_t stream)
Function: void term_ostream_set_posture (term_ostream_t stream, term_posture_t posture)

Gets/sets the font posture.

Function: term_underline_t term_ostream_get_underline (term_ostream_t stream)
Function: void term_ostream_set_underline (term_ostream_t stream, term_underline_t underline)

Gets/sets the text underline decoration.

Function: const char * term_ostream_get_hyperlink_ref (term_ostream_t stream)

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.

Function: const char * term_ostream_get_hyperlink_id (term_ostream_t stream)

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.

Function: void term_ostream_set_hyperlink (term_ostream_t stream, const char *ref, const char *id)

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.

Function: void term_ostream_flush_to_current_style (term_ostream_t stream)

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.

3.5.3.4 The 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:

Function: html_ostream_t html_ostream_create (ostream_t destination)

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:

Function: void html_ostream_begin_span (html_ostream_t stream, const char *classname)

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.

Function: void html_ostream_end_span (html_ostream_t stream, const char *classname)

Ends a <span class="classname"> element.

The html_ostream_begin_span / html_ostream_end_span calls must match properly.

Function: const char * html_ostream_get_hyperlink_ref (html_ostream_t stream)

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.

Function: void html_ostream_set_hyperlink_ref (html_ostream_t stream, const char *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.

Function: void html_ostream_flush_to_current_style (html_ostream_t stream)

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.

3.5.3.5 The 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:

Function: memory_ostream_t memory_ostream_create (void)

Creates an output stream that accumulates the output in a memory buffer.

The class adds the following method:

Function: void memory_ostream_contents (memory_ostream_t stream, const void **bufp, size_t *buflenp)

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.

3.5.3.6 The 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:

Function: iconv_ostream_t iconv_ostream_create (const char *from_encoding, const char *to_encoding, ostream_t destination)

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.

3.5.4 Concrete styled_ostream subclasses

3.5.4.1 The 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:

Function: term_styled_ostream_t term_styled_ostream_create (int fd, const char *filename, ttyctl_t tty_control, const char *css_filename)

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.

Function: styled_ostream_t styled_ostream_create (int fd, const char *filename, ttyctl_t tty_control, const char *css_filename)

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.

3.5.4.2 The 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:

Function: html_styled_ostream_t html_styled_ostream_create (ostream_t destination, const char *css_filename)

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.

3.5.4.3 The 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:

Function: noop_styled_ostream_t noop_styled_ostream_create (ostream_t destination, bool pass_ownership)

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.

3.5.5 Accessor functions

The various concrete stream classes have methods that allow you to retrieve the arguments passed to the respective constructor function.

Note: While these methods allow you to retrieve the underlying destination stream of various kinds of stream, it is not recommended to operate on both the stream and its underlying destination stream at the same time. Doing so can lead to undesired interactions between the two streams.

The file_ostream class has this accessor method:

Function: FILE * file_ostream_get_stdio_stream (file_ostream_t stream)

The fd_ostream class has these accessor methods:

Function: int fd_ostream_get_descriptor (fd_ostream_t stream)
Function: const char * fd_ostream_get_filename (fd_ostream_t stream)
Function: bool fd_ostream_is_buffered (fd_ostream_t stream)

The term_ostream class has these accessor methods:

Function: int term_ostream_get_descriptor (term_ostream_t stream)
Function: const char * term_ostream_get_filename (term_ostream_t stream)
Function: ttyctl_t term_ostream_get_tty_control (term_ostream_t stream)
Function: ttyctl_t term_ostream_get_effective_tty_control (term_ostream_t stream)

Returns the effective tty control of the stream (not TTYCTL_AUTO).

The iconv_ostream class has these accessor methods:

Function: const char * iconv_ostream_get_from_encoding (iconv_ostream_t stream)
Function: const char * iconv_ostream_get_to_encoding (iconv_ostream_t stream)
Function: ostream_t iconv_ostream_get_destination (iconv_ostream_t stream)

The html_ostream class has this accessor method:

Function: ostream_t html_ostream_get_destination (html_ostream_t stream)

The term_styled_ostream class has these accessor methods:

Function: term_ostream_t term_styled_ostream_get_destination (term_styled_ostream_t stream)
Function: const char * term_styled_ostream_get_css_filename (term_styled_ostream_t stream)

The html_styled_ostream class has these accessor methods:

Function: ostream_t html_styled_ostream_get_destination (html_styled_ostream_t stream)
Function: html_ostream_t html_styled_ostream_get_html_destination (html_styled_ostream_t stream)
Function: const char * html_styled_ostream_get_css_filename (html_styled_ostream_t stream)

The noop_styled_ostream class has these accessor methods:

Function: ostream_t noop_styled_ostream_get_destination (noop_styled_ostream_t stream)
Function: bool noop_styled_ostream_is_owning_destination (noop_styled_ostream_t stream)

3.6 Debugging the text styling support

If you want to understand which output of your program is associated with which CSS classes, the simplest way is as follows:

  1. Run the program with the command-line option --color=html, redirecting the output to a file.
  2. Then inspect this output. Text regions associated with a CSS class are surrounded by <span class="css-class">...</span>.

3.7 Documenting the text styling support

To make the text styling support available to the end user of your package, the following need to be documented:

[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Bruno Haible on February, 21 2024 using texi2html 1.78a.