2018-10-11 00:20:32 +08:00
|
|
|
package diag
|
2018-10-08 19:05:40 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
2018-10-11 00:20:32 +08:00
|
|
|
|
|
|
|
"github.com/elves/elvish/util"
|
2018-10-08 19:05:40 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Can be changed for testing.
|
|
|
|
var stderr io.Writer = os.Stderr
|
|
|
|
|
|
|
|
// PprintError pretty-prints an error. It uses the Pprint method if the error
|
|
|
|
// implements Pprinter, and uses Complain to print the error message otherwise.
|
|
|
|
func PprintError(err error) {
|
2018-10-11 00:20:32 +08:00
|
|
|
if pprinter, ok := err.(util.Pprinter); ok {
|
2018-10-08 19:05:40 +08:00
|
|
|
fmt.Fprintln(stderr, pprinter.Pprint(""))
|
|
|
|
} else {
|
|
|
|
Complain(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complain prints a message to stderr in bold and red, adding a trailing
|
|
|
|
// newline.
|
|
|
|
func Complain(msg string) {
|
|
|
|
fmt.Fprintf(stderr, "\033[31;1m%s\033[m\n", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complainf is like Complain, but accepts a format string and arguments.
|
|
|
|
func Complainf(format string, args ...interface{}) {
|
|
|
|
Complain(fmt.Sprintf(format, args...))
|
|
|
|
}
|