mirror of https://github.com/Hilbis/Hilbish
Compare commits
5 Commits
a30489aa52
...
ef4975f984
Author | SHA1 | Date |
---|---|---|
TorchedSammy | ef4975f984 | |
TorchedSammy | 3cb3b34023 | |
TorchedSammy | 22270dc537 | |
TorchedSammy | cfdab00684 | |
TorchedSammy | cc183620c5 |
12
lua.go
12
lua.go
|
@ -10,6 +10,13 @@ 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()
|
||||||
|
|
||||||
|
@ -56,7 +63,10 @@ func LuaInit() {
|
||||||
// Run config
|
// Run config
|
||||||
err = l.DoFile(homedir + "/.hilbishrc.lua")
|
err = l.DoFile(homedir + "/.hilbishrc.lua")
|
||||||
if err != nil {
|
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
33
main.go
|
@ -7,6 +7,7 @@ import (
|
||||||
"syscall"
|
"syscall"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"strings"
|
"strings"
|
||||||
|
"bufio"
|
||||||
"io"
|
"io"
|
||||||
hooks "hilbish/golibs/bait"
|
hooks "hilbish/golibs/bait"
|
||||||
|
|
||||||
|
@ -86,7 +87,7 @@ func main() {
|
||||||
readline.LoadHistory(homedir + "/.hilbish-history")
|
readline.LoadHistory(homedir + "/.hilbish-history")
|
||||||
|
|
||||||
for {
|
for {
|
||||||
cmdString, err := readline.String(fmtPrompt())
|
input, 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("")
|
||||||
|
@ -97,11 +98,35 @@ func main() {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// I have no idea if we need this anymore
|
input = strings.TrimSpace(input)
|
||||||
cmdString = strings.TrimSuffix(cmdString, "\n")
|
if len(input) == 0 { continue }
|
||||||
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
104
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"
|
||||||
|
|
||||||
|
@ -18,27 +18,76 @@ 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
|
||||||
//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
|
||||||
|
@ -105,24 +154,13 @@ 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 ContinuePrompt(prev string) string {
|
func HandleHistory(cmd string) {
|
||||||
fmt.Printf("... ")
|
readline.AddHistory(cmd)
|
||||||
|
readline.SaveHistory(homedir + "/.hilbish-history")
|
||||||
reader := bufio.NewReader(os.Stdin)
|
// TODO: load history again (history shared between sessions like this ye)
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -130,7 +168,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("... ")
|
fmt.Printf("sh> ")
|
||||||
|
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue