mirror of https://github.com/Hilbis/Hilbish
Compare commits
2 Commits
6c050194ed
...
a30489aa52
Author | SHA1 | Date |
---|---|---|
TorchedSammy | a30489aa52 | |
TorchedSammy | f4b45b370e |
16
README.md
16
README.md
|
@ -12,8 +12,20 @@ Prebuilt binaries are not yet provided, so to try it out you'll have to manually
|
||||||
|
|
||||||
**NOTE:** Hilbish is currently only officially supported and tested on Linux
|
**NOTE:** Hilbish is currently only officially supported and tested on Linux
|
||||||
|
|
||||||
### Requirements
|
### Prerequisites
|
||||||
- Go 1.16
|
- [Go 1.16](https://go.dev)
|
||||||
|
|
||||||
|
- GNU Readline
|
||||||
|
|
||||||
|
On Fedora, readline can be installed with:
|
||||||
|
```
|
||||||
|
sudo dnf install readline-devel
|
||||||
|
```
|
||||||
|
|
||||||
|
On Debian/Ubuntu and distros based on them, it can be installed with:
|
||||||
|
```
|
||||||
|
sudo apt install libreadline-dev
|
||||||
|
```
|
||||||
|
|
||||||
### Install
|
### Install
|
||||||
```sh
|
```sh
|
||||||
|
|
97
shell.go
97
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,79 +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
|
||||||
readline.AddHistory(input)
|
//readline.AddHistory(input)
|
||||||
readline.SaveHistory(homedir + "/.hilbish-history")
|
//readline.SaveHistory(homedir + "/.hilbish-history")
|
||||||
bait.Em.Emit("command.exit", nil)
|
bait.Em.Emit("command.exit", nil)
|
||||||
bait.Em.Emit("command.success", nil)
|
bait.Em.Emit("command.success", nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split up the input
|
|
||||||
cmdArgs, cmdString := splitInput(input)
|
|
||||||
// If there's actually no input, prompt again
|
|
||||||
if len(cmdArgs) == 0 { return }
|
|
||||||
|
|
||||||
// If alias was found, use command alias
|
|
||||||
if aliases[cmdArgs[0]] != "" {
|
|
||||||
cmdString = aliases[cmdArgs[0]] + strings.Trim(cmdString, cmdArgs[0])
|
|
||||||
execCommand(cmdString)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
readline.AddHistory(cmdString)
|
|
||||||
readline.SaveHistory(homedir + "/.hilbish-history")
|
|
||||||
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) {
|
|
||||||
sb := &strings.Builder{}
|
|
||||||
for {
|
|
||||||
done := StartMultiline(cmdString, sb)
|
|
||||||
if done {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if code, ok := interp.IsExitStatus(err); ok {
|
|
||||||
if code > 0 {
|
|
||||||
bait.Em.Emit("command.exit", nil)
|
|
||||||
bait.Em.Emit("command.fail", code)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Fprintln(os.Stderr, err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
bait.Em.Emit("command.exit", nil)
|
|
||||||
bait.Em.Emit("command.success", nil)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run command in sh interpreter
|
// Run command in sh interpreter
|
||||||
|
@ -162,6 +110,21 @@ func splitInput(input string) ([]string, string) {
|
||||||
return cmdArgs, cmdstr.String()
|
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 StartMultiline(prev string, sb *strings.Builder) bool {
|
func StartMultiline(prev string, sb *strings.Builder) bool {
|
||||||
// sb fromt outside is passed so we can
|
// sb fromt outside is passed so we can
|
||||||
// save input from previous prompts
|
// save input from previous prompts
|
||||||
|
|
Loading…
Reference in New Issue