github.com/minio/console@v1.4.1/pkg/logger/utils.go (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2022 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package logger
    18  
    19  import (
    20  	"fmt"
    21  	"regexp"
    22  	"runtime"
    23  
    24  	"github.com/minio/console/pkg/logger/color"
    25  )
    26  
    27  var ansiRE = regexp.MustCompile("(\x1b[^m]*m)")
    28  
    29  // Print ANSI Control escape
    30  func ansiEscape(format string, args ...interface{}) {
    31  	Esc := "\x1b"
    32  	fmt.Printf("%s%s", Esc, fmt.Sprintf(format, args...))
    33  }
    34  
    35  func ansiMoveRight(n int) {
    36  	if runtime.GOOS == "windows" {
    37  		return
    38  	}
    39  	if color.IsTerminal() {
    40  		ansiEscape("[%dC", n)
    41  	}
    42  }
    43  
    44  func ansiSaveAttributes() {
    45  	if runtime.GOOS == "windows" {
    46  		return
    47  	}
    48  	if color.IsTerminal() {
    49  		ansiEscape("7")
    50  	}
    51  }
    52  
    53  func ansiRestoreAttributes() {
    54  	if runtime.GOOS == "windows" {
    55  		return
    56  	}
    57  	if color.IsTerminal() {
    58  		ansiEscape("8")
    59  	}
    60  }