From f0fda9b83211977c43d39acdd47d9ca288260b79 Mon Sep 17 00:00:00 2001 From: Syuparn Date: Sat, 20 Feb 2021 22:11:58 +0900 Subject: [PATCH] fix output diff logic --- main.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 3e4f25a..9c06d3a 100644 --- a/main.go +++ b/main.go @@ -74,9 +74,8 @@ func runREPLMode(tmplGen *template.Template) { scanner := bufio.NewScanner(os.Stdin) tmplStr := "" lineNum := 1 - // NOTE: save previous output of template - // to print only added output, which corresponds to the latest input - previousOutStr := "" + // NOTE: save previous output length to print only diff added by the latest input + previousOutLen := 0 for { // show prompt @@ -105,18 +104,15 @@ func runREPLMode(tmplGen *template.Template) { // print only added output, which corresponds to the latest input // NOTE: break line is unneccessary because it already exists outStr := out.String() - fmt.Print(diffStr(outStr, previousOutStr)) + diffStr := outStr[previousOutLen:] + fmt.Print(diffStr) tmplStr = tmplStr + line - previousOutStr = outStr + previousOutLen = len(outStr) lineNum++ } } -func diffStr(newStr, previousStr string) string { - return newStr[len(previousStr):] -} - func newTemplate(leftDelim, rightDelim string) *template.Template { return template.New("tmpl").Delims(leftDelim, rightDelim).Funcs(funcMap()) }