github.com/yourbase/yb@v0.7.1/cmd/yb/styles.go (about)

     1  // Copyright 2021 YourBase Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //		 https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  // SPDX-License-Identifier: Apache-2.0
    16  
    17  package main
    18  
    19  import (
    20  	"os"
    21  	"strconv"
    22  
    23  	"github.com/yourbase/commons/envvar"
    24  )
    25  
    26  // termStyles generates ANSI escape codes for specific styles.
    27  // The zero value will not return any special escape codes.
    28  // https://en.wikipedia.org/wiki/ANSI_escape_code
    29  type termStyles bool
    30  
    31  func termStylesFromEnv() termStyles {
    32  	if os.Getenv("NO_COLOR") != "" {
    33  		// https://no-color.org/
    34  		return false
    35  	}
    36  	b, _ := strconv.ParseBool(envvar.Get("CLICOLOR", "1"))
    37  	return termStyles(b)
    38  }
    39  
    40  // reset returns the escape code to change the output to default settings.
    41  func (style termStyles) reset() string {
    42  	if !style {
    43  		return ""
    44  	}
    45  	return "\x1b[0m"
    46  }
    47  
    48  // target returns the escape code for formatting a target section heading.
    49  func (style termStyles) target() string {
    50  	if !style {
    51  		return ""
    52  	}
    53  	// Bold
    54  	return "\x1b[1m"
    55  }
    56  
    57  // command returns the escape code for formatting a command section heading.
    58  func (style termStyles) command() string {
    59  	return style.target()
    60  }
    61  
    62  // buildResult returns the escape code for formatting the final result message.
    63  func (style termStyles) buildResult(success bool) string {
    64  	switch {
    65  	case !bool(style):
    66  		return ""
    67  	case !success:
    68  		return style.failure()
    69  	default:
    70  		// Bold
    71  		return "\x1b[1m"
    72  	}
    73  }
    74  
    75  // failure returns the escape code for formatting error messages.
    76  func (style termStyles) failure() string {
    77  	if !style {
    78  		return ""
    79  	}
    80  	// Red, but not bold to avoid using "bright red".
    81  	return "\x1b[31m"
    82  }
    83  
    84  // debug returns the escape code for formatting debugging information.
    85  func (style termStyles) debug() string {
    86  	if !style {
    87  		return ""
    88  	}
    89  	// Gray
    90  	return "\x1b[90m"
    91  }