mirror of
https://github.com/go-sylixos/elvish.git
synced 2024-12-13 18:07:51 +08:00
34 lines
797 B
Go
34 lines
797 B
Go
package diag
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/elves/elvish/util"
|
|
)
|
|
|
|
// 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) {
|
|
if pprinter, ok := err.(util.PPrinter); ok {
|
|
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...))
|
|
}
|