| 1 |
structure ANSIColor: |
| 2 |
sig |
| 3 |
datatype color = |
| 4 |
DEFAULT |
| 5 |
| BLACK |
| 6 |
| RED |
| 7 |
| GREEN |
| 8 |
| YELLOW |
| 9 |
| BLUE |
| 10 |
| MAGENTA |
| 11 |
| CYAN |
| 12 |
| WHITE |
| 13 |
| BRIGHT_BLACK |
| 14 |
| BRIGHT_RED |
| 15 |
| BRIGHT_GREEN |
| 16 |
| BRIGHT_YELLOW |
| 17 |
| BRIGHT_BLUE |
| 18 |
| BRIGHT_MAGENTA |
| 19 |
| BRIGHT_CYAN |
| 20 |
| BRIGHT_WHITE |
| 21 |
val fromString: string -> color option |
| 22 |
val asForeground: color -> string option |
| 23 |
val asBackground: color -> string option |
| 24 |
end = |
| 25 |
struct |
| 26 |
datatype color = |
| 27 |
DEFAULT |
| 28 |
| BLACK |
| 29 |
| RED |
| 30 |
| GREEN |
| 31 |
| YELLOW |
| 32 |
| BLUE |
| 33 |
| MAGENTA |
| 34 |
| CYAN |
| 35 |
| WHITE |
| 36 |
| BRIGHT_BLACK |
| 37 |
| BRIGHT_RED |
| 38 |
| BRIGHT_GREEN |
| 39 |
| BRIGHT_YELLOW |
| 40 |
| BRIGHT_BLUE |
| 41 |
| BRIGHT_MAGENTA |
| 42 |
| BRIGHT_CYAN |
| 43 |
| BRIGHT_WHITE |
| 44 |
fun fromString "default" = SOME DEFAULT |
| 45 |
| fromString "black" = SOME BLACK |
| 46 |
| fromString "red" = SOME RED |
| 47 |
| fromString "green" = SOME GREEN |
| 48 |
| fromString "yellow" = SOME YELLOW |
| 49 |
| fromString "blue" = SOME BLUE |
| 50 |
| fromString "magenta" = SOME MAGENTA |
| 51 |
| fromString "cyan" = SOME CYAN |
| 52 |
| fromString "white" = SOME WHITE |
| 53 |
| fromString "brightblack" = SOME BRIGHT_BLACK |
| 54 |
| fromString "brightred" = SOME BRIGHT_RED |
| 55 |
| fromString "brightgreen" = SOME BRIGHT_GREEN |
| 56 |
| fromString "brightyellow" = SOME BRIGHT_YELLOW |
| 57 |
| fromString "brightblue" = SOME BRIGHT_BLUE |
| 58 |
| fromString "brightmagenta" = SOME BRIGHT_MAGENTA |
| 59 |
| fromString "brightcyan" = SOME BRIGHT_CYAN |
| 60 |
| fromString "brightwhite" = SOME BRIGHT_WHITE |
| 61 |
| fromString _ = NONE |
| 62 |
fun asForeground DEFAULT = NONE |
| 63 |
| asForeground BLACK = SOME "30" |
| 64 |
| asForeground RED = SOME "31" |
| 65 |
| asForeground GREEN = SOME "32" |
| 66 |
| asForeground YELLOW = SOME "33" |
| 67 |
| asForeground BLUE = SOME "34" |
| 68 |
| asForeground MAGENTA = SOME "35" |
| 69 |
| asForeground CYAN = SOME "36" |
| 70 |
| asForeground WHITE = SOME "37" |
| 71 |
| asForeground BRIGHT_BLACK = SOME "90" |
| 72 |
| asForeground BRIGHT_RED = SOME "91" |
| 73 |
| asForeground BRIGHT_GREEN = SOME "92" |
| 74 |
| asForeground BRIGHT_YELLOW = SOME "93" |
| 75 |
| asForeground BRIGHT_BLUE = SOME "94" |
| 76 |
| asForeground BRIGHT_MAGENTA = SOME "95" |
| 77 |
| asForeground BRIGHT_CYAN = SOME "96" |
| 78 |
| asForeground BRIGHT_WHITE = SOME "97" |
| 79 |
fun asBackground DEFAULT = NONE |
| 80 |
| asBackground BLACK = SOME "40" |
| 81 |
| asBackground RED = SOME "41" |
| 82 |
| asBackground GREEN = SOME "42" |
| 83 |
| asBackground YELLOW = SOME "43" |
| 84 |
| asBackground BLUE = SOME "44" |
| 85 |
| asBackground MAGENTA = SOME "45" |
| 86 |
| asBackground CYAN = SOME "46" |
| 87 |
| asBackground WHITE = SOME "47" |
| 88 |
| asBackground BRIGHT_BLACK = SOME "100" |
| 89 |
| asBackground BRIGHT_RED = SOME "101" |
| 90 |
| asBackground BRIGHT_GREEN = SOME "102" |
| 91 |
| asBackground BRIGHT_YELLOW = SOME "103" |
| 92 |
| asBackground BRIGHT_BLUE = SOME "104" |
| 93 |
| asBackground BRIGHT_MAGENTA = SOME "105" |
| 94 |
| asBackground BRIGHT_CYAN = SOME "106" |
| 95 |
| asBackground BRIGHT_WHITE = SOME "107" |
| 96 |
end; |
| 97 |
structure ANSIStyle: |
| 98 |
sig |
| 99 |
type style = |
| 100 |
{ foreground: ANSIColor.color option |
| 101 |
, background: ANSIColor.color option |
| 102 |
, bold: bool |
| 103 |
, dim: bool |
| 104 |
, underline: bool |
| 105 |
, blink: bool |
| 106 |
, reverse: bool |
| 107 |
, italic: bool |
| 108 |
, strike: bool |
| 109 |
} |
| 110 |
val defaultStyle: style |
| 111 |
val toString: style -> string |
| 112 |
val resetAll: string |
| 113 |
end = |
| 114 |
struct |
| 115 |
type style = |
| 116 |
{ foreground: ANSIColor.color option |
| 117 |
, background: ANSIColor.color option |
| 118 |
, bold: bool |
| 119 |
, dim: bool |
| 120 |
, underline: bool |
| 121 |
, blink: bool |
| 122 |
, reverse: bool |
| 123 |
, italic: bool |
| 124 |
, strike: bool |
| 125 |
} |
| 126 |
val defaultStyle: style = |
| 127 |
{ foreground = NONE |
| 128 |
, background = NONE |
| 129 |
, bold = false |
| 130 |
, dim = false |
| 131 |
, underline = false |
| 132 |
, blink = false |
| 133 |
, reverse = false |
| 134 |
, italic = false |
| 135 |
, strike = false |
| 136 |
} |
| 137 |
fun prependOption (SOME x, xs) = x :: xs |
| 138 |
| prependOption (NONE, xs) = xs |
| 139 |
fun toString |
| 140 |
({ foreground |
| 141 |
, background |
| 142 |
, bold |
| 143 |
, dim |
| 144 |
, underline |
| 145 |
, blink |
| 146 |
, reverse |
| 147 |
, italic |
| 148 |
, strike |
| 149 |
}: style) = |
| 150 |
let |
| 151 |
val attrs = [] |
| 152 |
val attrs = if strike then "9" :: attrs else attrs |
| 153 |
val attrs = if reverse then "7" :: attrs else attrs |
| 154 |
val attrs = if blink then "5" :: attrs else attrs |
| 155 |
val attrs = if underline then "4" :: attrs else attrs |
| 156 |
val attrs = if italic then "3" :: attrs else attrs |
| 157 |
val attrs = if dim then "2" :: attrs else attrs |
| 158 |
val attrs = if bold then "1" :: attrs else attrs |
| 159 |
val attrs = prependOption |
| 160 |
(Option.mapPartial ANSIColor.asBackground background, attrs) |
| 161 |
val attrs = prependOption |
| 162 |
(Option.mapPartial ANSIColor.asForeground foreground, attrs) |
| 163 |
in |
| 164 |
"\027[" ^ String.concatWith ";" attrs ^ "m" |
| 165 |
end |
| 166 |
val resetAll: string = "\027[0m" |
| 167 |
end; |