github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/color/color.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package color
    19  
    20  import (
    21  	"fmt"
    22  
    23  	"github.com/fatih/color"
    24  )
    25  
    26  // global colors.
    27  var (
    28  	// Check if we stderr, stdout are dumb terminals, we do not apply
    29  	// ansi coloring on dumb terminals.
    30  	IsTerminal = func() bool {
    31  		return !color.NoColor
    32  	}
    33  
    34  	Bold = func() func(format string, a ...interface{}) string {
    35  		if IsTerminal() {
    36  			return color.New(color.Bold).SprintfFunc()
    37  		}
    38  		return fmt.Sprintf
    39  	}()
    40  
    41  	RedBold = func() func(a ...interface{}) string {
    42  		if IsTerminal() {
    43  			return color.New(color.FgRed, color.Bold).SprintFunc()
    44  		}
    45  		return fmt.Sprint
    46  	}()
    47  
    48  	RedBoldf = func() func(format string, a ...interface{}) string {
    49  		if IsTerminal() {
    50  			return color.New(color.FgRed, color.Bold).SprintfFunc()
    51  		}
    52  		return fmt.Sprintf
    53  	}()
    54  
    55  	Red = func() func(format string, a ...interface{}) string {
    56  		if IsTerminal() {
    57  			return color.New(color.FgRed).SprintfFunc()
    58  		}
    59  		return fmt.Sprintf
    60  	}()
    61  
    62  	Blue = func() func(format string, a ...interface{}) string {
    63  		if IsTerminal() {
    64  			return color.New(color.FgBlue).SprintfFunc()
    65  		}
    66  		return fmt.Sprintf
    67  	}()
    68  
    69  	Yellow = func() func(format string, a ...interface{}) string {
    70  		if IsTerminal() {
    71  			return color.New(color.FgYellow).SprintfFunc()
    72  		}
    73  		return fmt.Sprintf
    74  	}()
    75  
    76  	Green = func() func(a ...interface{}) string {
    77  		if IsTerminal() {
    78  			return color.New(color.FgGreen).SprintFunc()
    79  		}
    80  		return fmt.Sprint
    81  	}()
    82  
    83  	Greenf = func() func(format string, a ...interface{}) string {
    84  		if IsTerminal() {
    85  			return color.New(color.FgGreen).SprintfFunc()
    86  		}
    87  		return fmt.Sprintf
    88  	}()
    89  
    90  	GreenBold = func() func(a ...interface{}) string {
    91  		if IsTerminal() {
    92  			return color.New(color.FgGreen, color.Bold).SprintFunc()
    93  		}
    94  		return fmt.Sprint
    95  	}()
    96  
    97  	CyanBold = func() func(a ...interface{}) string {
    98  		if IsTerminal() {
    99  			return color.New(color.FgCyan, color.Bold).SprintFunc()
   100  		}
   101  		return fmt.Sprint
   102  	}()
   103  
   104  	YellowBold = func() func(format string, a ...interface{}) string {
   105  		if IsTerminal() {
   106  			return color.New(color.FgYellow, color.Bold).SprintfFunc()
   107  		}
   108  		return fmt.Sprintf
   109  	}()
   110  
   111  	BlueBold = func() func(format string, a ...interface{}) string {
   112  		if IsTerminal() {
   113  			return color.New(color.FgBlue, color.Bold).SprintfFunc()
   114  		}
   115  		return fmt.Sprintf
   116  	}()
   117  
   118  	BgYellow = func() func(format string, a ...interface{}) string {
   119  		if IsTerminal() {
   120  			return color.New(color.BgYellow).SprintfFunc()
   121  		}
   122  		return fmt.Sprintf
   123  	}()
   124  
   125  	Black = func() func(format string, a ...interface{}) string {
   126  		if IsTerminal() {
   127  			return color.New(color.FgBlack).SprintfFunc()
   128  		}
   129  		return fmt.Sprintf
   130  	}()
   131  
   132  	FgRed = func() func(a ...interface{}) string {
   133  		if IsTerminal() {
   134  			return color.New(color.FgRed).SprintFunc()
   135  		}
   136  		return fmt.Sprint
   137  	}()
   138  
   139  	BgRed = func() func(format string, a ...interface{}) string {
   140  		if IsTerminal() {
   141  			return color.New(color.BgRed).SprintfFunc()
   142  		}
   143  		return fmt.Sprintf
   144  	}()
   145  
   146  	FgWhite = func() func(format string, a ...interface{}) string {
   147  		if IsTerminal() {
   148  			return color.New(color.FgWhite).SprintfFunc()
   149  		}
   150  		return fmt.Sprintf
   151  	}()
   152  )