Skip to content

Commit

Permalink
feat: go to next line if sh input is incomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
TorchedSammy committed Mar 24, 2021
1 parent d3bd479 commit 972c6e4
Showing 1 changed file with 37 additions and 7 deletions.
44 changes: 37 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package main

import (
_ "bufio"
"bufio"
"fmt"
"os"
_ "os/exec"
Expand All @@ -23,7 +23,7 @@ import (

)

const version = "0.1.0"
const version = "0.1.1"
var l *lua.LState
var prompt string
var commands = map[string]bool{}
Expand Down Expand Up @@ -125,12 +125,45 @@ func main() {
default:
err := execCommand(cmdString)
if err != nil {
fmt.Fprintln(os.Stderr, err)
if syntax.IsIncomplete(err) {
sb := &strings.Builder{}
for {
done := StartMultiline(cmdString, sb)
if done {
break
}
}
} else {
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 {
quoted := false
cmdArgs := []string{}
Expand Down Expand Up @@ -161,10 +194,7 @@ func splitInput(input string) []string {
func execCommand(cmd string) error {
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
if err != nil {
if syntax.IsIncomplete(err) {
fmt.Println("incomplete input")
return nil
}
return err
}
runner, _ := interp.New(
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
Expand Down

0 comments on commit 972c6e4

Please sign in to comment.