go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/ansi/color.go (about) 1 package ansi 2 3 // Black applies a given color. 4 func Black(text string) string { 5 return Apply(ColorBlack, text) 6 } 7 8 // Red applies a given color. 9 func Red(text string) string { 10 return Apply(ColorRed, text) 11 } 12 13 // Green applies a given color. 14 func Green(text string) string { 15 return Apply(ColorGreen, text) 16 } 17 18 // Yellow applies a given color. 19 func Yellow(text string) string { 20 return Apply(ColorYellow, text) 21 } 22 23 // Blue applies a given color. 24 func Blue(text string) string { 25 return Apply(ColorBlue, text) 26 } 27 28 // Purple applies a given color. 29 func Purple(text string) string { 30 return Apply(ColorPurple, text) 31 } 32 33 // Cyan applies a given color. 34 func Cyan(text string) string { 35 return Apply(ColorCyan, text) 36 } 37 38 // White applies a given color. 39 func White(text string) string { 40 return Apply(ColorWhite, text) 41 } 42 43 // LightBlack applies a given color. 44 func LightBlack(text string) string { 45 return Apply(ColorLightBlack, text) 46 } 47 48 // LightRed applies a given color. 49 func LightRed(text string) string { 50 return Apply(ColorLightRed, text) 51 } 52 53 // LightGreen applies a given color. 54 func LightGreen(text string) string { 55 return Apply(ColorLightGreen, text) 56 } 57 58 // LightYellow applies a given color. 59 func LightYellow(text string) string { 60 return Apply(ColorLightYellow, text) 61 } 62 63 // LightBlue applies a given color. 64 func LightBlue(text string) string { 65 return Apply(ColorLightBlue, text) 66 } 67 68 // LightPurple applies a given color. 69 func LightPurple(text string) string { 70 return Apply(ColorLightPurple, text) 71 } 72 73 // LightCyan applies a given color. 74 func LightCyan(text string) string { 75 return Apply(ColorLightCyan, text) 76 } 77 78 // LightWhite applies a given color. 79 func LightWhite(text string) string { 80 return Apply(ColorLightWhite, text) 81 } 82 83 // Apply applies a given color. 84 func Apply(colorCode Color, text string) string { 85 return colorCode.Normal() + text + ColorReset 86 } 87 88 // Bold applies a given color as bold. 89 func Bold(colorCode Color, text string) string { 90 return colorCode.Bold() + text + ColorReset 91 } 92 93 // Underline applies a given color as underline. 94 func Underline(colorCode Color, text string) string { 95 return colorCode.Underline() + text + ColorReset 96 } 97 98 // Color represents an ansi color code fragment. 99 type Color string 100 101 // Normal escapes the color for use in the terminal. 102 func (c Color) Normal() string { 103 return "\033[0;" + string(c) 104 } 105 106 // Bold escapes the color for use in the terminal as a bold color. 107 func (c Color) Bold() string { 108 return "\033[1;" + string(c) 109 } 110 111 // Underline escapes the color for use in the terminal as underlined color. 112 func (c Color) Underline() string { 113 return "\033[4;" + string(c) 114 } 115 116 // Apply applies a color to a given string. 117 func (c Color) Apply(text string) string { 118 return Apply(c, text) 119 } 120 121 // Utility Color Codes 122 const ( 123 ColorReset = "\033[0m" 124 ) 125 126 // Color codes 127 const ( 128 ColorDefault Color = "39m" 129 ColorBlack Color = "30m" 130 ColorRed Color = "31m" 131 ColorGreen Color = "32m" 132 ColorYellow Color = "33m" 133 ColorBlue Color = "34m" 134 ColorPurple Color = "35m" 135 ColorCyan Color = "36m" 136 ColorWhite Color = "37m" 137 ) 138 139 // BrightColorCodes 140 const ( 141 ColorLightBlack Color = "90m" 142 ColorLightRed Color = "91m" 143 ColorLightGreen Color = "92m" 144 ColorLightYellow Color = "93m" 145 ColorLightBlue Color = "94m" 146 ColorLightPurple Color = "95m" 147 ColorLightCyan Color = "96m" 148 ColorLightWhite Color = "97m" 149 ) 150 151 // BackgroundColorCodes 152 const ( 153 ColorBackgroundBlack Color = "40m" 154 ColorBackgroundRed Color = "41m" 155 ColorBackgroundGreen Color = "42m" 156 ColorBackgroundYellow Color = "43m" 157 ColorBackgroundBlue Color = "44m" 158 ColorBackgroundPurple Color = "45m" 159 ColorBackgroundCyan Color = "46m" 160 ColorBackgroundWhite Color = "47m" 161 ) 162 163 // BackgroundColorCodes 164 const ( 165 ColorBackgroundBrightBlack Color = "100m" 166 ColorBackgroundBrightRed Color = "101m" 167 ColorBackgroundBrightGreen Color = "102m" 168 ColorBackgroundBrightYellow Color = "103m" 169 ColorBackgroundBrightBlue Color = "104m" 170 ColorBackgroundBrightPurple Color = "105m" 171 ColorBackgroundBrightCyan Color = "106m" 172 ColorBackgroundBrightWhite Color = "107m" 173 )