fix: move input checks to main.go, some cleanup

pull/24/head
TorchedSammy 2021-04-04 18:42:56 -04:00
parent a30489aa52
commit cc183620c5
2 changed files with 28 additions and 29 deletions

29
main.go
View File

@ -7,6 +7,7 @@ import (
"syscall"
"os/signal"
"strings"
"bufio"
"io"
hooks "hilbish/golibs/bait"
@ -86,7 +87,7 @@ func main() {
readline.LoadHistory(homedir + "/.hilbish-history")
for {
cmdString, err := readline.String(fmtPrompt())
input, err := readline.String(fmtPrompt())
if err == io.EOF {
// Exit if user presses ^D (ctrl + d)
fmt.Println("")
@ -98,10 +99,32 @@ func main() {
}
// I have no idea if we need this anymore
cmdString = strings.TrimSuffix(cmdString, "\n")
RunInput(cmdString)
if strings.HasSuffix(input, "\\") {
for {
input, err = ContinuePrompt(strings.TrimSuffix(input, "\\"))
if err != nil || !strings.HasSuffix(input, "\\") { break }
}
}
RunInput(input)
}
}
func ContinuePrompt(prev string) (string, error) {
fmt.Printf("> ")
reader := bufio.NewReader(os.Stdin)
// TODO: use readline here?
cont, err := reader.ReadString('\n')
if err != nil {
fmt.Println("")
return "", err
}
cont = strings.TrimSpace(cont)
return prev + "\n" + strings.TrimSuffix(cont, "\n"), nil
}
// This semi cursed function formats our prompt (obviously)
func fmtPrompt() string {

View File

@ -18,23 +18,12 @@ import (
func RunInput(input string) {
// First try to run user input in Lua
if strings.HasSuffix(input, "\\") {
for {
input = strings.TrimSuffix(input, "\\")
input = ContinuePrompt(input)
input = strings.TrimSpace(input)
if input == "" { break }
// For some reason !HasSuffix didnt work :\, stupid
if !strings.HasSuffix(input, "\\") { break }
}
}
err := l.DoString(input)
if err == nil {
// If it succeeds, add to history and prompt again
//readline.AddHistory(input)
//readline.SaveHistory(homedir + "/.hilbish-history")
readline.AddHistory(input)
readline.SaveHistory(homedir + "/.hilbish-history")
bait.Em.Emit("command.exit", nil)
bait.Em.Emit("command.success", nil)
return
@ -110,20 +99,7 @@ func splitInput(input string) ([]string, string) {
return cmdArgs, cmdstr.String()
}
func ContinuePrompt(prev string) string {
fmt.Printf("... ")
reader := bufio.NewReader(os.Stdin)
cont, err := reader.ReadString('\n')
if err == io.EOF {
// Exit when ^D
fmt.Println("")
return ""
}
return prev + "\n" + cont
}
func StartMultiline(prev string, sb *strings.Builder) bool {
// sb fromt outside is passed so we can