Compare commits

..

8 Commits

Author SHA1 Message Date
sammyette 36ea20b550
fix: aliases in sh interp exec handler 2021-06-12 11:52:56 -04:00
sammyette 81f7b77b8b
docs: fix description of -l flag 2021-06-12 10:49:22 -04:00
sammyette e6c4c32bd7
feat: handle -h option 2021-06-12 10:48:57 -04:00
sammyette b974ada64e
fix: comment out unused module 2021-06-12 10:43:13 -04:00
sammyette 7e5f9e9541
fix: remove debug logging 2021-06-12 10:41:51 -04:00
sammyette a413b28f2a
fix: define deregister function in commander module 2021-06-12 10:32:48 -04:00
sammyette 26eee56c8b
feat: add commander.deregister function
simply deregisters/removes a lua defined command
2021-06-12 10:30:47 -04:00
sammyette fb04322844
feat: add option for fs.mkdir to make recursive 2021-06-12 09:31:42 -04:00
5 changed files with 30 additions and 8 deletions

View File

@ -18,10 +18,9 @@ func New() Commander {
func (c *Commander) Loader(L *lua.LState) int {
var exports = map[string]lua.LGFunction{
"register": c.register,
"deregister": c.deregister,
}
mod := L.SetFuncs(L.NewTable(), exports)
L.SetGlobal("commanding", &lua.LTable{})
L.SetField(L.GetGlobal("commanding"), "__commands", L.NewTable())
L.Push(mod)
@ -37,3 +36,10 @@ func (c *Commander) register(L *lua.LState) int {
return 0
}
func (c *Commander) deregister(L *lua.LState) int {
cmdName := L.CheckString(1)
c.Events.Emit("commandDeregister", cmdName)
return 0
}

View File

@ -47,9 +47,15 @@ func cd(L *lua.LState) int {
func mkdir(L *lua.LState) int {
dirname := L.CheckString(1)
recursive := L.ToBool(2)
path := strings.TrimSpace(dirname)
// TODO: handle error here
os.Mkdir(strings.TrimSpace(dirname), 0744)
if recursive {
os.MkdirAll(path, 0744)
} else {
os.Mkdir(path, 0744)
}
return 0
}

4
lua.go
View File

@ -48,7 +48,9 @@ func LuaInit() {
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
commands[cmdName] = cmd
})
cmds.Events.On("commandDeregister", func(cmdName string) {
delete(commands, cmdName)
})
l.PreloadModule("commander", cmds.Loader)
hooks = bait.New()

View File

@ -44,11 +44,12 @@ func main() {
defaultConfPath = filepath.Join(strings.Replace(defaultConfDir, "~", homedir, 1), ".hilbishrc.lua")
}
helpflag := getopt.BoolLong("help", 'h', "Prints Hilbish flags")
verflag := getopt.BoolLong("version", 'v', "Prints Hilbish version")
setshflag := getopt.BoolLong("setshellenv", 'S', "Sets $SHELL to Hilbish's executed path")
cmdflag := getopt.StringLong("command", 'c', "", "Executes a command on startup")
configflag := getopt.StringLong("config", 'C', defaultConfPath, "Sets the path to Hilbish's config")
getopt.BoolLong("login", 'l', "Makes Hilbish act like a login shell")
getopt.BoolLong("login", 'l', "Force Hilbish to be a login shell")
getopt.BoolLong("interactive", 'i', "Force Hilbish to be an interactive shell")
getopt.BoolLong("noexec", 'n', "Don't execute and only report Lua syntax errors")
@ -57,6 +58,11 @@ func main() {
interactiveflag := getopt.Lookup('i').Seen()
noexecflag := getopt.Lookup('n').Seen()
if *helpflag {
getopt.PrintUsage(os.Stdout)
os.Exit(0)
}
if *cmdflag == "" || interactiveflag {
interactive = true
}

View File

@ -9,7 +9,7 @@ import (
// "github.com/bobappleyard/readline"
"github.com/yuin/gopher-lua"
"github.com/yuin/gopher-lua/parse"
// "github.com/yuin/gopher-lua/parse"
"layeh.com/gopher-luar"
"mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax"
@ -33,11 +33,12 @@ func RunInput(input string) {
fn, err := l.LoadString(cmdString)
if err != nil && noexecute {
fmt.Println(err)
if lerr, ok := err.(*lua.ApiError); ok {
/* if lerr, ok := err.(*lua.ApiError); ok {
if perr, ok := lerr.Cause.(*parse.Error); ok {
print(perr.Pos.Line == parse.EOF)
}
}
*/
return
}
// And if there's no syntax errors and -n isnt provided, run
@ -120,7 +121,8 @@ func execCommand(cmd string) error {
if aliases[args[0]] != "" {
alias := aliases[args[0]]
argstring = alias + strings.TrimPrefix(argstring, args[0])
args[0] = alias
cmdArgs, _ := splitInput(argstring)
args = cmdArgs
}
// If command is defined in Lua then run it