|
| 1 | +! SPDX-Identifier: MIT |
| 2 | + |
| 3 | +!> Terminal color and style escape sequences |
| 4 | +module stdlib_ansi |
| 5 | + use stdlib_kinds, only : i1 => int8 |
| 6 | + use stdlib_string_type, only : string_type |
| 7 | + implicit none |
| 8 | + private |
| 9 | + |
| 10 | + public :: ansi_code |
| 11 | + public :: style_reset, style_bold, style_dim, style_italic, style_underline, & |
| 12 | + & style_blink, style_blink_fast, style_reverse, style_hidden, style_strikethrough |
| 13 | + public :: fg_color_black, fg_color_red, fg_color_green, fg_color_yellow, fg_color_blue, & |
| 14 | + & fg_color_magenta, fg_color_cyan, fg_color_white, fg_color_default |
| 15 | + public :: bg_color_black, bg_color_red, bg_color_green, bg_color_yellow, bg_color_blue, & |
| 16 | + & bg_color_magenta, bg_color_cyan, bg_color_white, bg_color_default |
| 17 | + |
| 18 | + public :: to_string, operator(+), operator(//) |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | + !> Container for terminal escape code |
| 23 | + type :: ansi_code |
| 24 | + private |
| 25 | + !> Style descriptor |
| 26 | + integer(i1) :: style = -1_i1 |
| 27 | + !> Background color descriptor |
| 28 | + integer(i1) :: bg = -1_i1 |
| 29 | + !> Foreground color descriptor |
| 30 | + integer(i1) :: fg = -1_i1 |
| 31 | + end type ansi_code |
| 32 | + |
| 33 | + |
| 34 | + !> Identifier for reset style |
| 35 | + type(ansi_code), parameter :: style_reset = ansi_code(style=0) |
| 36 | + !> Identifier for bold style |
| 37 | + type(ansi_code), parameter :: style_bold = ansi_code(style=1) |
| 38 | + !> Identifier for dim style |
| 39 | + type(ansi_code), parameter :: style_dim = ansi_code(style=2) |
| 40 | + !> Identifier for italic style |
| 41 | + type(ansi_code), parameter :: style_italic = ansi_code(style=3) |
| 42 | + !> Identifier for underline style |
| 43 | + type(ansi_code), parameter :: style_underline = ansi_code(style=4) |
| 44 | + !> Identifier for blink style |
| 45 | + type(ansi_code), parameter :: style_blink = ansi_code(style=5) |
| 46 | + !> Identifier for (fast) blink style |
| 47 | + type(ansi_code), parameter :: style_blink_fast = ansi_code(style=6) |
| 48 | + !> Identifier for reverse style |
| 49 | + type(ansi_code), parameter :: style_reverse = ansi_code(style=7) |
| 50 | + !> Identifier for hidden style |
| 51 | + type(ansi_code), parameter :: style_hidden = ansi_code(style=8) |
| 52 | + !> Identifier for strikethrough style |
| 53 | + type(ansi_code), parameter :: style_strikethrough = ansi_code(style=9) |
| 54 | + |
| 55 | + !> Identifier for black foreground color |
| 56 | + type(ansi_code), parameter :: fg_color_black = ansi_code(fg=0) |
| 57 | + !> Identifier for red foreground color |
| 58 | + type(ansi_code), parameter :: fg_color_red = ansi_code(fg=1) |
| 59 | + !> Identifier for green foreground color |
| 60 | + type(ansi_code), parameter :: fg_color_green = ansi_code(fg=2) |
| 61 | + !> Identifier for yellow foreground color |
| 62 | + type(ansi_code), parameter :: fg_color_yellow = ansi_code(fg=3) |
| 63 | + !> Identifier for blue foreground color |
| 64 | + type(ansi_code), parameter :: fg_color_blue = ansi_code(fg=4) |
| 65 | + !> Identifier for magenta foreground color |
| 66 | + type(ansi_code), parameter :: fg_color_magenta = ansi_code(fg=5) |
| 67 | + !> Identifier for cyan foreground color |
| 68 | + type(ansi_code), parameter :: fg_color_cyan = ansi_code(fg=6) |
| 69 | + !> Identifier for white foreground color |
| 70 | + type(ansi_code), parameter :: fg_color_white = ansi_code(fg=7) |
| 71 | + !> Identifier for the default foreground color |
| 72 | + type(ansi_code), parameter :: fg_color_default = ansi_code(fg=9) |
| 73 | + |
| 74 | + !> Identifier for black background color |
| 75 | + type(ansi_code), parameter :: bg_color_black = ansi_code(bg=0) |
| 76 | + !> Identifier for red background color |
| 77 | + type(ansi_code), parameter :: bg_color_red = ansi_code(bg=1) |
| 78 | + !> Identifier for green background color |
| 79 | + type(ansi_code), parameter :: bg_color_green = ansi_code(bg=2) |
| 80 | + !> Identifier for yellow background color |
| 81 | + type(ansi_code), parameter :: bg_color_yellow = ansi_code(bg=3) |
| 82 | + !> Identifier for blue background color |
| 83 | + type(ansi_code), parameter :: bg_color_blue = ansi_code(bg=4) |
| 84 | + !> Identifier for magenta background color |
| 85 | + type(ansi_code), parameter :: bg_color_magenta = ansi_code(bg=5) |
| 86 | + !> Identifier for cyan background color |
| 87 | + type(ansi_code), parameter :: bg_color_cyan = ansi_code(bg=6) |
| 88 | + !> Identifier for white background color |
| 89 | + type(ansi_code), parameter :: bg_color_white = ansi_code(bg=7) |
| 90 | + !> Identifier for the default background color |
| 91 | + type(ansi_code), parameter :: bg_color_default = ansi_code(bg=9) |
| 92 | + |
| 93 | + |
| 94 | + interface to_string |
| 95 | + !> Transform a color code into an actual ANSI escape sequence |
| 96 | + pure module function to_string_ansi_code(code) result(str) |
| 97 | + !> Color code to be used |
| 98 | + type(ansi_code), intent(in) :: code |
| 99 | + !> ANSI escape sequence representing the color code |
| 100 | + character(len=:), allocatable :: str |
| 101 | + end function to_string_ansi_code |
| 102 | + end interface to_string |
| 103 | + |
| 104 | + |
| 105 | + interface operator(+) |
| 106 | + !> Add two escape sequences, attributes in the right value override the left value ones. |
| 107 | + pure module function add(lval, rval) result(code) |
| 108 | + !> First escape code |
| 109 | + type(ansi_code), intent(in) :: lval |
| 110 | + !> Second escape code |
| 111 | + type(ansi_code), intent(in) :: rval |
| 112 | + !> Combined escape code |
| 113 | + type(ansi_code) :: code |
| 114 | + end function add |
| 115 | + end interface operator(+) |
| 116 | + |
| 117 | + interface operator(//) |
| 118 | + !> Concatenate an escape code with a string and turn it into an actual escape sequence |
| 119 | + pure module function concat_left(lval, code) result(str) |
| 120 | + !> String to add the escape code to |
| 121 | + character(len=*), intent(in) :: lval |
| 122 | + !> Escape sequence |
| 123 | + type(ansi_code), intent(in) :: code |
| 124 | + !> Concatenated string |
| 125 | + character(len=:), allocatable :: str |
| 126 | + end function concat_left |
| 127 | + |
| 128 | + !> Concatenate an escape code with a string and turn it into an actual escape sequence |
| 129 | + pure module function concat_right(code, rval) result(str) |
| 130 | + !> String to add the escape code to |
| 131 | + character(len=*), intent(in) :: rval |
| 132 | + !> Escape sequence |
| 133 | + type(ansi_code), intent(in) :: code |
| 134 | + !> Concatenated string |
| 135 | + character(len=:), allocatable :: str |
| 136 | + end function concat_right |
| 137 | + |
| 138 | + !> Concatenate an escape code with a string and turn it into an actual escape sequence |
| 139 | + pure module function concat_left_str(lval, code) result(str) |
| 140 | + !> String to add the escape code to |
| 141 | + type(string_type), intent(in) :: lval |
| 142 | + !> Escape sequence |
| 143 | + type(ansi_code), intent(in) :: code |
| 144 | + !> Concatenated string |
| 145 | + type(string_type) :: str |
| 146 | + end function concat_left_str |
| 147 | + |
| 148 | + !> Concatenate an escape code with a string and turn it into an actual escape sequence |
| 149 | + pure module function concat_right_str(code, rval) result(str) |
| 150 | + !> String to add the escape code to |
| 151 | + type(string_type), intent(in) :: rval |
| 152 | + !> Escape sequence |
| 153 | + type(ansi_code), intent(in) :: code |
| 154 | + !> Concatenated string |
| 155 | + type(string_type) :: str |
| 156 | + end function concat_right_str |
| 157 | + end interface operator(//) |
| 158 | + |
| 159 | +end module stdlib_ansi |
0 commit comments