github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/diag/show_error.go (about)

     1  package diag
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  )
     7  
     8  // ShowError shows an error. It uses the Show method if the error
     9  // implements Shower, and uses Complain to print the error message otherwise.
    10  func ShowError(w io.Writer, err error) {
    11  	if shower, ok := err.(Shower); ok {
    12  		fmt.Fprintln(w, shower.Show(""))
    13  	} else {
    14  		Complain(w, err.Error())
    15  	}
    16  }
    17  
    18  // Complain prints a message to w in bold and red, adding a trailing newline.
    19  func Complain(w io.Writer, msg string) {
    20  	fmt.Fprintf(w, "\033[31;1m%s\033[m\n", msg)
    21  }
    22  
    23  // Complainf is like Complain, but accepts a format string and arguments.
    24  func Complainf(w io.Writer, format string, args ...interface{}) {
    25  	Complain(w, fmt.Sprintf(format, args...))
    26  }