1 /* Output stream for attributed text, producing ANSI escape sequences.
2    Copyright (C) 2006, 2019 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 "ostream.h"
22 
23 
24 /* Querying and setting of text attributes.
25    The stream has a notion of the current text attributes; they apply
26    implicitly to all following output.  The attributes are automatically
27    reset when the stream is closed.
28    Note: Not all terminal types can actually render all attributes adequately.
29    For example, xterm cannot render POSTURE_ITALIC nor the combination of
30    WEIGHT_BOLD and UNDERLINE_ON.  */
31 
32 /* Colors are represented by indices >= 0 in a stream dependent format.  */
33 typedef int term_color_t;
34 /* The value -1 denotes the default (foreground or background) color.  */
35 enum
36 {
37   COLOR_DEFAULT = -1  /* unknown */
38 };
39 
40 typedef enum
41 {
42   WEIGHT_NORMAL = 0,
43   WEIGHT_BOLD,
44   WEIGHT_DEFAULT = WEIGHT_NORMAL
45 } term_weight_t;
46 
47 typedef enum
48 {
49   POSTURE_NORMAL = 0,
50   POSTURE_ITALIC, /* same as oblique */
51   POSTURE_DEFAULT = POSTURE_NORMAL
52 } term_posture_t;
53 
54 typedef enum
55 {
56   UNDERLINE_OFF = 0,
57   UNDERLINE_ON,
58   UNDERLINE_DEFAULT = UNDERLINE_OFF
59 } term_underline_t;
60 
61 struct term_ostream : struct ostream
62 {
63 methods:
64 
65   /* Convert an RGB value (red, green, blue in [0..255]) to a color, valid
66      for this stream only.  */
67   term_color_t rgb_to_color (term_ostream_t stream,
68                              int red, int green, int blue);
69 
70   /* Get/set the text color.  */
71   term_color_t get_color (term_ostream_t stream);
72   void         set_color (term_ostream_t stream, term_color_t color);
73 
74   /* Get/set the background color.  */
75   term_color_t get_bgcolor (term_ostream_t stream);
76   void         set_bgcolor (term_ostream_t stream, term_color_t color);
77 
78   /* Get/set the font weight.  */
79   term_weight_t get_weight (term_ostream_t stream);
80   void          set_weight (term_ostream_t stream, term_weight_t weight);
81 
82   /* Get/set the font posture.  */
83   term_posture_t get_posture (term_ostream_t stream);
84   void           set_posture (term_ostream_t stream, term_posture_t posture);
85 
86   /* Get/set the text underline decoration.  */
87   term_underline_t get_underline (term_ostream_t stream);
88   void             set_underline (term_ostream_t stream,
89                                   term_underline_t underline);
90 
91   /* Get/set the hyperlink attribute and its id.  */
92   const char * get_hyperlink_ref (term_ostream_t stream);
93   const char * get_hyperlink_id (term_ostream_t stream);
94   void         set_hyperlink (term_ostream_t stream,
95                               const char *ref, const char *id);
96 
97   /* Like term_ostream_flush (first_arg, FLUSH_THIS_STREAM), except that it
98      leaves the terminal with the current text attributes enabled, instead of
99      with the default text attributes.
100      After calling this function, you can output strings without newlines(!)
101      to the underlying file descriptor, and they will be rendered like strings
102      passed to 'ostream_write_mem', 'ostream_write_str', or
103      'ostream_write_printf'.  */
104   void flush_to_current_style (term_ostream_t stream);
105 };
106 
107 /* Get ttyctl_t.  */
108 #define term_style_user_data term_ostream_representation
109 #include "term-style-control.h"
110 
111 
112 #ifdef __cplusplus
113 extern "C" {
114 #endif
115 
116 
117 /* Create an output stream referring to the file descriptor FD.
118    FILENAME is used only for error messages.
119    TTY_CONTROL specifies the amount of control to take over the underlying tty.
120    The resulting stream will be line-buffered.
121    Note that the resulting stream must be closed before FD can be closed.  */
122 extern term_ostream_t
123        term_ostream_create (int fd, const char *filename, ttyctl_t tty_control);
124 
125 
126 #ifdef __cplusplus
127 }
128 #endif
129 
130 #endif /* _TERM_OSTREAM_H */
131