2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-07-01 08:42:04 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
754bb6c9c2
fix: add TableArg for golua 2025-06-14 14:01:41 -04:00
6d793f35eb
feat: add missing functions, os.setenv
clua now starts with no errors.
2025-06-14 13:59:25 -04:00
19938fa8ef
feat: add hilbish.runner and hilbish.appendPath 2025-06-14 13:40:28 -04:00
3e85e1bf68
feat: add hilbish.completion (enough to init nature) 2025-06-14 13:26:41 -04:00
417ccf7ca8
feat: add hilbish.cwd 2025-06-14 12:31:25 -04:00
9 changed files with 135 additions and 91 deletions

38
api.go
View File

@ -44,9 +44,7 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
var exports = map[string]moonlight.Export{
/*
"alias": {hlalias, 2, false},
"appendPath": {hlappendPath, 1, false},
"complete": {hlcomplete, 2, false},
"cwd": {hlcwd, 0, false},
"exec": {hlexec, 1, false},
"runnerMode": {hlrunnerMode, 1, false},
"goro": {hlgoro, 1, true},
@ -55,7 +53,9 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
"multiprompt": {hlmultiprompt, 1, false},
"prependPath": {hlprependPath, 1, false},
*/
"prompt": {hlprompt, 1, true},
"appendPath": {hlappendPath, 1, false},
"cwd": {hlcwd, 0, false},
"prompt": {hlprompt, 1, true},
/*
"inputMode": {hlinputMode, 1, false},
"interval": {hlinterval, 2, false},
@ -106,14 +106,14 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
//mod.Set(rt.StringValue("history"), rt.TableValue(historyModule))
// hilbish.completion table
//hshcomp := completionLoader(rtm)
hshcomp := completionLoader(mlr)
// TODO: REMOVE "completion" AND ONLY USE "completions" WITH AN S
//mod.Set(rt.StringValue("completion"), rt.TableValue(hshcomp))
//mod.Set(rt.StringValue("completions"), rt.TableValue(hshcomp))
hshMod.SetField("completion", moonlight.TableValue(hshcomp))
hshMod.SetField("completions", moonlight.TableValue(hshcomp))
// hilbish.runner table
//runnerModule := runnerModeLoader(mlr)
//hshMod.SetField("runner", moonlight.TableValue(runnerModule))
runnerModule := runnerModeLoader(mlr)
hshMod.SetField("runner", moonlight.TableValue(runnerModule))
// hilbish.jobs table
jobs = newJobHandler()
@ -475,11 +475,11 @@ hilbish.appendPath {
}
#example
*/
func hlappendPath(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
func hlappendPath(mlr *moonlight.Runtime) error {
if err := mlr.Check1Arg(); err != nil {
return nil, err
return err
}
arg := mlr.Arg(c, 0)
arg := mlr.Arg(0)
// check if dir is a table or a string
if moonlight.Type(arg) == moonlight.TableType {
@ -491,10 +491,10 @@ func hlappendPath(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont,
} else if moonlight.Type(arg) == moonlight.StringType {
appendPath(arg.AsString())
} else {
return nil, errors.New("bad argument to appendPath (expected string or table, got " + arg.TypeName() + ")")
return errors.New("bad argument to appendPath (expected string or table, got " + arg.TypeName() + ")")
}
return c.Next(), nil
return nil
}
func appendPath(dir string) {
@ -770,11 +770,11 @@ func hlinputMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// will call it to execute user input instead.
// Read [about runner mode](../features/runner-mode) for more information.
// #param mode string|function
func hlrunnerMode(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
func hlrunnerMode(mlr *moonlight.Runtime) error {
if err := mlr.Check1Arg(); err != nil {
return nil, err
return err
}
mode := mlr.Arg(c, 0)
mode := mlr.Arg(0)
switch moonlight.Type(mode) {
case moonlight.StringType:
@ -782,15 +782,15 @@ func hlrunnerMode(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont,
case "hybrid", "hybridRev", "lua", "sh":
runnerMode = mode
default:
return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.AsString())
return errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.AsString())
}
case moonlight.FunctionType:
runnerMode = mode
default:
return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName())
return errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName())
}
return c.Next(), nil
return nil
}
// hinter(line, pos)

View File

@ -2,13 +2,12 @@ package main
import (
//"errors"
"os"
"path/filepath"
"strings"
"os"
"hilbish/moonlight"
"hilbish/util"
//rt "github.com/arnodel/golua/runtime"
)
var charEscapeMap = []string{
@ -34,9 +33,9 @@ var escapeInvertReplaer = strings.NewReplacer(charEscapeMapInvert...)
func invert(m []string) []string {
newM := make([]string, len(charEscapeMap))
for i := range m {
if (i + 1) % 2 == 0 {
newM[i] = m[i - 1]
newM[i - 1] = m[i]
if (i+1)%2 == 0 {
newM[i] = m[i-1]
newM[i-1] = m[i]
}
}
@ -52,7 +51,7 @@ func splitForFile(str string) []string {
if r == '"' {
quoted = !quoted
sb.WriteRune(r)
} else if r == ' ' && str[i - 1] == '\\' {
} else if r == ' ' && str[i-1] == '\\' {
sb.WriteRune(r)
} else if !quoted && r == ' ' {
split = append(split, sb.String())
@ -76,7 +75,7 @@ func fileComplete(query, ctx string, fields []string) ([]string, string) {
q := splitForFile(ctx)
path := ""
if len(q) != 0 {
path = q[len(q) - 1]
path = q[len(q)-1]
}
return matchPath(path)
@ -86,7 +85,7 @@ func binaryComplete(query, ctx string, fields []string) ([]string, string) {
q := splitForFile(ctx)
query = ""
if len(q) != 0 {
query = q[len(q) - 1]
query = q[len(q)-1]
}
var completions []string
@ -111,7 +110,7 @@ func binaryComplete(query, ctx string, fields []string) ([]string, string) {
// filter out executables, but in path
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
// search for an executable which matches our query string
if matches, err := filepath.Glob(filepath.Join(dir, query + "*")); err == nil {
if matches, err := filepath.Glob(filepath.Join(dir, query+"*")); err == nil {
// get basename from matches
for _, match := range matches {
// check if we have execute permissions for our match
@ -159,7 +158,7 @@ func matchPath(query string) ([]string, string) {
for _, entry := range files {
// should we handle errors here?
file, err := entry.Info()
if err == nil && file.Mode() & os.ModeSymlink != 0 {
if err == nil && file.Mode()&os.ModeSymlink != 0 {
path, err := filepath.EvalSymlinks(filepath.Join(path, file.Name()))
if err == nil {
file, err = os.Lstat(path)
@ -191,21 +190,19 @@ func escapeFilename(fname string) string {
// #interface completion
// tab completions
// The completions interface deals with tab completions.
/*
func completionLoader(rtm *rt.Runtime) *rt.Table {
exports := map[string]util.LuaExport{
func completionLoader(mlr *moonlight.Runtime) *moonlight.Table {
exports := map[string]moonlight.Export{
"bins": {hcmpBins, 3, false},
"call": {hcmpCall, 4, false},
"files": {hcmpFiles, 3, false},
"handler": {hcmpHandler, 2, false},
//"call": {hcmpCall, 4, false},
// "files": {hcmpFiles, 3, false},
// "handler": {hcmpHandler, 2, false},
}
mod := rt.NewTable()
util.SetExports(rtm, mod, exports)
mod := moonlight.NewTable()
mlr.SetExports(mod, exports)
return mod
}
*/
// #interface completion
// bins(query, ctx, fields) -> entries (table), prefix (string)
@ -233,23 +230,23 @@ hilbish.complete('command.sudo', function(query, ctx, fields)
end)
#example
*/
/*
func hcmpBins(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
query, ctx, fds, err := getCompleteParams(t, c)
func hcmpBins(mlr *moonlight.Runtime) error {
query, ctx, fds, err := getCompleteParams(mlr)
if err != nil {
return nil, err
return err
}
completions, pfx := binaryComplete(query, ctx, fds)
luaComps := rt.NewTable()
completions, _ := binaryComplete(query, ctx, fds)
luaComps := moonlight.NewTable()
for i, comp := range completions {
luaComps.Set(rt.IntValue(int64(i + 1)), rt.StringValue(comp))
luaComps.Set(moonlight.IntValue(int64(i+1)), moonlight.StringValue(comp))
}
return c.PushingNext(t.Runtime, rt.TableValue(luaComps), rt.StringValue(pfx)), nil
mlr.PushNext1(moonlight.TableValue(luaComps))
//return c.PushingNext(t.Runtime, rt.TableValue(luaComps), rt.StringValue(pfx)), nil
return nil
}
*/
// #interface completion
// call(name, query, ctx, fields) -> completionGroups (table), prefix (string)
@ -352,35 +349,33 @@ function hilbish.completion.handler(line, pos)
end
#example
*/
/*
func hcmpHandler(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil
func hcmpHandler(mlr *moonlight.Runtime) error {
return nil
}
func getCompleteParams(t *rt.Thread, c *rt.GoCont) (string, string, []string, error) {
if err := c.CheckNArgs(3); err != nil {
func getCompleteParams(mlr *moonlight.Runtime) (string, string, []string, error) {
if err := mlr.CheckNArgs(3); err != nil {
return "", "", []string{}, err
}
query, err := c.StringArg(0)
query, err := mlr.StringArg(0)
if err != nil {
return "", "", []string{}, err
}
ctx, err := c.StringArg(1)
ctx, err := mlr.StringArg(1)
if err != nil {
return "", "", []string{}, err
}
fields, err := c.TableArg(2)
fields, err := mlr.TableArg(2)
if err != nil {
return "", "", []string{}, err
}
var fds []string
util.ForEach(fields, func(k rt.Value, v rt.Value) {
if v.Type() == rt.StringType {
moonlight.ForEach(fields, func(k moonlight.Value, v moonlight.Value) {
if v.Type() == moonlight.StringType {
fds = append(fds, v.AsString())
}
})
return query, ctx, fds, err
}
*/

View File

@ -213,8 +213,8 @@ func (b *Bait) callRecoverer(event string, handler *Listener, err interface{}) {
func (b *Bait) Loader(rtm *moonlight.Runtime) moonlight.Value {
exports := map[string]moonlight.Export{
"catch": {b.bcatch, 2, false},
/*
"catch": {b.bcatch, 2, false},
"catchOnce": util.LuaExport{b.bcatchOnce, 2, false},
"throw": util.LuaExport{b.bthrow, 1, true},
"release": util.LuaExport{b.brelease, 2, false},
@ -261,15 +261,15 @@ bait.catch('hilbish.exit', function()
end)
#example
*/
func (b *Bait) bcatch(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
func (b *Bait) bcatch(mlr *moonlight.Runtime) error {
name, catcher, err := util.HandleStrCallback(mlr)
if err != nil {
return nil, err
return err
}
b.OnLua(name, catcher)
return c.Next(), nil
return nil
}
/*

View File

@ -2,10 +2,6 @@
package moonlight
import (
"fmt"
)
type Callable interface {
Continuation(*Runtime, Cont) Cont
}
@ -16,8 +12,6 @@ type Closure struct {
}
func (mlr *Runtime) ClosureArg(num int) (*Closure, error) {
fmt.Println("type at ", num, "is", mlr.state.LTypename(num))
return &Closure{
refIdx: -1,
}, nil

View File

@ -3,6 +3,7 @@
package moonlight
import (
"errors"
"fmt"
"github.com/aarzilli/golua/lua"
@ -36,8 +37,13 @@ func (mlr *Runtime) StringArg(num int) (string, error) {
return mlr.state.CheckString(num + 1), nil
}
func (mlr *Runtime) Arg(c *GoCont, num int) Value {
return c.vals[num]
func (mlr *Runtime) TableArg(num int) (*Table, error) {
//return mlr.state.CheckType(num+1, lua.LUA_TTABLE)
return nil, errors.New("TableArg unimplemented")
}
func (mlr *Runtime) Arg(num int) Value {
return NilValue
}
func (mlr *Runtime) GoFunction(fun GoToLuaFunc) *GoFunctionFunc {

View File

@ -20,11 +20,22 @@ func (mlr *Runtime) StringArg(num int) (string, error) {
return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).StringArg(num)
}
func (mlr *Runtime) TableArg(num int) (*Table, error) {
tbl, err := mlr.rt.MainThread().CurrentCont().(*rt.GoCont).TableArg(num)
if err != nil {
return nil, err
}
return &Table{
lt: tbl,
}, nil
}
func (mlr *Runtime) ClosureArg(num int) (*Closure, error) {
return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).ClosureArg(num)
}
func (mlr *Runtime) Arg(c *GoCont, num int) Value {
func (mlr *Runtime) Arg(num int) Value {
return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).Arg(num)
}

View File

@ -4,6 +4,7 @@ package moonlight
import (
"fmt"
"os"
"github.com/aarzilli/golua/lua"
)
@ -17,9 +18,39 @@ func NewRuntime() *Runtime {
L := lua.NewState()
L.OpenLibs()
return &Runtime{
mlr := &Runtime{
state: L,
}
mlr.Extras()
return mlr
}
func (mlr *Runtime) Extras() {
mlr.state.GetGlobal("os")
mlr.pushToState(FunctionValue(mlr.GoFunction(setenv)))
mlr.state.SetField(-2, "setenv")
}
func setenv(mlr *Runtime) error {
if err := mlr.CheckNArgs(2); err != nil {
return err
}
env, err := mlr.StringArg(0)
if err != nil {
return err
}
varr, err := mlr.StringArg(1)
if err != nil {
return err
}
os.Setenv(env, varr)
return nil
}
func (mlr *Runtime) PushNext1(v Value) {

View File

@ -1,7 +1,7 @@
-- Prelude initializes everything else for our shell
local _ = require 'succulent' -- Function additions
local bait = require 'bait'
--local fs = require 'fs'
local fs = require 'fs'
package.path = package.path .. ';' .. hilbish.dataDir .. '/?/init.lua'
.. ';' .. hilbish.dataDir .. '/?/?.lua' .. ";" .. hilbish.dataDir .. '/?.lua'
@ -18,6 +18,8 @@ if not hilbish.midnightEdition then
-- it didnt work normally, idk
return function() return hilbish.module.load(path) end, path
end)
else
pcall = unsafe_pcall
end
require 'nature.commands'
@ -64,6 +66,7 @@ do
})
end
--[[
do
local startSearchPath = hilbish.userDir.data .. '/hilbish/start/?/init.lua;'
.. hilbish.userDir.data .. '/hilbish/start/?.lua'
@ -80,6 +83,7 @@ do
package.path = package.path .. ';' .. startSearchPath
end
]]--
bait.catch('error', function(event, handler, err)
print(string.format('Encountered an error in %s handler\n%s', event, err:sub(8)))

View File

@ -1,5 +1,7 @@
package main
import "hilbish/moonlight"
// #interface runner
// interactive command runner customization
/* The runner interface contains functions that allow the user to change
@ -42,10 +44,11 @@ hilbish.runnerMode(function(input)
return hilbish.runner.sh(input)
end)
```
*/
func runnerModeLoader(rtm *moonlight.Runtime) *moonlight.Table {
exports := map[string]moonlight.Export{
"sh": {shRunner, 1, false},
"lua": {luaRunner, 1, false},
"sh": {shRunner, 1, false},
"lua": {luaRunner, 1, false},
"setMode": {hlrunnerMode, 1, false},
}
@ -69,13 +72,13 @@ func _runnerMode() {}
// Runs a command in Hilbish's shell script interpreter.
// This is the equivalent of using `source`.
// #param cmd string
func shRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
if err := mlr.Check1Arg(c); err != nil {
return nil, err
func shRunner(mlr *moonlight.Runtime) error {
if err := mlr.Check1Arg(); err != nil {
return err
}
cmd, err := mlr.StringArg(c, 0)
cmd, err := mlr.StringArg(0)
if err != nil {
return nil, err
return err
}
_, exitCode, cont, err := execSh(aliases.Resolve(cmd))
@ -89,7 +92,8 @@ func shRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, erro
runnerRet.SetField("continue", moonlight.BoolValue(cont))
runnerRet.SetField("err", luaErr)
return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil
mlr.PushNext1(moonlight.TableValue(runnerRet))
return nil
}
// #interface runner
@ -97,13 +101,13 @@ func shRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, erro
// Evaluates `cmd` as Lua input. This is the same as using `dofile`
// or `load`, but is appropriated for the runner interface.
// #param cmd string
func luaRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
if err := mlr.Check1Arg(c); err != nil {
return nil, err
func luaRunner(mlr *moonlight.Runtime) error {
if err := mlr.Check1Arg(); err != nil {
return err
}
cmd, err := mlr.StringArg(c, 0)
cmd, err := mlr.StringArg(0)
if err != nil {
return nil, err
return err
}
input, exitCode, err := handleLua(cmd)
@ -116,7 +120,6 @@ func luaRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, err
runnerRet.SetField("exitCode", moonlight.IntValue(int64(exitCode)))
runnerRet.SetField("err", luaErr)
return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil
mlr.PushNext1(moonlight.TableValue(runnerRet))
return nil
}
*/