From 198f5f63343b82a78002c8b43212c56a22d1bf6e Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Sat, 20 Mar 2021 18:51:05 -0400 Subject: [PATCH] "feat: add custom commands via lua in config" --- go.mod | 8 +++++--- go.sum | 5 +++++ lua.go | 1 + main.go | 39 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 47 insertions(+), 6 deletions(-) diff --git a/go.mod b/go.mod index 75c5198..45acaea 100644 --- a/go.mod +++ b/go.mod @@ -4,10 +4,12 @@ go 1.16 require ( github.com/Nerdmaster/terminal v0.12.1 // indirect - github.com/akamensky/argparse v1.2.2 // indirect - github.com/bobappleyard/readline v0.0.0-20150707195538-7e300e02d38e // indirect + github.com/akamensky/argparse v1.2.2 + github.com/bobappleyard/readline v0.0.0-20150707195538-7e300e02d38e + github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 // indirect github.com/eiannone/keyboard v0.0.0-20200508000154-caf4b762e807 // indirect github.com/gdamore/tcell v1.4.0 // indirect - github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da // indirect + github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d // indirect + layeh.com/gopher-luar v1.0.8 // indirect ) diff --git a/go.sum b/go.sum index 21bb593..1e46906 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ github.com/akamensky/argparse v1.2.2 h1:P17T0ZjlUNJuWTPPJ2A5dM1wxarHgHqfYH+AZTo2 github.com/akamensky/argparse v1.2.2/go.mod h1:S5kwC7IuDcEr5VeXtGPRVZ5o/FdhcMlQz4IZQuw64xA= github.com/bobappleyard/readline v0.0.0-20150707195538-7e300e02d38e h1:4G8AYOOwZdDWOiJR6D6JXaFmj5BDS7c5D5PyqsG/+Hg= github.com/bobappleyard/readline v0.0.0-20150707195538-7e300e02d38e/go.mod h1:fmqtV+Wqx0uFYLN1F4VhjZdtT56Dr8c3yA7nALFsw/Q= +github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9 h1:xz6Nv3zcwO2Lila35hcb0QloCQsc38Al13RNEzWRpX4= +github.com/chuckpreslar/emission v0.0.0-20170206194824-a7ddd980baf9/go.mod h1:2wSM9zJkl1UQEFZgSd68NfCgRz1VL1jzy/RjCg+ULrs= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -17,6 +19,7 @@ github.com/lucasb-eyer/go-colorful v1.0.3 h1:QIbQXiugsb+q10B+MI+7DI1oQLdmnep86tW github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54= github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= +github.com/yuin/gopher-lua v0.0.0-20190206043414-8bfc7677f583/go.mod h1:gqRgreBUhTSL0GeU64rtZ3Uq3wtjOa/TB2YfrtkCbVQ= github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da h1:NimzV1aGyq29m5ukMK0AMWEhFaL/lrEOaephfuoiARg= github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da/go.mod h1:E1AXubJBdNmFERAOucpDIxNzeGfLzg0mYh+UfMWdChA= golang.org/x/sys v0.0.0-20190204203706-41f3e6584952/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -25,3 +28,5 @@ golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d h1:jbzgAvDZn8aEnytae+4ou0J0G golang.org/x/sys v0.0.0-20210319071255-635bc2c9138d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +layeh.com/gopher-luar v1.0.8 h1:Uqws1Z6T0vK6pZ7ehNNurLLSFcz7+E0EOHVM4FNiMQs= +layeh.com/gopher-luar v1.0.8/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk= diff --git a/lua.go b/lua.go index 685df84..0fde645 100644 --- a/lua.go +++ b/lua.go @@ -9,3 +9,4 @@ func hshprompt(L *lua.LState) int { return 0 } + diff --git a/main.go b/main.go index ab2c15d..714553a 100644 --- a/main.go +++ b/main.go @@ -10,15 +10,19 @@ import ( "os/signal" "strings" "io" + lfs "hilbish/golibs/fs" + cmds "hilbish/golibs/commander" "github.com/akamensky/argparse" "github.com/bobappleyard/readline" "github.com/yuin/gopher-lua" + "layeh.com/gopher-luar" ) -const version = "0.0.5" +const version = "0.0.6" var l *lua.LState var prompt string +var commands = map[string]bool{} func main() { parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers") @@ -76,11 +80,25 @@ func main() { if len(cmdArgs) == 0 { continue } + if commands[cmdArgs[0]] { + err := l.CallByParam(lua.P{ + Fn: l.GetField( + l.GetTable( + l.GetGlobal("commander"), + lua.LString("__commands")), + cmdArgs[0]), + NRet: 0, + Protect: true, + }, luar.New(l, cmdArgs[1:])) + if err != nil { + // TODO: dont panic + panic(err) + } + continue + } switch cmdArgs[0] { case "exit": os.Exit(0) - case "cd": - os.Chdir(strings.Trim(cmdString, "cd ")) default: cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) cmd.Stderr = os.Stderr @@ -109,6 +127,21 @@ func LuaInit() { l.SetGlobal("prompt", l.NewFunction(hshprompt)) + l.PreloadModule("fs", lfs.Loader) + + commander := cmds.New() + commander.Events.On("commandRegister", + func (cmdName string, cmd *lua.LFunction) { + commands[cmdName] = true + l.SetField( + l.GetTable(l.GetGlobal("commander"), + lua.LString("__commands")), + cmdName, + cmd) + }) + + l.PreloadModule("commander", commander.Loader) + err := l.DoFile(os.Getenv("HOME") + "/.hilbishrc.lua") if err != nil { panic(err)