Skip to content

Commit

Permalink
add debug method to input to improve debugging ux
Browse files Browse the repository at this point in the history
  • Loading branch information
liamawhite committed Dec 22, 2024
1 parent b44b053 commit a95076b
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/input.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

package core

import (
"strings"
)

// Use unexported type alias so we control the interface and prevent people doing arbirary things e.g. use negative ints.
type checkpoint int

Expand All @@ -22,6 +26,7 @@ type Input interface {
Take(n int) (string, bool)
Checkpoint() checkpoint
Restore(cp checkpoint)
Debug() string
}

type input struct {
Expand Down Expand Up @@ -66,3 +71,12 @@ func (i *input) Checkpoint() checkpoint {
func (i *input) Restore(cp checkpoint) {
i.index = int(cp)
}

// Outputs the full input string with a marker at the current parsing position
func (i *input) Debug() string {
var s strings.Builder
s.WriteString(i.s[:i.index])
s.WriteString("☝️")
s.WriteString(i.s[i.index:])
return s.String()
}

0 comments on commit a95076b

Please sign in to comment.