From 049b517b88d568db3e52cec443729592fc77a7ea Mon Sep 17 00:00:00 2001 From: sammyette Date: Fri, 30 May 2025 12:27:57 -0400 Subject: [PATCH] feat: emit hilbish.cd on fs.cd (closes #350) --- golibs/fs/fs.go | 11 +++++++++++ lua.go | 16 ++++++++-------- nature/commands/cd.lua | 1 - nature/hilbish.lua | 1 + 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/golibs/fs/fs.go b/golibs/fs/fs.go index 9e03325a..adf6f888 100644 --- a/golibs/fs/fs.go +++ b/golibs/fs/fs.go @@ -96,12 +96,23 @@ func fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return nil, err } path = util.ExpandHome(strings.TrimSpace(path)) + oldWd, _ := os.Getwd() + + abspath, err := filepath.Abs(path) + if err != nil { + return nil, err + } err = os.Chdir(path) if err != nil { return nil, err } + util.DoString(t.Runtime, fmt.Sprintf(` + local bait = require 'bait' + bait.throw('hilbish.cd', '%s', '%s') + `, abspath, oldWd)) + return c.Next(), err } diff --git a/lua.go b/lua.go index 9ae030a2..90ad97fb 100644 --- a/lua.go +++ b/lua.go @@ -30,19 +30,11 @@ func luaInit() { // yes this is stupid, i know util.DoString(l, "hilbish = require 'hilbish'") - lib.LoadLibs(l, fs.Loader) - lib.LoadLibs(l, terminal.Loader) - lib.LoadLibs(l, snail.Loader) - - cmds = commander.New(l) - lib.LoadLibs(l, cmds.Loader) - hooks = bait.New(l) hooks.SetRecoverer(func(event string, handler *bait.Listener, err interface{}) { fmt.Println("Error in `error` hook handler:", err) hooks.Off(event, handler) }) - lib.LoadLibs(l, hooks.Loader) // Add Ctrl-C handler @@ -57,6 +49,14 @@ func luaInit() { hooks.Emit("hilbish.rawInput", string(r)) } + lib.LoadLibs(l, fs.Loader) + lib.LoadLibs(l, terminal.Loader) + lib.LoadLibs(l, snail.Loader) + + cmds = commander.New(l) + lib.LoadLibs(l, cmds.Loader) + + // Add more paths that Lua can require from _, err := util.DoString(l, "package.path = package.path .. " + requirePaths) if err != nil { diff --git a/nature/commands/cd.lua b/nature/commands/cd.lua index 9f532cab..723b8288 100644 --- a/nature/commands/cd.lua +++ b/nature/commands/cd.lua @@ -25,5 +25,4 @@ commander.register('cd', function (args, sinks) end bait.throw('cd', path, oldPath) - bait.throw('hilbish.cd', absPath, oldPath) end) diff --git a/nature/hilbish.lua b/nature/hilbish.lua index 3d852a84..6ae9a5d1 100644 --- a/nature/hilbish.lua +++ b/nature/hilbish.lua @@ -3,6 +3,7 @@ local bait = require 'bait' local snail = require 'snail' hilbish.snail = snail.new() +hilbish.snail:run 'true' -- to "initialize" snail bait.catch('hilbish.cd', function(path) hilbish.snail:dir(path) end)