From a95076b84676e084c5bb52abaecf6084ee33379c Mon Sep 17 00:00:00 2001 From: liam Date: Sun, 22 Dec 2024 11:42:54 -0800 Subject: [PATCH] add debug method to input to improve debugging ux --- core/input.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/input.go b/core/input.go index 7193747..1c47f38 100644 --- a/core/input.go +++ b/core/input.go @@ -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 @@ -22,6 +26,7 @@ type Input interface { Take(n int) (string, bool) Checkpoint() checkpoint Restore(cp checkpoint) + Debug() string } type input struct { @@ -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() +}