Skip to content

Commit ef972d2

Browse files
committed
Use different strategy for providing color codes
1 parent a68eb72 commit ef972d2

6 files changed

+250
-230
lines changed

doc/specs/stdlib_terminal_color.md

+90-17
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,33 @@ Support terminal escape sequences to produce styled and colored terminal output.
1515
## Derived types provided
1616

1717

18-
### ``fg_color24`` type
18+
### ``ansi_color`` type
1919

20-
The ``fg_color24`` type represent a true color (24-bit) foreground color.
21-
It contains the members ``red``, ``blue`` and ``green`` as default integer types.
20+
The ``ansi_color`` type represent an ANSI escape sequence with a style, forground
21+
color and background color attribute. By default the instances of this type are
22+
empty and represent no escape sequence.
2223

2324
#### Status
2425

2526
Experimental
2627

28+
#### Example
2729

28-
### ``bg_color24`` type
30+
```fortran
31+
program demo_color
32+
use stdlib_terminal_colors, only : fg_color_blue, style_bold, style_reset, ansi_color, &
33+
& operator(//), operator(+)
34+
implicit none
35+
type(ansi_color) :: highlight, reset
2936
30-
The ``bg_color24`` type represent a true color (24-bit) background color.
31-
It contains the members ``red``, ``blue`` and ``green`` as default integer types.
37+
print '(a)', highlight // "Dull text message" // reset
3238
33-
#### Status
39+
highlight = fg_color_blue + style_bold
40+
reset = style_reset
3441
35-
Experimental
42+
print '(a)', highlight // "Colorful text message" // reset
43+
end program demo_color
44+
```
3645

3746

3847
## Constants provided
@@ -52,13 +61,11 @@ Style enumerator representing a bold escape code.
5261
Style enumerator representing a dim escape code.
5362

5463

55-
5664
### ``style_italic``
5765

5866
Style enumerator representing an italic escape code.
5967

6068

61-
6269
### ``style_underline``
6370

6471
Style enumerator representing an underline escape code.
@@ -187,15 +194,16 @@ Generic interface to turn a style, foreground or background enumerator into an a
187194

188195
#### Syntax
189196

190-
`string = [[stdlib_string_colors(module):to_string(interface)]] (enum)`
197+
`string = [[stdlib_string_colors(module):to_string(interface)]] (code)`
191198

192199
#### Class
193200

194201
Pure function.
195202

196203
#### Argument
197204

198-
``enum``: Style, foreground or background enumerator, this argument is ``intent(in)``.
205+
``code``: Style, foreground or background code of ``ansi_color`` type,
206+
this argument is ``intent(in)``.
199207

200208
#### Result value
201209

@@ -205,27 +213,92 @@ The result is a default character string.
205213

206214
Experimental
207215

216+
#### Example
208217

209-
### ``to_string``
218+
```fortran
219+
program demo_string
220+
use stdlib_terminal_colors, only : fg_color_green, style_reset, to_string
221+
implicit none
210222
211-
Generic interface to turn a foreground or background true color type into an actual escape code string for printout.
223+
print '(a)', to_string(fg_color_green) // "Colorized text message" // to_string(style_reset)
224+
end program demo_string
225+
```
226+
227+
228+
### ``operator(+)``
229+
230+
Add two escape sequences, attributes in the right value override the left value ones.
212231

213232
#### Syntax
214233

215-
`string = [[stdlib_string_colors(module):to_string(interface)]] (color24)`
234+
`code = lval + rval`
216235

217236
#### Class
218237

219238
Pure function.
220239

221240
#### Argument
222241

223-
``color24``: Foreground or background true color instance, this argument is ``intent(in)``.
242+
``lval``: Style, foreground or background code of ``ansi_color`` type,
243+
this argument is ``intent(in)``.
244+
``rval``: Style, foreground or background code of ``ansi_color`` type,
245+
this argument is ``intent(in)``.
224246

225247
#### Result value
226248

227-
The result is a default character string.
249+
The result is a style, foreground or background code of ``ansi_color`` type.
228250

229251
#### Status
230252

231253
Experimental
254+
255+
#### Example
256+
257+
```fortran
258+
program demo_combine
259+
use stdlib_terminal_colors, only : fg_color_red, style_bold, ansi_color
260+
implicit none
261+
type(ansi_color) :: bold_red
262+
263+
bold_red = fg_color_red + style_bold
264+
end program demo_combine
265+
```
266+
267+
268+
### ``operator(//)``
269+
270+
Concatenate an escape code with a string and turn it into an actual escape sequence
271+
272+
#### Syntax
273+
274+
`code = lval + rval`
275+
276+
#### Class
277+
278+
Pure function.
279+
280+
#### Argument
281+
282+
``lval``: Style, foreground or background code of ``ansi_color`` type or a character string,
283+
this argument is ``intent(in)``.
284+
``rval``: Style, foreground or background code of ``ansi_color`` type or a character string,
285+
this argument is ``intent(in)``.
286+
287+
#### Result value
288+
289+
The result is a character string with the escape sequence prepended or appended.
290+
291+
#### Status
292+
293+
Experimental
294+
295+
#### Example
296+
297+
```fortran
298+
program demo_concat
299+
use stdlib_terminal_colors, only : fg_color_red, style_reset, operator(//)
300+
implicit none
301+
302+
print '(a)', fg_color_red // "Colorized text message" // style_reset
303+
end program demo_concat
304+
```

src/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ set(SRC
8888
stdlib_specialfunctions.f90
8989
stdlib_specialfunctions_legendre.f90
9090
stdlib_terminal_colors.f90
91+
stdlib_terminal_colors_operator.f90
9192
stdlib_terminal_colors_to_string.f90
9293
stdlib_quadrature_gauss.f90
9394
stdlib_stringlist_type.f90

0 commit comments

Comments
 (0)