mirror of https://github.com/Hilbis/Hilbish
Compare commits
10 Commits
edcc1b39f0
...
a4dbfbf4bb
Author | SHA1 | Date |
---|---|---|
sammyette | a4dbfbf4bb | |
sammyette | efc956a04c | |
sammyette | 1e1662a6b2 | |
sammyette | fe3df8c66e | |
sammyette | 419e327d95 | |
sammyette | 7a8df8b9f2 | |
sammyette | 5b03b3cef4 | |
sammyette | 03b98bdd26 | |
sammyette | 72973eade7 | |
sammyette | b3a28b067c |
|
@ -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)
|
||||
|
||||
|
|
52
lua.go
52
lua.go
|
@ -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,16 +45,9 @@ 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
|
||||
}
|
||||
|
||||
|
|
9
main.go
9
main.go
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
local fs = require 'fs'
|
||||
local commander = require 'commander'
|
||||
local bait = require 'bait'
|
||||
local old_dir = hilbish.cwd()
|
||||
local oldDir = hilbish.cwd()
|
||||
|
||||
local shlvl = tonumber(os.getenv 'SHLVL')
|
||||
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+", "")
|
||||
|
||||
if path == '-' then
|
||||
path = old_dir
|
||||
path = oldDir
|
||||
print(path)
|
||||
end
|
||||
old_dir = hilbish.cwd()
|
||||
oldDir = hilbish.cwd()
|
||||
|
||||
local ok, err = pcall(function() fs.cd(path) end)
|
||||
if not ok then
|
||||
|
|
16
shell.go
16
shell.go
|
@ -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:]))
|
||||
|
|
Loading…
Reference in New Issue