github.com/rvichery/terraform@v0.11.10/backend/remote/colorize.go (about) 1 package remote 2 3 import ( 4 "regexp" 5 6 "github.com/mitchellh/colorstring" 7 ) 8 9 // colorsRe is used to find ANSI escaped color codes. 10 var colorsRe = regexp.MustCompile("\033\\[\\d{1,3}m") 11 12 // Colorer is the interface that must be implemented to colorize strings. 13 type Colorer interface { 14 Color(v string) string 15 } 16 17 // Colorize is used to print output when the -no-color flag is used. It will 18 // strip all ANSI escaped color codes which are set while the operation was 19 // executed in Terraform Enterprise. 20 // 21 // When Terraform Enterprise supports run specific variables, this code can be 22 // removed as we can then pass the CLI flag to the backend and prevent the color 23 // codes from being written to the output. 24 type Colorize struct { 25 cliColor *colorstring.Colorize 26 } 27 28 // Color will strip all ANSI escaped color codes and return a uncolored string. 29 func (c *Colorize) Color(v string) string { 30 return colorsRe.ReplaceAllString(c.cliColor.Color(v), "") 31 } 32 33 // Colorize returns the Colorize structure that can be used for colorizing 34 // output. This is guaranteed to always return a non-nil value and so is useful 35 // as a helper to wrap any potentially colored strings. 36 func (b *Remote) Colorize() Colorer { 37 if b.CLIColor != nil && !b.CLIColor.Disable { 38 return b.CLIColor 39 } 40 if b.CLIColor != nil { 41 return &Colorize{cliColor: b.CLIColor} 42 } 43 return &Colorize{cliColor: &colorstring.Colorize{ 44 Colors: colorstring.DefaultColors, 45 Disable: true, 46 }} 47 }