mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "ef4975f984b739062eb6354adf66c5424f0534dd" and "a30489aa5285ef44782a0a8b8310a5b1da0b7d82" have entirely different histories.
ef4975f984
...
a30489aa52
12
lua.go
12
lua.go
|
@ -10,13 +10,6 @@ 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()
|
||||
|
||||
|
@ -63,10 +56,7 @@ func LuaInit() {
|
|||
// Run config
|
||||
err = l.DoFile(homedir + "/.hilbishrc.lua")
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err,
|
||||
"An error has occured while loading your config! Falling back to minimal default config.\n")
|
||||
|
||||
l.DoString(minimalconf)
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
33
main.go
33
main.go
|
@ -7,7 +7,6 @@ import (
|
|||
"syscall"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"bufio"
|
||||
"io"
|
||||
hooks "hilbish/golibs/bait"
|
||||
|
||||
|
@ -87,7 +86,7 @@ func main() {
|
|||
readline.LoadHistory(homedir + "/.hilbish-history")
|
||||
|
||||
for {
|
||||
input, err := readline.String(fmtPrompt())
|
||||
cmdString, err := readline.String(fmtPrompt())
|
||||
if err == io.EOF {
|
||||
// Exit if user presses ^D (ctrl + d)
|
||||
fmt.Println("")
|
||||
|
@ -98,36 +97,12 @@ func main() {
|
|||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
|
||||
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)
|
||||
// I have no idea if we need this anymore
|
||||
cmdString = strings.TrimSuffix(cmdString, "\n")
|
||||
RunInput(cmdString)
|
||||
}
|
||||
}
|
||||
|
||||
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,76 +18,27 @@ 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
|
||||
HandleHistory(input)
|
||||
|
||||
bait.Em.Emit("command.exit", 0)
|
||||
//readline.AddHistory(input)
|
||||
//readline.SaveHistory(homedir + "/.hilbish-history")
|
||||
bait.Em.Emit("command.exit", nil)
|
||||
bait.Em.Emit("command.success", nil)
|
||||
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
|
||||
|
@ -154,13 +105,24 @@ func splitInput(input string) ([]string, string) {
|
|||
cmdArgs = append(cmdArgs, sb.String())
|
||||
}
|
||||
|
||||
readline.AddHistory(input)
|
||||
readline.SaveHistory(homedir + "/.hilbish-history")
|
||||
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 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 StartMultiline(prev string, sb *strings.Builder) bool {
|
||||
|
@ -168,7 +130,7 @@ func StartMultiline(prev string, sb *strings.Builder) bool {
|
|||
// save input from previous prompts
|
||||
if sb.String() == "" { sb.WriteString(prev + "\n") }
|
||||
|
||||
fmt.Printf("sh> ")
|
||||
fmt.Printf("... ")
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
|
||||
|
|
Loading…
Reference in New Issue