Skip to content

Commit

Permalink
Merge pull request #8 from Syuparn/fix-diff-logic
Browse files Browse the repository at this point in the history
fix output diff logic
  • Loading branch information
Syuparn authored Feb 20, 2021
2 parents 1fd5743 + f0fda9b commit 9c18346
Showing 1 changed file with 5 additions and 9 deletions.
14 changes: 5 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
}

0 comments on commit 9c18346

Please sign in to comment.