Compare commits

...

9 Commits

Author SHA1 Message Date
sammyette a4dbfbf4bb
feat: add hilbish.home
`hilbish.home` is the user's home directory path
this is a cross platform variable that works instead of using
`$HOME` (which isnt set in windows cmd, i think)
2021-06-11 21:49:28 -04:00
sammyette efc956a04c
fix: cleanup, store lua commands in map 2021-06-11 21:37:52 -04:00
sammyette 1e1662a6b2
feat: add interval function
interval(func, time)
works exactly the same as `setInterval` in javascript
runs `func` in an interval of `time` milliseconds
2021-06-11 21:21:41 -04:00
sammyette fe3df8c66e
feat: add timeout function
timeout(func, time)
works exactly like the `setTimeout` function in javascript
runs `func` after a period of `time` in milliseconds
2021-06-11 19:40:08 -04:00
sammyette 419e327d95
fix: prompt for new input on error with ContinuePrompt
basically, dont disregard the error we get and dont return previous
input. now we try to prompt for new input without running the
previous if the function got an error
2021-06-11 18:26:34 -04:00
sammyette 7a8df8b9f2
revert: fix: return previous input on continue input error
This reverts commit 5b03b3cef4.
we shouldn't return this just to fix the panic,
instead just handle the error
2021-06-11 18:25:24 -04:00
sammyette 5b03b3cef4
fix: return previous input on continue input error
this is basically 72973eade7
but it actually works and doesnt break everything
if an error occurred with the ContinuePrompt function (in this case,
the error is simply EOL/ctrl d) then return previous input alone
2021-06-11 18:08:31 -04:00
sammyette 03b98bdd26
revert: fix: panic on ctrl d on continued input
This reverts commit 72973eade7.
i'm kinda a bit dumb
this fix broke basically everything else that used the splitInput
function
2021-06-11 18:03:52 -04:00
sammyette 72973eade7
fix: panic on ctrl d on continued input
basically when a person did the continue prompt (input ending with `\`)
and exited with ctrl d it caused a panic
this was the simplest way to fix that
2021-06-11 17:58:35 -04:00
4 changed files with 53 additions and 25 deletions

View File

@ -25,6 +25,7 @@ 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)

50
lua.go
View File

@ -6,6 +6,7 @@ import (
"os/exec"
"strings"
"syscall"
"time"
"hilbish/golibs/bait"
"hilbish/golibs/commander"
@ -32,6 +33,8 @@ 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)
@ -42,15 +45,8 @@ func LuaInit() {
cmds := commander.New()
// When a command from Lua is added, register it for use
// 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)
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
commands[cmdName] = cmd
})
l.PreloadModule("commander", cmds.Loader)
@ -151,3 +147,39 @@ 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
}

View File

@ -22,7 +22,7 @@ var (
l *lua.LState
lr *LineReader
commands = map[string]bool{}
commands = map[string]*lua.LFunction{}
aliases = map[string]string{}
homedir string
@ -140,6 +140,7 @@ func main() {
os.Exit(0)
}
input:
for interactive {
running = false
@ -162,8 +163,10 @@ func main() {
if strings.HasSuffix(input, "\\") {
for {
input, err = ContinuePrompt(strings.TrimSuffix(input, "\\"))
if err != nil || !strings.HasSuffix(input, "\\") {
if err != nil {
goto input // continue inside nested loop
}
if !strings.HasSuffix(input, "\\") {
break
}
}

View File

@ -49,13 +49,9 @@ func RunInput(input string) {
hooks.Em.Emit("command.exit", 0)
return
}
if commands[cmdArgs[0]] {
if commands[cmdArgs[0]] != nil {
err := l.CallByParam(lua.P{
Fn: l.GetField(
l.GetTable(
l.GetGlobal("commanding"),
lua.LString("__commands")),
cmdArgs[0]),
Fn: commands[cmdArgs[0]],
NRet: 1,
Protect: true,
}, luar.New(l, cmdArgs[1:]))
@ -128,13 +124,9 @@ func execCommand(cmd string) error {
}
// If command is defined in Lua then run it
if commands[args[0]] {
if commands[args[0]] != nil {
err := l.CallByParam(lua.P{
Fn: l.GetField(
l.GetTable(
l.GetGlobal("commanding"),
lua.LString("__commands")),
args[0]),
Fn: commands[args[0]],
NRet: 1,
Protect: true,
}, luar.New(l, args[1:]))