Compare commits

...

8 Commits

Author SHA1 Message Date
TorchedSammy 5500baf5b6 feat: throw cd hook when chanding directory 2021-03-31 01:02:14 -04:00
TorchedSammy 60a0204958 chore: add todo node 2021-03-31 00:02:39 -04:00
TorchedSammy 8d92fc3b74 feat: cd with no path changes to homedir 2021-03-30 23:59:59 -04:00
TorchedSammy 7326cb77c6 fix: display error to user if dir does not exist 2021-03-30 23:58:12 -04:00
TorchedSammy 0d5c826f54 fix: change error level to 2 2021-03-30 23:57:13 -04:00
TorchedSammy 5dd7ce382a fix: throw lua error if fs.cd fails 2021-03-30 23:56:37 -04:00
TorchedSammy 1d22e4cdc1 fix: move LuaInit function to lua.go 2021-03-30 22:37:08 -04:00
TorchedSammy 1234c780f8 chore: new entry to todo 2021-03-30 22:36:56 -04:00
5 changed files with 79 additions and 52 deletions

View File

@ -6,3 +6,4 @@
- [ ] Readme badges
- [x] Hooks
- [x] Aliases
- [ ] hlua (hilbish lua) - 100% lua in the hilbish interactive shell, cuz why not

View File

@ -13,6 +13,12 @@ func Loader(L *lua.LState) int {
return 1
}
func LuaErr(L *lua.LState, code int) {
// TODO: Error with a table, with path and error code
L.Error(lua.LNumber(code), 1)
}
var exports = map[string]lua.LGFunction{
"cd": cd,
}
@ -20,7 +26,13 @@ var exports = map[string]lua.LGFunction{
func cd(L *lua.LState) int {
path := L.ToString(1)
os.Chdir(path)
err := os.Chdir(path)
if err != nil {
switch err.(*os.PathError).Err.Error() {
case "no such file or directory":
LuaErr(L, 2)
}
}
return 0
}

54
lua.go
View File

@ -1,9 +1,63 @@
package main
import (
"fmt"
"os"
lfs "hilbish/golibs/fs"
cmds "hilbish/golibs/commander"
hooks "hilbish/golibs/bait"
"github.com/yuin/gopher-lua"
)
func LuaInit() {
l = lua.NewState()
l.OpenLibs()
l.SetGlobal("prompt", l.NewFunction(hshprompt))
l.SetGlobal("alias", l.NewFunction(hshalias))
// Add fs module to Lua
l.PreloadModule("fs", lfs.Loader)
commander := cmds.New()
// When a command from Lua is added, register it for use
commander.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", commander.Loader)
bait = hooks.New()
l.PreloadModule("bait", bait.Loader)
// Add more paths that Lua can require from
l.DoString("package.path = package.path .. ';./libs/?/init.lua;/usr/share/hilbish/libs/?/init.lua'")
err := l.DoFile("/usr/share/hilbish/preload.lua")
if err != nil {
err = l.DoFile("preload.lua")
if err != nil {
fmt.Fprintln(os.Stderr,
"Missing preload file, builtins may be missing.")
}
}
homedir, _ := os.UserHomeDir()
// Run config
err = l.DoFile(homedir + "/.hilbishrc.lua")
if err != nil {
panic(err)
}
}
func hshprompt(L *lua.LState) int {
prompt = L.ToString(1)

50
main.go
View File

@ -10,8 +10,6 @@ import (
"strings"
"io"
"context"
lfs "hilbish/golibs/fs"
cmds "hilbish/golibs/commander"
hooks "hilbish/golibs/bait"
"github.com/akamensky/argparse"
@ -304,51 +302,3 @@ func HandleSignals() {
}()
}
func LuaInit() {
// TODO: Move to lua.go
l = lua.NewState()
l.OpenLibs()
l.SetGlobal("prompt", l.NewFunction(hshprompt))
l.SetGlobal("alias", l.NewFunction(hshalias))
// Add fs module to Lua
l.PreloadModule("fs", lfs.Loader)
commander := cmds.New()
// When a command from Lua is added, register it for use
commander.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", commander.Loader)
bait = hooks.New()
l.PreloadModule("bait", bait.Loader)
// Add more paths that Lua can require from
l.DoString("package.path = package.path .. ';./libs/?/init.lua;/usr/share/hilbish/libs/?/init.lua'")
err := l.DoFile("/usr/share/hilbish/preload.lua")
if err != nil {
err = l.DoFile("preload.lua")
if err != nil {
fmt.Fprintln(os.Stderr,
"Missing preload file, builtins may be missing.")
}
}
homedir, _ := os.UserHomeDir()
// Run config
err = l.DoFile(homedir + "/.hilbishrc.lua")
if err != nil {
panic(err)
}
}

View File

@ -3,9 +3,19 @@
local fs = require 'fs'
local commander = require 'commander'
local bait = require 'bait'
commander.register('cd', function (path)
if #path == 1 then
fs.cd(path[1])
local ok, err = pcall(function() fs.cd(path[1]) end)
if not ok then
if err == 1 then
print('directory does not exist')
end
end
bait.throw('cd', path)
return
end
fs.cd(os.getenv 'HOME')
bait.throw('cd', path)
end)