github.com/fnproject/cli@v0.0.0-20240508150455-e5d88bd86117/common/color/color.go (about) 1 /* 2 * Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package color 18 19 import ( 20 "os" 21 "strings" 22 23 "github.com/mattn/go-isatty" 24 ) 25 26 var useColors bool 27 28 var Colors map[string]interface{} 29 30 func init() { 31 useColors = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) 32 Colors = map[string]interface{}{ 33 "bold": Bold, 34 "italic": Italic, 35 "join": strings.Join, 36 "trim": strings.TrimLeft, 37 "cyan": Cyan, 38 "brightcyan": BrightCyan, 39 "boldcyan": BoldCyan, 40 "yellow": Yellow, 41 "red": Red, 42 "brightred": BrightRed, 43 "boldred": BoldRed, 44 "underlinebrightred": UnderlineBrightRed, 45 } 46 } 47 48 func Bold(text string) string { 49 if useColors { 50 return "\x1b[1m" + text + "\x1b[0m" 51 } else { 52 return text 53 } 54 } 55 56 func Italic(text string) string { 57 if useColors { 58 return "\x1b[3m" + text + "\x1b[0m" 59 } else { 60 return text 61 } 62 } 63 64 func BoldRed(text string) string { 65 if useColors { 66 return "\x1b[31;1m" + text + "\x1b[0m" 67 } else { 68 return text 69 } 70 } 71 72 func BrightRed(text string) string { 73 if useColors { 74 return "\x1b[91;21m" + text + "\x1b[0m" 75 } else { 76 return text 77 } 78 } 79 80 func Red(text string) string { 81 if useColors { 82 return "\x1b[31;21m" + text + "\x1b[0m" 83 } else { 84 return text 85 } 86 } 87 88 func UnderlineBrightRed(text string) string { 89 if useColors { 90 return "\x1b[91;4m" + text + "\x1b[0m" 91 } else { 92 return text 93 } 94 } 95 96 func BrightCyan(text string) string { 97 if useColors { 98 return "\x1b[96;21m" + text + "\x1b[0m" 99 } else { 100 return text 101 } 102 } 103 104 func Cyan(text string) string { 105 if useColors { 106 return "\x1b[36;21m" + text + "\x1b[0m" 107 } else { 108 return text 109 } 110 } 111 112 func BoldCyan(text string) string { 113 if useColors { 114 return "\x1b[36;1m" + text + "\x1b[0m" 115 } else { 116 return text 117 } 118 } 119 120 func Yellow(text string) string { 121 if useColors { 122 return "\x1b[33;21m" + text + "\x1b[0m" 123 } else { 124 return text 125 } 126 }