Compare commits

...

9 Commits

Author SHA1 Message Date
sammyette 12828d197a
feat: add hilbish.flag function
this checks if a certain flag has been passed to hilbish
2021-05-16 18:10:46 -04:00
sammyette 4b2891b503
fix: use new way to get username in default config 2021-05-16 17:40:55 -04:00
sammyette ef1ced4a04
refactor: make `hilbish` an already required module and add run function
idk how to explain this man
in code `hilbish` is now a module, which can also be required like
others. but it is already a global export as the variable of the same
name.
also
2021-05-16 17:13:28 -04:00
sammyette c6aa5fa387
fix: remove unneeded struct tag 2021-05-16 16:18:46 -04:00
sammyette 88e117974e
chore: add todo comment for exec function 2021-05-16 16:17:55 -04:00
sammyette e29ba6a0d5
feat!: move username and version access to hilbish table
instead of having ugly `_user` and `_ver` variables,
`hilbish.user` and `hilbish.version` is used
2021-05-16 15:53:21 -04:00
sammyette 9415c5193e
feat(wip): check if lua input is incomplete 2021-05-16 15:50:49 -04:00
sammyette 0850247615
feat: recursively alias commands 2021-05-16 09:32:02 -04:00
sammyette 415e66ab9c
fix: trim prefix of alias instead of trim 2021-05-16 09:29:39 -04:00
5 changed files with 76 additions and 10 deletions

View File

@ -8,8 +8,8 @@ function doPrompt(fail)
))
end
print(lunacolors.format('Welcome to {magenta}Hilbish{reset}, {cyan}' .. _user ..
'{reset}.\n' .. 'The nice lil shell for {blue}Lua{reset} fanatics!\n'))
print(lunacolors.format('Welcome to {magenta}Hilbish{reset}, {cyan}' .. hilbish.user
.. '{reset}.\n' .. 'The nice lil shell for {blue}Lua{reset} fanatics!\n'))
doPrompt()

55
hilbish.go 100644
View File

@ -0,0 +1,55 @@
// Here is the core api for the hilbi shell itself
// Basically, stuff about the shell itself and other functions
// go here.
package main
import (
"os"
"github.com/pborman/getopt"
"github.com/yuin/gopher-lua"
"mvdan.cc/sh/v3/interp"
)
var exports = map[string]lua.LGFunction {
"run": run,
"flag": flag,
}
func HilbishLoader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports)
host, _ := os.Hostname()
L.SetField(mod, "ver", lua.LString(version))
L.SetField(mod, "user", lua.LString(curuser.Username))
L.SetField(mod, "host", lua.LString(host))
L.Push(mod)
return 1
}
// Runs a command
func run(L *lua.LState) int {
var exitcode uint8 = 0
cmd := L.CheckString(1)
err := execCommand(cmd)
if code, ok := interp.IsExitStatus(err); ok {
exitcode = code
} else if err != nil {
exitcode = 1
}
L.Push(lua.LNumber(exitcode))
return 1
}
func flag(L *lua.LState) int {
flagchar := L.CheckString(1)
L.Push(lua.LBool(getopt.Lookup([]rune(flagchar)[0]).Seen()))
return 1
}

11
lua.go
View File

@ -23,18 +23,18 @@ prompt(ansikit.format(
func LuaInit() {
l = lua.NewState()
l.OpenLibs()
l.SetGlobal("_ver", lua.LString(version))
l.SetGlobal("_user", lua.LString(curuser.Username))
l.SetGlobal("prompt", l.NewFunction(hshprompt))
l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt))
l.SetGlobal("alias", l.NewFunction(hshalias))
l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
l.SetGlobal("exec", l.NewFunction(hshexec))
// yes this is stupid, i know
l.PreloadModule("hilbish", HilbishLoader)
l.DoString("hilbish = require 'hilbish'")
// Add fs module to Lua
l.PreloadModule("fs", fs.Loader)
@ -147,6 +147,7 @@ func hshexec(L *lua.LState) int {
// syscall.Exec requires an absolute path to a binary
// path, args, string slice of environments
// TODO: alternative for windows
syscall.Exec(cmdPath, cmdArgs, os.Environ())
return 0
return 0 // random thought: does this ever return?
}

View File

@ -18,9 +18,9 @@ import (
"golang.org/x/term"
)
var version = "v0.4.0"
var (
version = "v0.4.0"
l *lua.LState
prompt string // User's prompt, this will get set when lua side is initialized

View File

@ -8,6 +8,7 @@ import (
"github.com/bobappleyard/readline"
"github.com/yuin/gopher-lua"
"github.com/yuin/gopher-lua/parse"
"layeh.com/gopher-luar"
"mvdan.cc/sh/v3/interp"
"mvdan.cc/sh/v3/syntax"
@ -17,16 +18,25 @@ func RunInput(input string) {
cmdArgs, cmdString := splitInput(input)
// If alias was found, use command alias
if aliases[cmdArgs[0]] != "" {
for aliases[cmdArgs[0]] != "" {
alias := aliases[cmdArgs[0]]
cmdString = alias + strings.Trim(cmdString, cmdArgs[0])
cmdString = alias + strings.TrimPrefix(cmdString, cmdArgs[0])
cmdArgs, cmdString = splitInput(cmdString)
if aliases[cmdArgs[0]] != "" {
continue
}
}
// First try to load input, essentially compiling to bytecode
fn, err := l.LoadString(cmdString)
if err != nil && noexecute {
fmt.Println(err)
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