github.com/whatap/golib@v0.0.22/util/ansi/AnsiPrint.go (about)

     1  package ansi
     2  
     3  /*
     4   *  Copyright 2015 Scouter Project.
     5   *  @https://github.com/scouter-project/scouter
     6   *
     7   *  Licensed under the Apache License, Version 2.0 (the "License");
     8   *  you may not use this file except in compliance with the License.
     9   *  You may obtain a copy of the License at
    10   *
    11   *      http://www.apache.org/licenses/LICENSE-2.0
    12   *
    13   *  Unless required by applicable law or agreed to in writing, software
    14   *  distributed under the License is distributed on an "AS IS" BASIS,
    15   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16   *  See the License for the specific language governing permissions and
    17   *  limitations under the License.
    18   *
    19   */
    20  
    21  import (
    22  	"fmt"
    23  )
    24  
    25  var (
    26  	enable bool
    27  )
    28  
    29  const (
    30  	ANSI_RESET  = "\u001B[0m"
    31  	ANSI_BLACK  = "\u001B[30m"
    32  	ANSI_RED    = "\u001B[31m"
    33  	ANSI_GREEN  = "\u001B[32m"
    34  	ANSI_YELLOW = "\u001B[33m"
    35  	ANSI_BLUE   = "\u001B[34m"
    36  	ANSI_PURPLE = "\u001B[35m"
    37  	ANSI_CYAN   = "\u001B[36m"
    38  	ANSI_WHITE  = "\u001B[37m"
    39  )
    40  
    41  func init() {
    42  	//SystemUtil.IS_WINDOWS == false;
    43  	enable = true
    44  }
    45  
    46  func Red(s string) string {
    47  	if enable == false {
    48  		return s
    49  	}
    50  	return fmt.Sprintf("%s%s%s", ANSI_RED, s, ANSI_RESET)
    51  }
    52  
    53  func Yellow(s string) string {
    54  	if enable == false {
    55  		return s
    56  	}
    57  	return fmt.Sprintf("%s%s%s", ANSI_YELLOW, s, ANSI_RESET)
    58  }
    59  
    60  func Green(s string) string {
    61  	if enable == false {
    62  		return s
    63  	}
    64  	return fmt.Sprintf("%s%s%s", ANSI_GREEN, s, ANSI_RESET)
    65  }
    66  
    67  func Cyan(s string) string {
    68  	if enable == false {
    69  		return s
    70  	}
    71  	return fmt.Sprintf("%s%s%s", ANSI_CYAN, s, ANSI_RESET)
    72  }
    73  
    74  func Blue(s string) string {
    75  	if enable == false {
    76  		return s
    77  	}
    78  	return fmt.Sprintf("%s%s%s", ANSI_BLUE, s, ANSI_RESET)
    79  }
    80  
    81  func RedOut(s string) {
    82  	fmt.Printf("%s\n", Red(s))
    83  }