1 /* Output stream for attributed text, producing ANSI escape sequences.
2 Copyright (C) 2006, 2019-2020 Free Software Foundation, Inc.
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17
18 #ifndef _TERM_OSTREAM_H
19 #define _TERM_OSTREAM_H
20
21 #include <stdbool.h>
22
23 #include "ostream.h"
24
25
26 /* Querying and setting of text attributes.
27 The stream has a notion of the current text attributes; they apply
28 implicitly to all following output. The attributes are automatically
29 reset when the stream is closed.
30 Note: Not all terminal types can actually render all attributes adequately.
31 For example, xterm cannot render POSTURE_ITALIC nor the combination of
32 WEIGHT_BOLD and UNDERLINE_ON. */
33
34 /* Colors are represented by indices >= 0 in a stream dependent format. */
35 typedef int term_color_t;
36 /* The value -1 denotes the default (foreground or background) color. */
37 enum
38 {
39 COLOR_DEFAULT = -1 /* unknown */
40 };
41
42 typedef enum
43 {
44 WEIGHT_NORMAL = 0,
45 WEIGHT_BOLD,
46 WEIGHT_DEFAULT = WEIGHT_NORMAL
47 } term_weight_t;
48
49 typedef enum
50 {
51 POSTURE_NORMAL = 0,
52 POSTURE_ITALIC, /* same as oblique */
53 POSTURE_DEFAULT = POSTURE_NORMAL
54 } term_posture_t;
55
56 typedef enum
57 {
58 UNDERLINE_OFF = 0,
59 UNDERLINE_ON,
60 UNDERLINE_DEFAULT = UNDERLINE_OFF
61 } term_underline_t;
62
63 /* Get ttyctl_t. */
64 #define term_style_user_data term_ostream_representation
65 #include "term-style-control.h"
66
67 struct term_ostream : struct ostream
68 {
69 methods:
70
71 /* Convert an RGB value (red, green, blue in [0..255]) to a color, valid
72 for this stream only. */
73 term_color_t rgb_to_color (term_ostream_t stream,
74 int red, int green, int blue);
75
76 /* Get/set the text color. */
77 term_color_t get_color (term_ostream_t stream);
78 void set_color (term_ostream_t stream, term_color_t color);
79
80 /* Get/set the background color. */
81 term_color_t get_bgcolor (term_ostream_t stream);
82 void set_bgcolor (term_ostream_t stream, term_color_t color);
83
84 /* Get/set the font weight. */
85 term_weight_t get_weight (term_ostream_t stream);
86 void set_weight (term_ostream_t stream, term_weight_t weight);
87
88 /* Get/set the font posture. */
89 term_posture_t get_posture (term_ostream_t stream);
90 void set_posture (term_ostream_t stream, term_posture_t posture);
91
92 /* Get/set the text underline decoration. */
93 term_underline_t get_underline (term_ostream_t stream);
94 void set_underline (term_ostream_t stream,
95 term_underline_t underline);
96
97 /* Get/set the hyperlink attribute and its id. */
98 const char * get_hyperlink_ref (term_ostream_t stream);
99 const char * get_hyperlink_id (term_ostream_t stream);
100 void set_hyperlink (term_ostream_t stream,
101 const char *ref, const char *id);
102
103 /* Like term_ostream_flush (first_arg, FLUSH_THIS_STREAM), except that it
104 leaves the terminal with the current text attributes enabled, instead of
105 with the default text attributes.
106 After calling this function, you can output strings without newlines(!)
107 to the underlying file descriptor, and they will be rendered like strings
108 passed to 'ostream_write_mem', 'ostream_write_str', or
109 'ostream_write_printf'. */
110 void flush_to_current_style (term_ostream_t stream);
111
112 /* Accessors. */
113 int get_descriptor (term_ostream_t stream);
114 const char * get_filename (term_ostream_t stream);
115 ttyctl_t get_tty_control (term_ostream_t stream);
116 ttyctl_t get_effective_tty_control (term_ostream_t stream);
117 };
118
119
120 #ifdef __cplusplus
121 extern "C" {
122 #endif
123
124
125 /* Create an output stream referring to the file descriptor FD.
126 FILENAME is used only for error messages.
127 TTY_CONTROL specifies the amount of control to take over the underlying tty.
128 The resulting stream will be line-buffered.
129 Note that the resulting stream must be closed before FD can be closed. */
130 extern term_ostream_t
131 term_ostream_create (int fd, const char *filename, ttyctl_t tty_control);
132
133
134 /* Test whether a given output stream is a term_ostream. */
135 extern bool is_instance_of_term_ostream (ostream_t stream);
136
137
138 #ifdef __cplusplus
139 }
140 #endif
141
142 #endif /* _TERM_OSTREAM_H */