wip: add appendpath

Devin Singh 2021-04-28 06:26:23 -05:00
parent bf11feb48b
commit 8af74a395e
No known key found for this signature in database
GPG Key ID: 7EA319C5D57B6506
4 changed files with 50 additions and 14 deletions

View File

@ -4,27 +4,42 @@ import (
"os" "os"
"strings" "strings"
"github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
"layeh.com/gopher-luar" luar "layeh.com/gopher-luar"
) )
func Loader(L *lua.LState) int { func Loader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports) mod := L.SetFuncs(L.NewTable(), exports)
L.Push(mod) L.Push(mod)
return 1 return 1
} }
func LuaErr(L *lua.LState, code int) { func LuaErr(L *lua.LState, code int) {
// TODO: Error with a table, with path and error code // TODO: Error with a table, with path and error code
L.Error(lua.LNumber(code), 2) L.Error(lua.LNumber(code), 2)
} }
var exports = map[string]lua.LGFunction{ var exports = map[string]lua.LGFunction{
"cd": cd, "cd": cd,
"mkdir": mkdir, "mkdir": mkdir,
"stat": stat, "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 { func cd(L *lua.LState) int {

6
lua.go
View File

@ -7,7 +7,7 @@ import (
lfs "hilbish/golibs/fs" lfs "hilbish/golibs/fs"
"os" "os"
"github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
) )
var minimalconf = ` var minimalconf = `
@ -67,7 +67,9 @@ func LuaInit(confpath string) {
} }
// Run config // Run config
if !interactive { return } if !interactive {
return
}
err = l.DoFile(confpath) err = l.DoFile(confpath)
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err, fmt.Fprintln(os.Stderr, err,

View File

@ -5,6 +5,23 @@ local fs = require 'fs'
local commander = require 'commander' local commander = require 'commander'
local bait = require 'bait' 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) commander.register('cd', function (args)
bait.throw('cd', args) bait.throw('cd', args)
if #args > 0 then if #args > 0 then

View File

@ -8,9 +8,9 @@ import (
"os" "os"
"strings" "strings"
"github.com/yuin/gopher-lua"
"github.com/bobappleyard/readline" "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/interp"
"mvdan.cc/sh/v3/syntax" "mvdan.cc/sh/v3/syntax"
) )
@ -167,7 +167,9 @@ func HandleHistory(cmd string) {
func StartMultiline(prev string, sb *strings.Builder) bool { func StartMultiline(prev string, sb *strings.Builder) bool {
// sb fromt outside is passed so we can // sb fromt outside is passed so we can
// save input from previous prompts // save input from previous prompts
if sb.String() == "" { sb.WriteString(prev + " ") } if sb.String() == "" {
sb.WriteString(prev + " ")
}
fmt.Print(multilinePrompt) fmt.Print(multilinePrompt)