Wrap text in the requested attributes and a reset.
The reset is emitted only when something was actually set, so
styled("x") is "x" and not "x" with a stray reset after it.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | text |
Text to wrap. |
||
| integer(kind=int32), | intent(in), | optional | :: | fg |
Foreground colour, one of the ANSI_* constants. |
|
| integer(kind=int32), | intent(in), | optional | :: | bg |
Background colour, one of the ANSI_* constants. |
|
| logical, | intent(in), | optional | :: | bold |
Whether to embolden. |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| logical, | private | :: | any_set |
pure function styled(text, fg, bg, bold) result(seq) !! Wrap `text` in the requested attributes and a reset. !! !! The reset is emitted only when something was actually set, so !! `styled("x")` is `"x"` and not `"x"` with a stray reset after it. character(len=*), intent(in) :: text !! Text to wrap. integer(int32), intent(in), optional :: fg !! Foreground colour, one of the ANSI_* constants. integer(int32), intent(in), optional :: bg !! Background colour, one of the ANSI_* constants. logical, intent(in), optional :: bold !! Whether to embolden. character(len=:), allocatable :: seq logical :: any_set seq = "" any_set = .false. if (present(fg)) then seq = seq//ansi_fg(fg) any_set = .true. end if if (present(bg)) then seq = seq//ansi_bg(bg) any_set = .true. end if if (present(bold)) then if (bold) then seq = seq//ansi_bold() any_set = .true. end if end if seq = seq//text if (any_set) seq = seq//ansi_reset() end function styled