feat(wip): add appendpath

pull/42/head
Devin Singh 2021-04-28 06:26:23 -05:00
parent bf11feb48b
commit 4476a96eec
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"
"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 {

6
lua.go
View File

@ -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,

View File

@ -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

View File

@ -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)