mirror of https://github.com/Hilbis/Hilbish
Compare commits
5 Commits
7b0b06d9bf
...
17e12c8c2a
Author | SHA1 | Date |
---|---|---|
sammy | 17e12c8c2a | |
sammy | bdc8c770e6 | |
sammy | 08c56a54ca | |
sammy | 428db24831 | |
Devin Singh | 94f0ccf9f6 |
8
lua.go
8
lua.go
|
@ -2,10 +2,10 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
lfs "hilbish/golibs/fs"
|
|
||||||
cmds "hilbish/golibs/commander"
|
|
||||||
hooks "hilbish/golibs/bait"
|
hooks "hilbish/golibs/bait"
|
||||||
|
cmds "hilbish/golibs/commander"
|
||||||
|
lfs "hilbish/golibs/fs"
|
||||||
|
"os"
|
||||||
|
|
||||||
"github.com/yuin/gopher-lua"
|
"github.com/yuin/gopher-lua"
|
||||||
)
|
)
|
||||||
|
@ -34,7 +34,7 @@ func LuaInit(confpath string) {
|
||||||
commander := cmds.New()
|
commander := cmds.New()
|
||||||
// When a command from Lua is added, register it for use
|
// When a command from Lua is added, register it for use
|
||||||
commander.Events.On("commandRegister",
|
commander.Events.On("commandRegister",
|
||||||
func (cmdName string, cmd *lua.LFunction) {
|
func(cmdName string, cmd *lua.LFunction) {
|
||||||
commands[cmdName] = true
|
commands[cmdName] = true
|
||||||
l.SetField(
|
l.SetField(
|
||||||
l.GetTable(l.GetGlobal("commanding"),
|
l.GetTable(l.GetGlobal("commanding"),
|
||||||
|
|
51
main.go
51
main.go
|
@ -2,11 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"os/user"
|
|
||||||
"os/signal"
|
|
||||||
"strings"
|
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"os/user"
|
||||||
|
"strings"
|
||||||
|
|
||||||
hooks "hilbish/golibs/bait"
|
hooks "hilbish/golibs/bait"
|
||||||
|
|
||||||
"github.com/akamensky/argparse"
|
"github.com/akamensky/argparse"
|
||||||
|
@ -16,18 +17,21 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const version = "0.4.0-dev.2+hilbiline"
|
const version = "0.4.0-dev.2+hilbiline"
|
||||||
var l *lua.LState
|
|
||||||
// User's prompt, this will get set when lua side is initialized
|
|
||||||
var prompt string
|
|
||||||
var multilinePrompt = "> "
|
|
||||||
|
|
||||||
// Map of builtin/custom commands defined in the commander lua module
|
var (
|
||||||
var commands = map[string]bool{}
|
l *lua.LState
|
||||||
// Command aliases
|
|
||||||
var aliases = map[string]string{}
|
// User's prompt, this will get set when lua side is initialized
|
||||||
var bait hooks.Bait
|
prompt string
|
||||||
var homedir string
|
multilinePrompt = "> "
|
||||||
var running bool
|
|
||||||
|
commands = map[string]bool{}
|
||||||
|
aliases = map[string]string{}
|
||||||
|
|
||||||
|
bait hooks.Bait
|
||||||
|
homedir string
|
||||||
|
running bool
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
homedir, _ = os.UserHomeDir()
|
homedir, _ = os.UserHomeDir()
|
||||||
|
@ -69,7 +73,9 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set $SHELL if the user wants to
|
// Set $SHELL if the user wants to
|
||||||
if *setshflag { os.Setenv("SHELL", os.Args[0]) }
|
if *setshflag {
|
||||||
|
os.Setenv("SHELL", os.Args[0])
|
||||||
|
}
|
||||||
|
|
||||||
// If user's config doesn't exixt,
|
// If user's config doesn't exixt,
|
||||||
if _, err := os.Stat(defaultconfpath); os.IsNotExist(err) {
|
if _, err := os.Stat(defaultconfpath); os.IsNotExist(err) {
|
||||||
|
@ -86,7 +92,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create it using either default config we found
|
// Create it using either default config we found
|
||||||
err = os.WriteFile(homedir + "/.hilbishrc.lua", input, 0644)
|
err = os.WriteFile(homedir+"/.hilbishrc.lua", input, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If that fails, bail
|
// If that fails, bail
|
||||||
fmt.Println("Error creating config file")
|
fmt.Println("Error creating config file")
|
||||||
|
@ -125,15 +131,19 @@ func main() {
|
||||||
for {
|
for {
|
||||||
input, err = ContinuePrompt(strings.TrimSuffix(input, "\\"))
|
input, err = ContinuePrompt(strings.TrimSuffix(input, "\\"))
|
||||||
|
|
||||||
if err != nil || !strings.HasSuffix(input, "\\") { break }
|
if err != nil || !strings.HasSuffix(input, "\\") {
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
running = true
|
running = true
|
||||||
RunInput(input)
|
RunInput(input)
|
||||||
|
|
||||||
termwidth, _, err := term.GetSize(0)
|
termwidth, _, err := term.GetSize(0)
|
||||||
if err != nil { continue }
|
if err != nil {
|
||||||
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth - 1) + "\r")
|
continue
|
||||||
|
}
|
||||||
|
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth-1) + "\r")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -188,4 +198,3 @@ func HandleSignals() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
23
shell.go
23
shell.go
|
@ -1,18 +1,17 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"bufio"
|
"bufio"
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"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"
|
||||||
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func RunInput(input string) {
|
func RunInput(input string) {
|
||||||
|
@ -31,7 +30,9 @@ func RunInput(input string) {
|
||||||
|
|
||||||
// If alias was found, use command alias
|
// If alias was found, use command alias
|
||||||
if aliases[cmdArgs[0]] != "" {
|
if aliases[cmdArgs[0]] != "" {
|
||||||
cmdString = aliases[cmdArgs[0]] + strings.Trim(cmdString, cmdArgs[0])
|
alias := aliases[cmdArgs[0]]
|
||||||
|
cmdString = alias + strings.Trim(cmdString, cmdArgs[0])
|
||||||
|
cmdArgs[0] = alias
|
||||||
}
|
}
|
||||||
|
|
||||||
// If command is defined in Lua then run it
|
// If command is defined in Lua then run it
|
||||||
|
@ -47,9 +48,11 @@ func RunInput(input string) {
|
||||||
}, luar.New(l, cmdArgs[1:]))
|
}, luar.New(l, cmdArgs[1:]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr,
|
fmt.Fprintln(os.Stderr,
|
||||||
"Error in command:\n\n" + err.Error())
|
"Error in command:\n\n"+err.Error())
|
||||||
|
}
|
||||||
|
if cmdArgs[0] != "exit" {
|
||||||
|
HandleHistory(cmdString)
|
||||||
}
|
}
|
||||||
if cmdArgs[0] != "exit" { HandleHistory(cmdString) }
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +63,9 @@ func RunInput(input string) {
|
||||||
if syntax.IsIncomplete(err) {
|
if syntax.IsIncomplete(err) {
|
||||||
for {
|
for {
|
||||||
cmdString, err = ContinuePrompt(strings.TrimSuffix(cmdString, "\\"))
|
cmdString, err = ContinuePrompt(strings.TrimSuffix(cmdString, "\\"))
|
||||||
if err != nil { break }
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
err = execCommand(cmdString)
|
err = execCommand(cmdString)
|
||||||
if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") {
|
if syntax.IsIncomplete(err) || strings.HasSuffix(input, "\\") {
|
||||||
continue
|
continue
|
||||||
|
@ -75,7 +80,9 @@ func RunInput(input string) {
|
||||||
} else {
|
} else {
|
||||||
if code, ok := interp.IsExitStatus(err); ok {
|
if code, ok := interp.IsExitStatus(err); ok {
|
||||||
bait.Em.Emit("command.exit", code)
|
bait.Em.Emit("command.exit", code)
|
||||||
} else { fmt.Fprintln(os.Stderr, err) }
|
} else {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
bait.Em.Emit("command.exit", 0)
|
bait.Em.Emit("command.exit", 0)
|
||||||
|
|
Loading…
Reference in New Issue