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"
|
||||
)
|
||||
|
||||
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
33
main.go
|
@ -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,12 +98,36 @@ 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 {
|
||||
user, _ := user.Current()
|
||||
|
|
104
shell.go
104
shell.go
|
@ -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)
|
||||
|
||||
|
|
Loading…
Reference in New Issue