feat: go to next line if sh input is incomplete

pull/9/head v0.1.1
TorchedSammy 2021-03-24 19:18:30 -04:00
parent d3bd47963d
commit 972c6e44b3
1 changed files with 37 additions and 7 deletions

42
main.go
View File

@ -1,7 +1,7 @@
package main package main
import ( import (
_ "bufio" "bufio"
"fmt" "fmt"
"os" "os"
_ "os/exec" _ "os/exec"
@ -23,7 +23,7 @@ import (
) )
const version = "0.1.0" const version = "0.1.1"
var l *lua.LState var l *lua.LState
var prompt string var prompt string
var commands = map[string]bool{} var commands = map[string]bool{}
@ -125,10 +125,43 @@ func main() {
default: default:
err := execCommand(cmdString) err := execCommand(cmdString)
if err != nil { if err != nil {
if syntax.IsIncomplete(err) {
sb := &strings.Builder{}
for {
done := StartMultiline(cmdString, sb)
if done {
break
}
}
} else {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
} }
} }
}
}
func StartMultiline(prev string, sb *strings.Builder) bool {
if sb.String() == "" { sb.WriteString(prev + "\n") }
fmt.Printf("... ")
reader := bufio.NewReader(os.Stdin)
cont, err := reader.ReadString('\n')
if err == io.EOF {
fmt.Println("")
return true
}
sb.WriteString(cont)
err = execCommand(sb.String())
if err != nil && syntax.IsIncomplete(err) {
return false
}
return true
} }
func splitInput(input string) []string { func splitInput(input string) []string {
@ -161,10 +194,7 @@ func splitInput(input string) []string {
func execCommand(cmd string) error { func execCommand(cmd string) error {
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "") file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
if err != nil { if err != nil {
if syntax.IsIncomplete(err) { return err
fmt.Println("incomplete input")
return nil
}
} }
runner, _ := interp.New( runner, _ := interp.New(
interp.StdIO(os.Stdin, os.Stdout, os.Stderr), interp.StdIO(os.Stdin, os.Stdout, os.Stderr),