diff --git a/encoding.go b/encoding.go index 6088e65..8903479 100644 --- a/encoding.go +++ b/encoding.go @@ -5,6 +5,7 @@ import ( "log/slog" "path/filepath" "runtime" + "strings" "time" ) @@ -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()) } diff --git a/handler.go b/handler.go index 82ce3ea..1edc314 100644 --- a/handler.go +++ b/handler.go @@ -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