Compare commits

..

No commits in common. "ef4975f984b739062eb6354adf66c5424f0534dd" and "a30489aa5285ef44782a0a8b8310a5b1da0b7d82" have entirely different histories.

3 changed files with 38 additions and 111 deletions

12
lua.go
View File

@ -10,13 +10,6 @@ import (
"github.com/yuin/gopher-lua" "github.com/yuin/gopher-lua"
) )
var minimalconf = `
ansikit = require 'ansikit'
prompt(ansikit.text(
'{blue}%u {cyan}%d {green}{reset} '
))
`
func LuaInit() { func LuaInit() {
l = lua.NewState() l = lua.NewState()
@ -63,10 +56,7 @@ func LuaInit() {
// Run config // Run config
err = l.DoFile(homedir + "/.hilbishrc.lua") err = l.DoFile(homedir + "/.hilbishrc.lua")
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err, panic(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,7 +7,6 @@ import (
"syscall" "syscall"
"os/signal" "os/signal"
"strings" "strings"
"bufio"
"io" "io"
hooks "hilbish/golibs/bait" hooks "hilbish/golibs/bait"
@ -87,7 +86,7 @@ func main() {
readline.LoadHistory(homedir + "/.hilbish-history") readline.LoadHistory(homedir + "/.hilbish-history")
for { for {
input, err := readline.String(fmtPrompt()) cmdString, err := readline.String(fmtPrompt())
if err == io.EOF { if err == io.EOF {
// Exit if user presses ^D (ctrl + d) // Exit if user presses ^D (ctrl + d)
fmt.Println("") fmt.Println("")
@ -98,35 +97,11 @@ func main() {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
input = strings.TrimSpace(input) // I have no idea if we need this anymore
if len(input) == 0 { continue } 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) // This semi cursed function formats our prompt (obviously)
func fmtPrompt() string { func fmtPrompt() string {

104
shell.go
View File

@ -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"
@ -18,76 +18,27 @@ import (
func RunInput(input string) { func RunInput(input string) {
// First try to run user input in Lua // 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) err := l.DoString(input)
if err == nil { if err == nil {
// If it succeeds, add to history and prompt again // If it succeeds, add to history and prompt again
HandleHistory(input) //readline.AddHistory(input)
//readline.SaveHistory(homedir + "/.hilbish-history")
bait.Em.Emit("command.exit", 0) bait.Em.Emit("command.exit", nil)
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
@ -154,13 +105,24 @@ 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) { func ContinuePrompt(prev string) string {
readline.AddHistory(cmd) fmt.Printf("... ")
readline.SaveHistory(homedir + "/.hilbish-history")
// TODO: load history again (history shared between sessions like this ye) 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 { func StartMultiline(prev string, sb *strings.Builder) bool {
@ -168,7 +130,7 @@ func StartMultiline(prev string, sb *strings.Builder) bool {
// save input from previous prompts // save input from previous prompts
if sb.String() == "" { sb.WriteString(prev + "\n") } if sb.String() == "" { sb.WriteString(prev + "\n") }
fmt.Printf("sh> ") fmt.Printf("... ")
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)