Skip to content

Commit

Permalink
Merge pull request #2 from Syuparn/fix-print-lines
Browse files Browse the repository at this point in the history
Fix print lines
  • Loading branch information
Syuparn authored Feb 17, 2021
2 parents ef78a85 + b9550e7 commit 02ff42e
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ func runREPLMode() {
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 := ""

for {
// show prompt
Expand All @@ -82,25 +85,19 @@ func runREPLMode() {
fmt.Fprintf(os.Stderr, "runtime error:\n%v\n", err)
continue
}
// print only last line, which corresponds to the latest input
// print only added output, which corresponds to the latest input
// NOTE: break line is unneccessary because it already exists
fmt.Print(lastLine(out))
outStr := out.String()
fmt.Print(diffStr(outStr, previousOutStr))

tmplStr = tmplStr + line
previousOutStr = outStr
lineNum++
}
}

func lastLine(out *bytes.Buffer) string {
last := ""

for {
line, err := out.ReadString('\n')
if err != nil {
return last
}
last = line
}
func diffStr(newStr, previousStr string) string {
return newStr[len(previousStr):]
}

func funcMap() template.FuncMap {
Expand All @@ -127,7 +124,6 @@ func searchFunc(funcMap template.FuncMap) func(string) []string {
}

func docFunc(funcMap template.FuncMap) func(string) string {
// TODO: impl
return func(name string) string {
f, ok := funcMap[name]
if !ok {
Expand All @@ -144,6 +140,11 @@ func docFunc(funcMap template.FuncMap) func(string) string {
paramTypes = append(paramTypes, rt.In(i).String())
}

// add `...` to variadic parameter
if rt.IsVariadic() {
paramTypes[len(paramTypes)-1] = "..." + paramTypes[len(paramTypes)-1]
}

returnTypes := []string{}
for i := 0; i < rt.NumOut(); i++ {
returnTypes = append(returnTypes, rt.Out(i).String())
Expand Down

0 comments on commit 02ff42e

Please sign in to comment.