mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "a4dbfbf4bbe8a288139861b8af80ab42a45f0f21" and "b3a28b067c2972f61bce669e917762f9c24774f5" have entirely different histories.
a4dbfbf4bb
...
b3a28b067c
|
@ -25,7 +25,6 @@ func HilbishLoader(L *lua.LState) int {
|
|||
L.SetField(mod, "ver", lua.LString(version))
|
||||
L.SetField(mod, "user", lua.LString(curuser.Username))
|
||||
L.SetField(mod, "host", lua.LString(host))
|
||||
L.SetField(mod, "home", lua.LString(homedir))
|
||||
|
||||
L.Push(mod)
|
||||
|
||||
|
|
52
lua.go
52
lua.go
|
@ -6,7 +6,6 @@ import (
|
|||
"os/exec"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"hilbish/golibs/bait"
|
||||
"hilbish/golibs/commander"
|
||||
|
@ -33,8 +32,6 @@ func LuaInit() {
|
|||
l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
|
||||
l.SetGlobal("exec", l.NewFunction(hshexec))
|
||||
l.SetGlobal("goro", luar.New(l, hshgoroutine))
|
||||
l.SetGlobal("timeout", luar.New(l, hshtimeout))
|
||||
l.SetGlobal("interval", l.NewFunction(hshinterval))
|
||||
|
||||
// yes this is stupid, i know
|
||||
l.PreloadModule("hilbish", HilbishLoader)
|
||||
|
@ -45,9 +42,16 @@ func LuaInit() {
|
|||
|
||||
cmds := commander.New()
|
||||
// When a command from Lua is added, register it for use
|
||||
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
|
||||
commands[cmdName] = cmd
|
||||
})
|
||||
// TODO: maybe dont add command code to a lua table? insstead use a map
|
||||
cmds.Events.On("commandRegister",
|
||||
func(cmdName string, cmd *lua.LFunction) {
|
||||
commands[cmdName] = true
|
||||
l.SetField(
|
||||
l.GetTable(l.GetGlobal("commanding"),
|
||||
lua.LString("__commands")),
|
||||
cmdName,
|
||||
cmd)
|
||||
})
|
||||
|
||||
l.PreloadModule("commander", cmds.Loader)
|
||||
|
||||
|
@ -147,39 +151,3 @@ func hshexec(L *lua.LState) int {
|
|||
func hshgoroutine(gofunc func()) {
|
||||
go gofunc()
|
||||
}
|
||||
|
||||
func hshtimeout(timeoutfunc func(), ms int) {
|
||||
timeout := time.Duration(ms) * time.Millisecond
|
||||
time.AfterFunc(timeout, timeoutfunc)
|
||||
}
|
||||
|
||||
func hshinterval(L *lua.LState) int {
|
||||
intervalfunc := L.CheckFunction(1)
|
||||
ms := L.CheckInt(2)
|
||||
interval := time.Duration(ms) * time.Millisecond
|
||||
|
||||
ticker := time.NewTicker(interval)
|
||||
stop := make(chan lua.LValue)
|
||||
|
||||
go func() {
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
if err := L.CallByParam(lua.P{
|
||||
Fn: intervalfunc,
|
||||
NRet: 0,
|
||||
Protect: true,
|
||||
}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
case <-stop:
|
||||
ticker.Stop()
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
L.Push(lua.LChannel(stop))
|
||||
return 1
|
||||
}
|
||||
|
||||
|
|
9
main.go
9
main.go
|
@ -22,7 +22,7 @@ var (
|
|||
l *lua.LState
|
||||
lr *LineReader
|
||||
|
||||
commands = map[string]*lua.LFunction{}
|
||||
commands = map[string]bool{}
|
||||
aliases = map[string]string{}
|
||||
|
||||
homedir string
|
||||
|
@ -140,7 +140,6 @@ func main() {
|
|||
os.Exit(0)
|
||||
}
|
||||
|
||||
input:
|
||||
for interactive {
|
||||
running = false
|
||||
|
||||
|
@ -163,10 +162,8 @@ input:
|
|||
if strings.HasSuffix(input, "\\") {
|
||||
for {
|
||||
input, err = ContinuePrompt(strings.TrimSuffix(input, "\\"))
|
||||
if err != nil {
|
||||
goto input // continue inside nested loop
|
||||
}
|
||||
if !strings.HasSuffix(input, "\\") {
|
||||
|
||||
if err != nil || !strings.HasSuffix(input, "\\") {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
|
16
shell.go
16
shell.go
|
@ -49,9 +49,13 @@ func RunInput(input string) {
|
|||
hooks.Em.Emit("command.exit", 0)
|
||||
return
|
||||
}
|
||||
if commands[cmdArgs[0]] != nil {
|
||||
if commands[cmdArgs[0]] {
|
||||
err := l.CallByParam(lua.P{
|
||||
Fn: commands[cmdArgs[0]],
|
||||
Fn: l.GetField(
|
||||
l.GetTable(
|
||||
l.GetGlobal("commanding"),
|
||||
lua.LString("__commands")),
|
||||
cmdArgs[0]),
|
||||
NRet: 1,
|
||||
Protect: true,
|
||||
}, luar.New(l, cmdArgs[1:]))
|
||||
|
@ -124,9 +128,13 @@ func execCommand(cmd string) error {
|
|||
}
|
||||
|
||||
// If command is defined in Lua then run it
|
||||
if commands[args[0]] != nil {
|
||||
if commands[args[0]] {
|
||||
err := l.CallByParam(lua.P{
|
||||
Fn: commands[args[0]],
|
||||
Fn: l.GetField(
|
||||
l.GetTable(
|
||||
l.GetGlobal("commanding"),
|
||||
lua.LString("__commands")),
|
||||
args[0]),
|
||||
NRet: 1,
|
||||
Protect: true,
|
||||
}, luar.New(l, args[1:]))
|
||||
|
|
Loading…
Reference in New Issue