77 "fmt"
88 "io/ioutil"
99 "os"
10+ "strings"
1011 "text/template"
1112)
1213
@@ -74,8 +75,6 @@ func runREPLMode(tmplGen *template.Template) {
7475 scanner := bufio .NewScanner (os .Stdin )
7576 tmplStr := ""
7677 lineNum := 1
77- // NOTE: save previous output length to print only diff added by the latest input
78- previousOutLen := 0
7978
8079 for {
8180 // show prompt
@@ -89,7 +88,9 @@ func runREPLMode(tmplGen *template.Template) {
8988 line := scanner .Text () + "\n "
9089
9190 // NOTE: whole history is necessary to refer previous variable statement
92- tmpl , err := tmplGen .Parse (tmplStr + line )
91+ // HACK: insert "\034"(, which is not printable) to detect
92+ // output generated by the latest input line
93+ tmpl , err := tmplGen .Parse (tmplStr + "\034 " + line )
9394 if err != nil {
9495 fmt .Fprintf (os .Stderr , "template error:\n %v\n " , err )
9596 continue
@@ -102,13 +103,17 @@ func runREPLMode(tmplGen *template.Template) {
102103 continue
103104 }
104105 // print only added output, which corresponds to the latest input
105- // NOTE: break line is unneccessary because it already exists
106- outStr := out .String ()
107- diffStr := outStr [previousOutLen :]
108- fmt .Print (diffStr )
106+ splitted := strings .Split (out .String (), "\034 " )
107+ if len (splitted ) != 2 {
108+ fmt .Fprintf (os .Stderr ,
109+ "runtime error:\n there must be 1 internal delimiter \\ 034 (found %d)\n " , len (splitted )- 1 )
110+ continue
111+ }
112+
113+ diffStr := splitted [1 ]
114+ fmt .Println (diffStr )
109115
110116 tmplStr = tmplStr + line
111- previousOutLen = len (outStr )
112117 lineNum ++
113118 }
114119}
0 commit comments