Beispiel #1
0
// backendNewColor returns a new colorful error string using the following
// format:
//    pkg.func (file:line): error: text
func backendNewColor(text string, skip int) error {
	pc, file, line, ok := runtime.Caller(skip)
	if !ok {
		return errors.New(term.RedBold("error: ") + text)
	}
	f := runtime.FuncForPC(pc)
	if f == nil {
		format := term.WhiteBold("(%s:%d): ") + term.RedBold("error: ") + "%s"
		return fmt.Errorf(format, path.Base(file), line, text)
	}
	format := term.MagentaBold("%s") + term.WhiteBold(" (%s:%d): ") + term.RedBold("error: ") + "%s"
	return fmt.Errorf(format, f.Name(), path.Base(file), line, text)
}
Beispiel #2
0
// grep is helper function to display the relative (target) line from a source string.
func grep(source interface{}, target string) string {
	var str string
	switch source.(type) {
	case []byte:
		str = string(source.([]byte))
	case string:
		str = source.(string)
	default:
		// working on it
	}
	defer func() {
		backup([]byte(str))
	}()

	lines := strings.Split(str, "\n")

	for _, line := range lines {
		if strings.Contains(line, target) {
			return clr.MagentaBold("Found: ") + clr.MagentaBold(line)
		}
	}
	return ""
}
Beispiel #3
0
Datei: err.go Projekt: mewkiz/pkg
func (pos *position) String() string {
	if pos == nil {
		return "<no position>"
	}
	filePos := fmt.Sprintf("(%s:%d):", pos.file, pos.line)

	if UseColor {
		// Use colors.
		filePosColor := term.WhiteBold(filePos)
		if pos.callee == "" {
			return filePosColor
		}
		return fmt.Sprintf("%s %s", term.MagentaBold(pos.callee), filePosColor)
	}

	// No colors.
	if pos.callee == "" {
		return filePos
	}
	return fmt.Sprintf("%s %s", pos.callee, filePos)
}