github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/util/pprinter.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  // Pprinter wraps the Pprint function.
     9  type Pprinter interface {
    10  	// Pprint takes an indentation string and pretty-prints.
    11  	Pprint(indent string) string
    12  }
    13  
    14  // PprintError pretty-prints an error if it implements Pprinter, and prints it
    15  // in bold and red otherwise.
    16  func PprintError(err error) {
    17  	if pprinter, ok := err.(Pprinter); ok {
    18  		fmt.Fprintln(os.Stderr, pprinter.Pprint(""))
    19  	} else {
    20  		fmt.Fprintf(os.Stderr, "\033[31;1m%s\033[m", err.Error())
    21  	}
    22  }