mirror of https://github.com/Hilbis/Hilbish
fix: add back sh, cleanup a bit and use new functions
parent
22270dc537
commit
3cb3b34023
80
shell.go
80
shell.go
|
@ -9,8 +9,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/bobappleyard/readline"
|
"github.com/bobappleyard/readline"
|
||||||
_ "github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
||||||
_ "layeh.com/gopher-luar"
|
"layeh.com/gopher-luar"
|
||||||
"mvdan.cc/sh/v3/interp"
|
"mvdan.cc/sh/v3/interp"
|
||||||
"mvdan.cc/sh/v3/syntax"
|
"mvdan.cc/sh/v3/syntax"
|
||||||
|
|
||||||
|
@ -22,12 +22,72 @@ func RunInput(input string) {
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
// If it succeeds, add to history and prompt again
|
// If it succeeds, add to history and prompt again
|
||||||
readline.AddHistory(input)
|
HandleHistory(input)
|
||||||
readline.SaveHistory(homedir + "/.hilbish-history")
|
|
||||||
bait.Em.Emit("command.exit", nil)
|
bait.Em.Emit("command.exit", 0)
|
||||||
bait.Em.Emit("command.success", nil)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cmdArgs, cmdString := splitInput(input)
|
||||||
|
|
||||||
|
// If alias was found, use command alias
|
||||||
|
if aliases[cmdArgs[0]] != "" {
|
||||||
|
cmdString = aliases[cmdArgs[0]] + strings.Trim(cmdString, cmdArgs[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// If command is defined in Lua then run it
|
||||||
|
if commands[cmdArgs[0]] {
|
||||||
|
err := l.CallByParam(lua.P{
|
||||||
|
Fn: l.GetField(
|
||||||
|
l.GetTable(
|
||||||
|
l.GetGlobal("commanding"),
|
||||||
|
lua.LString("__commands")),
|
||||||
|
cmdArgs[0]),
|
||||||
|
NRet: 0,
|
||||||
|
Protect: true,
|
||||||
|
}, luar.New(l, cmdArgs[1:]))
|
||||||
|
if err != nil {
|
||||||
|
// TODO: dont panic
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
HandleHistory(cmdString)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Last option: use sh interpreter
|
||||||
|
switch cmdArgs[0] {
|
||||||
|
case "exit":
|
||||||
|
os.Exit(0)
|
||||||
|
default:
|
||||||
|
err := execCommand(cmdString)
|
||||||
|
if err != nil {
|
||||||
|
// If input is incomplete, start multiline prompting
|
||||||
|
if syntax.IsIncomplete(err) {
|
||||||
|
for {
|
||||||
|
cmdString, err = ContinuePrompt(strings.TrimSuffix(cmdString, "\\"))
|
||||||
|
if err != nil { break }
|
||||||
|
err = execCommand(cmdString)
|
||||||
|
|
||||||
|
if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") {
|
||||||
|
continue
|
||||||
|
} else if code, ok := interp.IsExitStatus(err); ok {
|
||||||
|
bait.Em.Emit("command.exit", code)
|
||||||
|
} else if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
bait.Em.Emit("command.exit", 1)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if code, ok := interp.IsExitStatus(err); ok {
|
||||||
|
bait.Em.Emit("command.exit", code)
|
||||||
|
} else { fmt.Fprintln(os.Stderr, err) }
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bait.Em.Emit("command.exit", 0)
|
||||||
|
}
|
||||||
|
HandleHistory(cmdString)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run command in sh interpreter
|
// Run command in sh interpreter
|
||||||
|
@ -94,12 +154,14 @@ func splitInput(input string) ([]string, string) {
|
||||||
cmdArgs = append(cmdArgs, sb.String())
|
cmdArgs = append(cmdArgs, sb.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
readline.AddHistory(input)
|
|
||||||
readline.SaveHistory(homedir + "/.hilbish-history")
|
|
||||||
return cmdArgs, cmdstr.String()
|
return cmdArgs, cmdstr.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func HandleHistory(cmd string) {
|
||||||
|
readline.AddHistory(cmd)
|
||||||
|
readline.SaveHistory(homedir + "/.hilbish-history")
|
||||||
|
// TODO: load history again (history shared between sessions like this ye)
|
||||||
|
}
|
||||||
|
|
||||||
func StartMultiline(prev string, sb *strings.Builder) bool {
|
func StartMultiline(prev string, sb *strings.Builder) bool {
|
||||||
// sb fromt outside is passed so we can
|
// sb fromt outside is passed so we can
|
||||||
|
|
Loading…
Reference in New Issue