Compare commits

..

No commits in common. "a4dbfbf4bbe8a288139861b8af80ab42a45f0f21" and "edcc1b39f09dbf9317d1cf4915208f6d1f7c3fdc" have entirely different histories.

5 changed files with 28 additions and 56 deletions

View File

@ -25,7 +25,6 @@ func HilbishLoader(L *lua.LState) int {
L.SetField(mod, "ver", lua.LString(version)) L.SetField(mod, "ver", lua.LString(version))
L.SetField(mod, "user", lua.LString(curuser.Username)) L.SetField(mod, "user", lua.LString(curuser.Username))
L.SetField(mod, "host", lua.LString(host)) L.SetField(mod, "host", lua.LString(host))
L.SetField(mod, "home", lua.LString(homedir))
L.Push(mod) L.Push(mod)

52
lua.go
View File

@ -6,7 +6,6 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"syscall" "syscall"
"time"
"hilbish/golibs/bait" "hilbish/golibs/bait"
"hilbish/golibs/commander" "hilbish/golibs/commander"
@ -33,8 +32,6 @@ func LuaInit() {
l.SetGlobal("appendPath", l.NewFunction(hshappendPath)) l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
l.SetGlobal("exec", l.NewFunction(hshexec)) l.SetGlobal("exec", l.NewFunction(hshexec))
l.SetGlobal("goro", luar.New(l, hshgoroutine)) 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 // yes this is stupid, i know
l.PreloadModule("hilbish", HilbishLoader) l.PreloadModule("hilbish", HilbishLoader)
@ -45,9 +42,16 @@ func LuaInit() {
cmds := commander.New() cmds := commander.New()
// When a command from Lua is added, register it for use // When a command from Lua is added, register it for use
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) { // TODO: maybe dont add command code to a lua table? insstead use a map
commands[cmdName] = cmd 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) l.PreloadModule("commander", cmds.Loader)
@ -147,39 +151,3 @@ func hshexec(L *lua.LState) int {
func hshgoroutine(gofunc func()) { func hshgoroutine(gofunc func()) {
go gofunc() 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 l *lua.LState
lr *LineReader lr *LineReader
commands = map[string]*lua.LFunction{} commands = map[string]bool{}
aliases = map[string]string{} aliases = map[string]string{}
homedir string homedir string
@ -140,7 +140,6 @@ func main() {
os.Exit(0) os.Exit(0)
} }
input:
for interactive { for interactive {
running = false running = false
@ -163,10 +162,8 @@ input:
if strings.HasSuffix(input, "\\") { if strings.HasSuffix(input, "\\") {
for { for {
input, err = ContinuePrompt(strings.TrimSuffix(input, "\\")) input, err = ContinuePrompt(strings.TrimSuffix(input, "\\"))
if err != nil {
goto input // continue inside nested loop if err != nil || !strings.HasSuffix(input, "\\") {
}
if !strings.HasSuffix(input, "\\") {
break break
} }
} }

View File

@ -3,7 +3,7 @@
local fs = require 'fs' local fs = require 'fs'
local commander = require 'commander' local commander = require 'commander'
local bait = require 'bait' local bait = require 'bait'
local oldDir = hilbish.cwd() local old_dir = hilbish.cwd()
local shlvl = tonumber(os.getenv 'SHLVL') local shlvl = tonumber(os.getenv 'SHLVL')
if shlvl ~= nil then os.setenv('SHLVL', shlvl + 1) else os.setenv('SHLVL', 1) end if shlvl ~= nil then os.setenv('SHLVL', shlvl + 1) else os.setenv('SHLVL', 1) end
@ -20,10 +20,10 @@ commander.register('cd', function (args)
:gsub('$([%w_]+)', os.getenv):gsub('%z','$'):gsub("%s+", "") :gsub('$([%w_]+)', os.getenv):gsub('%z','$'):gsub("%s+", "")
if path == '-' then if path == '-' then
path = oldDir path = old_dir
print(path) print(path)
end end
oldDir = hilbish.cwd() old_dir = hilbish.cwd()
local ok, err = pcall(function() fs.cd(path) end) local ok, err = pcall(function() fs.cd(path) end)
if not ok then if not ok then

View File

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