add lua config

pull/5/head
TorchedSammy 2021-03-19 19:03:11 -04:00
parent 981acc49d2
commit 716d24963f
2 changed files with 39 additions and 10 deletions

11
lua.go 100644
View File

@ -0,0 +1,11 @@
package main
import (
"github.com/yuin/gopher-lua"
)
func hshprompt(L *lua.LState) int {
prompt = L.ToString(1)
return 0
}

38
main.go
View File

@ -5,28 +5,37 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"os/user" _ "os/user"
"os/signal" "os/signal"
"strings" "strings"
"github.com/yuin/gopher-lua"
) )
var l *lua.LState
var prompt string
func main() { func main() {
HandleSignals() HandleSignals()
LuaInit()
for { for {
user, _ := user.Current() //user, _ := user.Current()
dir, _ := os.Getwd() //dir, _ := os.Getwd()
host, _ := os.Hostname() //host, _ := os.Hostname()
reader := bufio.NewReader(os.Stdin) reader := bufio.NewReader(os.Stdin)
fmt.Printf("\u001b[1m\u001b[36m%s@%s \u001b[34m%s $ \u001b[0m", user.Username, host, dir) fmt.Printf(prompt)
cmdString, err := reader.ReadString('\n') cmdString, err := reader.ReadString('\n')
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
} }
cmdString = strings.TrimSuffix(cmdString, "\n") cmdString = strings.TrimSuffix(cmdString, "\n")
err = l.DoString(cmdString)
if err == nil { continue }
cmdArgs := strings.Fields(cmdString) cmdArgs := strings.Fields(cmdString)
if len(cmdArgs) == 0 { continue } if len(cmdArgs) == 0 { continue }
@ -47,9 +56,18 @@ func main() {
} }
func HandleSignals() { func HandleSignals() {
c := make(chan os.Signal) signal.Ignore(os.Interrupt)
signal.Notify(c, os.Interrupt) }
go func() {
<-c func LuaInit() {
}() l = lua.NewState()
l.OpenLibs()
l.SetGlobal("prompt", l.NewFunction(hshprompt))
err := l.DoFile(os.Getenv("HOME") + "/.hilbishrc.lua")
if err != nil {
panic(err)
}
} }