Compare commits

..

5 Commits

Author SHA1 Message Date
TorchedSammy ef4975f984 feat: fallback to default config on error loading user conf 2021-04-04 20:31:32 -04:00
TorchedSammy 3cb3b34023 fix: add back sh, cleanup a bit and use new functions 2021-04-04 20:30:47 -04:00
TorchedSammy 22270dc537 fix: trim input and remove comment 2021-04-04 20:30:03 -04:00
TorchedSammy cfdab00684 fix: make shell continue prompt "sh> " 2021-04-04 18:46:37 -04:00
TorchedSammy cc183620c5 fix: move input checks to main.go, some cleanup 2021-04-04 18:42:56 -04:00
3 changed files with 111 additions and 38 deletions

12
lua.go
View File

@ -10,6 +10,13 @@ import (
"github.com/yuin/gopher-lua"
)
var minimalconf = `
ansikit = require 'ansikit'
prompt(ansikit.text(
'{blue}%u {cyan}%d {green}{reset} '
))
`
func LuaInit() {
l = lua.NewState()
@ -56,7 +63,10 @@ func LuaInit() {
// Run config
err = l.DoFile(homedir + "/.hilbishrc.lua")
if err != nil {
panic(err)
fmt.Fprintln(os.Stderr, err,
"An error has occured while loading your config! Falling back to minimal default config.\n")
l.DoString(minimalconf)
}
}

33
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("")
@ -97,11 +98,35 @@ func main() {
fmt.Fprintln(os.Stderr, err)
}
// I have no idea if we need this anymore
cmdString = strings.TrimSuffix(cmdString, "\n")
RunInput(cmdString)
input = strings.TrimSpace(input)
if len(input) == 0 { continue }
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 {

104
shell.go
View File

@ -9,8 +9,8 @@ import (
"strings"
"github.com/bobappleyard/readline"
_ "github.com/yuin/gopher-lua"
_ "layeh.com/gopher-luar"
"github.com/yuin/gopher-lua"
"layeh.com/gopher-luar"
"mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax"
@ -18,27 +18,76 @@ 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")
bait.Em.Emit("command.exit", nil)
bait.Em.Emit("command.success", nil)
HandleHistory(input)
bait.Em.Emit("command.exit", 0)
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
@ -105,24 +154,13 @@ func splitInput(input string) ([]string, string) {
cmdArgs = append(cmdArgs, sb.String())
}
readline.AddHistory(input)
readline.SaveHistory(homedir + "/.hilbish-history")
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 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 {
@ -130,7 +168,7 @@ func StartMultiline(prev string, sb *strings.Builder) bool {
// save input from previous prompts
if sb.String() == "" { sb.WriteString(prev + "\n") }
fmt.Printf("... ")
fmt.Printf("sh> ")
reader := bufio.NewReader(os.Stdin)