styled Function

public 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.

Arguments

Type IntentOptional 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.

Return Value character(len=:), allocatable


Calls

proc~~styled~~CallsGraph proc~styled styled proc~ansi_bg ansi_bg proc~styled->proc~ansi_bg proc~ansi_bold ansi_bold proc~styled->proc~ansi_bold proc~ansi_fg ansi_fg proc~styled->proc~ansi_fg proc~ansi_reset ansi_reset proc~styled->proc~ansi_reset interface~to_string to_string proc~ansi_bg->interface~to_string proc~base_code base_code proc~ansi_bg->proc~base_code proc~ansi_fg->interface~to_string proc~ansi_fg->proc~base_code

Variables

Type Visibility Attributes Name Initial
logical, private :: any_set

Source Code

   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