From 3e85e1bf682eea2a8ab671bb8ed47e4bd9fccc45 Mon Sep 17 00:00:00 2001 From: sammy-ette Date: Sat, 14 Jun 2025 13:26:41 -0400 Subject: [PATCH] feat: add hilbish.completion (enough to init nature) --- api.go | 6 +-- complete.go | 77 ++++++++++++++++++-------------------- moonlight/function_clua.go | 6 +++ nature/init.lua | 2 + 4 files changed, 47 insertions(+), 44 deletions(-) diff --git a/api.go b/api.go index 37aec7d8..3800d150 100644 --- a/api.go +++ b/api.go @@ -106,10 +106,10 @@ 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) diff --git a/complete.go b/complete.go index 45a932f4..cd0da015 100644 --- a/complete.go +++ b/complete.go @@ -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 } -*/ diff --git a/moonlight/function_clua.go b/moonlight/function_clua.go index 84cfeffa..27b3ab61 100644 --- a/moonlight/function_clua.go +++ b/moonlight/function_clua.go @@ -3,6 +3,7 @@ package moonlight import ( + "errors" "fmt" "github.com/aarzilli/golua/lua" @@ -36,6 +37,11 @@ func (mlr *Runtime) StringArg(num int) (string, error) { return mlr.state.CheckString(num + 1), nil } +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(c *GoCont, num int) Value { return c.vals[num] } diff --git a/nature/init.lua b/nature/init.lua index 6b696cfd..716c8da2 100644 --- a/nature/init.lua +++ b/nature/init.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'