github.com/rakutentech/cli@v6.12.5-0.20151006231303-24468b65536e+incompatible/cf/terminal/tee_printer.go (about)

     1  package terminal
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type Printer interface {
     8  	Print(a ...interface{}) (n int, err error)
     9  	Printf(format string, a ...interface{}) (n int, err error)
    10  	Println(a ...interface{}) (n int, err error)
    11  	ForcePrint(a ...interface{}) (n int, err error)
    12  	ForcePrintf(format string, a ...interface{}) (n int, err error)
    13  	ForcePrintln(a ...interface{}) (n int, err error)
    14  }
    15  
    16  type OutputCapture interface {
    17  	SetOutputBucket(*[]string)
    18  }
    19  
    20  type TerminalOutputSwitch interface {
    21  	DisableTerminalOutput(bool)
    22  }
    23  
    24  type TeePrinter struct {
    25  	disableTerminalOutput bool
    26  	outputBucket          *[]string
    27  }
    28  
    29  func NewTeePrinter() *TeePrinter {
    30  	return &TeePrinter{}
    31  }
    32  
    33  func (t *TeePrinter) SetOutputBucket(bucket *[]string) {
    34  	t.outputBucket = bucket
    35  }
    36  
    37  func (t *TeePrinter) Print(values ...interface{}) (n int, err error) {
    38  	str := fmt.Sprint(values...)
    39  	t.saveOutputToBucket(str)
    40  	if !t.disableTerminalOutput {
    41  		return fmt.Print(str)
    42  	}
    43  	return
    44  }
    45  
    46  func (t *TeePrinter) Printf(format string, a ...interface{}) (n int, err error) {
    47  	str := fmt.Sprintf(format, a...)
    48  	t.saveOutputToBucket(str)
    49  	if !t.disableTerminalOutput {
    50  		return fmt.Print(str)
    51  	}
    52  	return
    53  }
    54  
    55  func (t *TeePrinter) Println(values ...interface{}) (n int, err error) {
    56  	str := fmt.Sprint(values...)
    57  	t.saveOutputToBucket(str)
    58  	if !t.disableTerminalOutput {
    59  		return fmt.Println(str)
    60  	}
    61  	return
    62  }
    63  
    64  func (t *TeePrinter) ForcePrint(values ...interface{}) (n int, err error) {
    65  	str := fmt.Sprint(values...)
    66  	t.saveOutputToBucket(str)
    67  	return fmt.Print(str)
    68  }
    69  
    70  func (t *TeePrinter) ForcePrintf(format string, a ...interface{}) (n int, err error) {
    71  	str := fmt.Sprintf(format, a...)
    72  	t.saveOutputToBucket(str)
    73  	return fmt.Print(str)
    74  }
    75  
    76  func (t *TeePrinter) ForcePrintln(values ...interface{}) (n int, err error) {
    77  	str := fmt.Sprint(values...)
    78  	t.saveOutputToBucket(str)
    79  	return fmt.Println(str)
    80  }
    81  
    82  func (t *TeePrinter) DisableTerminalOutput(disable bool) {
    83  	t.disableTerminalOutput = disable
    84  }
    85  
    86  func (t *TeePrinter) saveOutputToBucket(output string) {
    87  	if t.outputBucket == nil {
    88  		return
    89  	}
    90  
    91  	*t.outputBucket = append(*t.outputBucket, Decolorize(output))
    92  }