gitee.com/go-spring2/spring-base@v1.1.3/color/color.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     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   *      https://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 provides some console output formats.
    18  package color
    19  
    20  import (
    21  	"bytes"
    22  	"fmt"
    23  )
    24  
    25  const (
    26  	Bold         Attribute = "1"
    27  	Italic       Attribute = "3"
    28  	Underline    Attribute = "4"
    29  	ReverseVideo Attribute = "7"
    30  	CrossedOut   Attribute = "9"
    31  )
    32  
    33  const (
    34  	Black   Attribute = "30"
    35  	Red     Attribute = "31"
    36  	Green   Attribute = "32"
    37  	Yellow  Attribute = "33"
    38  	Blue    Attribute = "34"
    39  	Magenta Attribute = "35"
    40  	Cyan    Attribute = "36"
    41  	White   Attribute = "37"
    42  )
    43  
    44  const (
    45  	BgBlack   Attribute = "40"
    46  	BgRed     Attribute = "41"
    47  	BgGreen   Attribute = "42"
    48  	BgYellow  Attribute = "43"
    49  	BgBlue    Attribute = "44"
    50  	BgMagenta Attribute = "45"
    51  	BgCyan    Attribute = "46"
    52  	BgWhite   Attribute = "47"
    53  )
    54  
    55  type Attribute string
    56  
    57  // Sprint returns a string formatted according to console properties.
    58  func (attr Attribute) Sprint(a ...interface{}) string {
    59  	return wrap([]Attribute{attr}, fmt.Sprint(a...))
    60  }
    61  
    62  // Sprintf returns a string formatted according to console properties.
    63  func (attr Attribute) Sprintf(format string, a ...interface{}) string {
    64  	return wrap([]Attribute{attr}, fmt.Sprintf(format, a...))
    65  }
    66  
    67  type Text struct {
    68  	attributes []Attribute
    69  }
    70  
    71  // NewText returns a new *Text.
    72  func NewText(attributes ...Attribute) *Text {
    73  	return &Text{attributes: attributes}
    74  }
    75  
    76  // Sprint returns a string formatted according to console properties.
    77  func (c *Text) Sprint(a ...interface{}) string {
    78  	return wrap(c.attributes, fmt.Sprint(a...))
    79  }
    80  
    81  // Sprintf returns a string formatted according to console properties.
    82  func (c *Text) Sprintf(format string, a ...interface{}) string {
    83  	return wrap(c.attributes, fmt.Sprintf(format, a...))
    84  }
    85  
    86  func wrap(attributes []Attribute, str string) string {
    87  	if len(attributes) == 0 {
    88  		return str
    89  	}
    90  	var buf bytes.Buffer
    91  	buf.WriteString("\x1b[")
    92  	for i := 0; i < len(attributes); i++ {
    93  		buf.WriteString(string(attributes[i]))
    94  		if i < len(attributes)-1 {
    95  			buf.WriteByte(';')
    96  		}
    97  	}
    98  	buf.WriteByte('m')
    99  	buf.WriteString(str)
   100  	buf.WriteString("\x1b[0m")
   101  	return buf.String()
   102  }