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, "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)
|
||||||
|
|
||||||
|
|
50
lua.go
50
lua.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"hilbish/golibs/bait"
|
"hilbish/golibs/bait"
|
||||||
"hilbish/golibs/commander"
|
"hilbish/golibs/commander"
|
||||||
|
@ -32,6 +33,8 @@ 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)
|
||||||
|
@ -42,15 +45,8 @@ 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
|
||||||
// TODO: maybe dont add command code to a lua table? insstead use a map
|
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
|
||||||
cmds.Events.On("commandRegister",
|
commands[cmdName] = cmd
|
||||||
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)
|
||||||
|
@ -151,3 +147,39 @@ 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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
9
main.go
9
main.go
|
@ -22,7 +22,7 @@ var (
|
||||||
l *lua.LState
|
l *lua.LState
|
||||||
lr *LineReader
|
lr *LineReader
|
||||||
|
|
||||||
commands = map[string]bool{}
|
commands = map[string]*lua.LFunction{}
|
||||||
aliases = map[string]string{}
|
aliases = map[string]string{}
|
||||||
|
|
||||||
homedir string
|
homedir string
|
||||||
|
@ -140,6 +140,7 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
input:
|
||||||
for interactive {
|
for interactive {
|
||||||
running = false
|
running = false
|
||||||
|
|
||||||
|
@ -162,8 +163,10 @@ func main() {
|
||||||
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 {
|
||||||
if err != nil || !strings.HasSuffix(input, "\\") {
|
goto input // continue inside nested loop
|
||||||
|
}
|
||||||
|
if !strings.HasSuffix(input, "\\") {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 old_dir = hilbish.cwd()
|
local oldDir = 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 = old_dir
|
path = oldDir
|
||||||
print(path)
|
print(path)
|
||||||
end
|
end
|
||||||
old_dir = hilbish.cwd()
|
oldDir = 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
|
||||||
|
|
16
shell.go
16
shell.go
|
@ -49,13 +49,9 @@ func RunInput(input string) {
|
||||||
hooks.Em.Emit("command.exit", 0)
|
hooks.Em.Emit("command.exit", 0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if commands[cmdArgs[0]] {
|
if commands[cmdArgs[0]] != nil {
|
||||||
err := l.CallByParam(lua.P{
|
err := l.CallByParam(lua.P{
|
||||||
Fn: l.GetField(
|
Fn: commands[cmdArgs[0]],
|
||||||
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:]))
|
||||||
|
@ -128,13 +124,9 @@ 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]] {
|
if commands[args[0]] != nil {
|
||||||
err := l.CallByParam(lua.P{
|
err := l.CallByParam(lua.P{
|
||||||
Fn: l.GetField(
|
Fn: commands[args[0]],
|
||||||
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:]))
|
||||||
|
|
Loading…
Reference in New Issue