From cc4187f3ae839cb32507b8d6456a5b68f39e6cf8 Mon Sep 17 00:00:00 2001 From: Syuparn Date: Wed, 17 Feb 2021 09:17:11 +0900 Subject: [PATCH 1/2] fix repl to print multiple lines --- main.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index b83952c..a97197e 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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 { @@ -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 { @@ -140,6 +136,7 @@ func docFunc(funcMap template.FuncMap) func(string) string { } paramTypes := []string{} + // TODO: check varargs for i := 0; i < rt.NumIn(); i++ { paramTypes = append(paramTypes, rt.In(i).String()) } From b9550e7fe2ed164adba14b06985397dc6fcb4106 Mon Sep 17 00:00:00 2001 From: Syuparn Date: Wed, 17 Feb 2021 09:22:22 +0900 Subject: [PATCH 2/2] fix docFunc to show variadic parameters --- main.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index a97197e..602cf1a 100644 --- a/main.go +++ b/main.go @@ -136,11 +136,15 @@ func docFunc(funcMap template.FuncMap) func(string) string { } paramTypes := []string{} - // TODO: check varargs for i := 0; i < rt.NumIn(); i++ { 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())