Documentation ¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultGutter separates line numbers from content. DefaultGutter = " | " // DefaultPointer is the rune that points up at a region. DefaultPointer = '^' // DefaultContext is the default amount of context lines surrounding an error. // It is used by New* functions. DefaultContext = 0 // NoColor disables color output. NoColor bool )
var ( // ErrNoMatch is returned by Parse when no file path or position can be found. ErrNoMatch = fmt.Errorf("line does not match position pattern: %s", matchRegionColon.String()) )
Functions ¶
This section is empty.
Types ¶
type Ctx ¶
type Ctx struct { Lines []string // Context is the maximum amount of context lines surrounding the marked region. // // 0: no lines of context. // -1: all lines, the full input string // 3: limited context of 3 lines Context int Region // Path to the source of the context (optional). Path string // Hint that is displayed near the region markers (optional). Hint string // Err is the error that occurred at this region (optional). Err error }
Ctx points to runes in (multiline) strings.
func New ¶
New returns a new Ctx pointing to a region in an input string.
Use functions Point and Range to create a Region.
Example ¶
fmt.Println(New("source code containing error", Range(1, 24, 1, 28)))
Output: 1:24-28: 1 | source code containing error | ^^^^^
func NewFromPath ¶
NewFromPath returns a new Ctx pointing to a region in the given file. Returns an error when the file does not exist or could not be read.
Example ¶
cerr, err := NewFromPath("LICENSE", Range(1, 1, 1, 3)) if err != nil { fmt.Println(err) return } cerr.Context = 0 fmt.Println(cerr)
Output: LICENSE:1:1-3: 1 | MIT License | ^^^
func Parse ¶
Parse a single line with at least a file path and position. Returns error ErrNoMatch when this does not appear to be an error line. Otherwise will return parsing errors or nil on success.
foo.go:15:1: message foo.go[1, 2]: message
func (Ctx) Error ¶
Error formats a summary of this context error.
Example ¶
err := New("ab!cd", Point(1, 3)) err.Err = fmt.Errorf("not a letter") fmt.Println(err)
Output: not a letter 1:3: 1 | ab!cd | ^
Example (ContextAll) ¶
in := `1st 2nd 3rd has an error 4th 5th` ctx := New(in, Point(3, 12)) ctx.Context = -1 fmt.Println(ctx)
Output: 3:12: 1 | 1st 2 | 2nd 3 | 3rd has an error | ^ 4 | 4th 5 | 5th
Example (ContextLimited) ¶
in := `1st 2nd 3rd has an error 4th 5th` ctx := New(in, Point(3, 12)) ctx.Context = 1 fmt.Println(ctx)
Output: 3:12: 2 | 2nd 3 | 3rd has an error | ^ 4 | 4th
Example (ContextLimitedMultiline) ¶
in := `1st 2nd 3rd has an error 4th still has an error 5th` ctx := New(in, Range(3, 1, 4, 22)) ctx.Context = 1 fmt.Println(ctx)
Output: 3:1-4:22: 2 | 2nd 3 | 3rd has an error | ^^^^^^^^^^^^^^^^ 4 | 4th still has an error | ^^^^^^^^^^^^^^^^^^^^^^ 5 | 5th
Example (Hint) ¶
ctx := New("010101102110", Point(1, 9)) ctx.Hint = "don't worry, bender" fmt.Println(ctx)
Output: 1:9: 1 | 010101102110 | ^ don't worry, bender
Example (Path) ¶
ctx := New("42", Point(1, 1)) ctx.Path = "/tmp/ctxerr/answer.txt" fmt.Println(ctx)
Output: /tmp/ctxerr/answer.txt:1:1: 1 | 42 | ^
Example (Tabwidth) ¶
in := "\tfoo\tbar" ctx := New(in, Point(1, 5)) fmt.Println(ctx)
Output: 1:5: 1 | foo bar | ^^^^
type Position ¶
type Position struct {
Line, Col int
}
Position of a single point in a multiline string.
type Region ¶
type Region struct {
Start, End Position
}
Region defines a selection of runes (utf8 codepoints) in a string.
func Point ¶
Point returns a Region pointing to a specific rune. Line and column are one-based.
Example ¶
fmt.Println(New("00100", Point(1, 3)))
Output: 1:3: 1 | 00100 | ^
func Range ¶
Range returns a Region pointing to a range of runes. Line and column are one-based.
Example ¶
fmt.Println(New("01110", Range(1, 2, 1, 4)))
Output: 1:2-4: 1 | 01110 | ^^^
Example (Multiline) ¶
fmt.Println(New("00001\n11110", Range(1, 5, 2, 4)))
Output: 1:5-2:4: 1 | 00001 | ^ 2 | 11110 | ^^^^