From 8af74a395e60ee8d3b4ff0654a7b083f9ab4314c Mon Sep 17 00:00:00 2001 From: Devin Singh Date: Wed, 28 Apr 2021 06:26:23 -0500 Subject: [PATCH] wip: add appendpath --- golibs/fs/fs.go | 33 ++++++++++++++++++++++++--------- lua.go | 6 ++++-- preload.lua | 17 +++++++++++++++++ shell.go | 8 +++++--- 4 files changed, 50 insertions(+), 14 deletions(-) diff --git a/golibs/fs/fs.go b/golibs/fs/fs.go index 7a9ce52..4b521f0 100644 --- a/golibs/fs/fs.go +++ b/golibs/fs/fs.go @@ -4,27 +4,42 @@ import ( "os" "strings" - "github.com/yuin/gopher-lua" - "layeh.com/gopher-luar" + lua "github.com/yuin/gopher-lua" + luar "layeh.com/gopher-luar" ) func Loader(L *lua.LState) int { - mod := L.SetFuncs(L.NewTable(), exports) + mod := L.SetFuncs(L.NewTable(), exports) - L.Push(mod) - return 1 + L.Push(mod) + return 1 } - func LuaErr(L *lua.LState, code int) { // TODO: Error with a table, with path and error code L.Error(lua.LNumber(code), 2) } var exports = map[string]lua.LGFunction{ - "cd": cd, - "mkdir": mkdir, - "stat": stat, + "cd": cd, + "mkdir": mkdir, + "stat": stat, + "appendpath": appendpath, +} + +func appendpath(L *lua.LState) int { + path := L.ToString(1) + + os.Setenv("PATH", os.Getenv("PATH")+":"+path) + // cmd := exec.Command("export PATH=" + os.Getenv("PATH") + ":" + path) + // shell.execCommand("export PATH=" + os.Getenv("PATH") + ":" + path) + + // fmt.Println(cmd.String()) + // err := cmd.Run() + // if err != nil { + // LuaErr(L, 1) + // } + return 0 } func cd(L *lua.LState) int { diff --git a/lua.go b/lua.go index 143b7cd..a1060fe 100644 --- a/lua.go +++ b/lua.go @@ -7,7 +7,7 @@ import ( lfs "hilbish/golibs/fs" "os" - "github.com/yuin/gopher-lua" + lua "github.com/yuin/gopher-lua" ) var minimalconf = ` @@ -67,7 +67,9 @@ func LuaInit(confpath string) { } // Run config - if !interactive { return } + if !interactive { + return + } err = l.DoFile(confpath) if err != nil { fmt.Fprintln(os.Stderr, err, diff --git a/preload.lua b/preload.lua index a5d087f..b352583 100644 --- a/preload.lua +++ b/preload.lua @@ -5,6 +5,23 @@ local fs = require 'fs' local commander = require 'commander' local bait = require 'bait' +commander.register('appendpath', function (args) + bait.throw('appendpath', args) + local path = '' + for i = 1, #args do + path = path .. tostring(args[i]) .. ':' + end + path = path:sub(1, -2) + + local ok, err = pcall(function() fs.appendpath(path) end) + if not ok then + if err == 1 then + print('failed to set path') + end + bait.throw('command.exit', err) + else bait.throw('command.exit', 0) end +end) + commander.register('cd', function (args) bait.throw('cd', args) if #args > 0 then diff --git a/shell.go b/shell.go index c1c1d5f..402d99b 100644 --- a/shell.go +++ b/shell.go @@ -8,9 +8,9 @@ import ( "os" "strings" - "github.com/yuin/gopher-lua" "github.com/bobappleyard/readline" - "layeh.com/gopher-luar" + lua "github.com/yuin/gopher-lua" + luar "layeh.com/gopher-luar" "mvdan.cc/sh/v3/interp" "mvdan.cc/sh/v3/syntax" ) @@ -167,7 +167,9 @@ func HandleHistory(cmd string) { func StartMultiline(prev string, sb *strings.Builder) bool { // sb fromt outside is passed so we can // save input from previous prompts - if sb.String() == "" { sb.WriteString(prev + " ") } + if sb.String() == "" { + sb.WriteString(prev + " ") + } fmt.Print(multilinePrompt)