elvish/util/contextual-error.go

43 lines
1.1 KiB
Go
Raw Normal View History

2013-10-18 11:38:30 +08:00
package util
import (
"bytes"
2014-01-16 09:24:14 +08:00
"fmt"
"strings"
)
2013-10-18 11:38:30 +08:00
type ContextualError struct {
srcname string
title string
line string
lineno int
colno int
msg string
}
func NewContextualError(srcname, title, text string, pos int, format string, args ...interface{}) *ContextualError {
2013-10-18 11:38:30 +08:00
lineno, colno, line := FindContext(text, pos)
return &ContextualError{srcname, title, line, lineno, colno, fmt.Sprintf(format, args...)}
2013-10-18 11:38:30 +08:00
}
func (e *ContextualError) Error() string {
return fmt.Sprintf("%s:%d:%d %s:%s", e.srcname, e.lineno, e.colno, e.title, e.msg)
}
2013-10-18 11:38:30 +08:00
func (e *ContextualError) Pprint() string {
buf := new(bytes.Buffer)
// Position info
fmt.Fprintf(buf, "\033[1m%s:%d:%d: ", e.srcname, e.lineno+1, e.colno+1)
// "error:"
fmt.Fprintf(buf, "\033[31m%s: ", e.title)
// Message
fmt.Fprintf(buf, "\033[m\033[1m%s\033[m\n", e.msg)
// Context: line
// TODO Handle long lines
fmt.Fprintf(buf, "%s\n", e.line)
// Context: arrow
// TODO Handle multi-width characters
fmt.Fprintf(buf, "%s\033[32;1m^\033[m\n", strings.Repeat(" ", e.colno))
return buf.String()
}