Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"log/slog"
"path/filepath"
"runtime"
"strings"
"time"
)

Expand Down Expand Up @@ -83,9 +84,25 @@ func (e encoder) writeSource(buf *buffer, pc uintptr, cwd string) {
}
}
e.withColor(buf, e.opts.Theme.Source(), func() {
buf.AppendString(frame.File)
buf.AppendByte(':')
buf.AppendInt(int64(frame.Line))
if e.opts.SourceLength > 0 {
source := fmt.Sprintf("%s:%d", frame.File, frame.Line)
if len(source) > e.opts.SourceLength {
charsThatFit := e.opts.SourceLength - 3
frontChars := source[:charsThatFit/2]
if charsThatFit%2 == 1 {
charsThatFit = charsThatFit + 1
}
rearChars := source[len(source)-charsThatFit/2:]
source = frontChars + "..." + rearChars
} else {
source = source + strings.Repeat(" ", e.opts.SourceLength-len(source))
}
buf.AppendString(source)
} else {
buf.AppendString(frame.File)
buf.AppendByte(':')
buf.AppendInt(int64(frame.Line))
}
})
e.writeColoredString(buf, " > ", e.opts.Theme.AttrKey())
}
Expand Down
5 changes: 5 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ type HandlerOptions struct {
// Disable colorized output
NoColor bool

// Enforce a fixed length for the source code field. If set, source code references longer than the field value
// will be trimmed but retain their starting & ending characters. If the source code is shorter than the set value,
// it is padded with spaces.
SourceLength int

// TimeFormat is the format used for time.DateTime
TimeFormat string

Expand Down