mirror of
https://github.com/Hilbis/Hilbish
synced 2025-07-01 08:42:04 +00:00
feat: add hilbish.completion (enough to init nature)
This commit is contained in:
parent
417ccf7ca8
commit
3e85e1bf68
6
api.go
6
api.go
@ -106,10 +106,10 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
|
|||||||
//mod.Set(rt.StringValue("history"), rt.TableValue(historyModule))
|
//mod.Set(rt.StringValue("history"), rt.TableValue(historyModule))
|
||||||
|
|
||||||
// hilbish.completion table
|
// hilbish.completion table
|
||||||
//hshcomp := completionLoader(rtm)
|
hshcomp := completionLoader(mlr)
|
||||||
// TODO: REMOVE "completion" AND ONLY USE "completions" WITH AN S
|
// TODO: REMOVE "completion" AND ONLY USE "completions" WITH AN S
|
||||||
//mod.Set(rt.StringValue("completion"), rt.TableValue(hshcomp))
|
hshMod.SetField("completion", moonlight.TableValue(hshcomp))
|
||||||
//mod.Set(rt.StringValue("completions"), rt.TableValue(hshcomp))
|
hshMod.SetField("completions", moonlight.TableValue(hshcomp))
|
||||||
|
|
||||||
// hilbish.runner table
|
// hilbish.runner table
|
||||||
//runnerModule := runnerModeLoader(mlr)
|
//runnerModule := runnerModeLoader(mlr)
|
||||||
|
77
complete.go
77
complete.go
@ -2,13 +2,12 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
//"errors"
|
//"errors"
|
||||||
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"os"
|
|
||||||
|
|
||||||
|
"hilbish/moonlight"
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
|
|
||||||
//rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var charEscapeMap = []string{
|
var charEscapeMap = []string{
|
||||||
@ -34,9 +33,9 @@ var escapeInvertReplaer = strings.NewReplacer(charEscapeMapInvert...)
|
|||||||
func invert(m []string) []string {
|
func invert(m []string) []string {
|
||||||
newM := make([]string, len(charEscapeMap))
|
newM := make([]string, len(charEscapeMap))
|
||||||
for i := range m {
|
for i := range m {
|
||||||
if (i + 1) % 2 == 0 {
|
if (i+1)%2 == 0 {
|
||||||
newM[i] = m[i - 1]
|
newM[i] = m[i-1]
|
||||||
newM[i - 1] = m[i]
|
newM[i-1] = m[i]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -52,7 +51,7 @@ func splitForFile(str string) []string {
|
|||||||
if r == '"' {
|
if r == '"' {
|
||||||
quoted = !quoted
|
quoted = !quoted
|
||||||
sb.WriteRune(r)
|
sb.WriteRune(r)
|
||||||
} else if r == ' ' && str[i - 1] == '\\' {
|
} else if r == ' ' && str[i-1] == '\\' {
|
||||||
sb.WriteRune(r)
|
sb.WriteRune(r)
|
||||||
} else if !quoted && r == ' ' {
|
} else if !quoted && r == ' ' {
|
||||||
split = append(split, sb.String())
|
split = append(split, sb.String())
|
||||||
@ -76,7 +75,7 @@ func fileComplete(query, ctx string, fields []string) ([]string, string) {
|
|||||||
q := splitForFile(ctx)
|
q := splitForFile(ctx)
|
||||||
path := ""
|
path := ""
|
||||||
if len(q) != 0 {
|
if len(q) != 0 {
|
||||||
path = q[len(q) - 1]
|
path = q[len(q)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
return matchPath(path)
|
return matchPath(path)
|
||||||
@ -86,7 +85,7 @@ func binaryComplete(query, ctx string, fields []string) ([]string, string) {
|
|||||||
q := splitForFile(ctx)
|
q := splitForFile(ctx)
|
||||||
query = ""
|
query = ""
|
||||||
if len(q) != 0 {
|
if len(q) != 0 {
|
||||||
query = q[len(q) - 1]
|
query = q[len(q)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
var completions []string
|
var completions []string
|
||||||
@ -111,7 +110,7 @@ func binaryComplete(query, ctx string, fields []string) ([]string, string) {
|
|||||||
// filter out executables, but in path
|
// filter out executables, but in path
|
||||||
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
for _, dir := range filepath.SplitList(os.Getenv("PATH")) {
|
||||||
// search for an executable which matches our query string
|
// 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
|
// get basename from matches
|
||||||
for _, match := range matches {
|
for _, match := range matches {
|
||||||
// check if we have execute permissions for our match
|
// check if we have execute permissions for our match
|
||||||
@ -159,7 +158,7 @@ func matchPath(query string) ([]string, string) {
|
|||||||
for _, entry := range files {
|
for _, entry := range files {
|
||||||
// should we handle errors here?
|
// should we handle errors here?
|
||||||
file, err := entry.Info()
|
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()))
|
path, err := filepath.EvalSymlinks(filepath.Join(path, file.Name()))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
file, err = os.Lstat(path)
|
file, err = os.Lstat(path)
|
||||||
@ -191,21 +190,19 @@ func escapeFilename(fname string) string {
|
|||||||
// #interface completion
|
// #interface completion
|
||||||
// tab completions
|
// tab completions
|
||||||
// The completions interface deals with tab completions.
|
// The completions interface deals with tab completions.
|
||||||
/*
|
func completionLoader(mlr *moonlight.Runtime) *moonlight.Table {
|
||||||
func completionLoader(rtm *rt.Runtime) *rt.Table {
|
exports := map[string]moonlight.Export{
|
||||||
exports := map[string]util.LuaExport{
|
|
||||||
"bins": {hcmpBins, 3, false},
|
"bins": {hcmpBins, 3, false},
|
||||||
"call": {hcmpCall, 4, false},
|
//"call": {hcmpCall, 4, false},
|
||||||
"files": {hcmpFiles, 3, false},
|
// "files": {hcmpFiles, 3, false},
|
||||||
"handler": {hcmpHandler, 2, false},
|
// "handler": {hcmpHandler, 2, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
mod := rt.NewTable()
|
mod := moonlight.NewTable()
|
||||||
util.SetExports(rtm, mod, exports)
|
mlr.SetExports(mod, exports)
|
||||||
|
|
||||||
return mod
|
return mod
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
// #interface completion
|
// #interface completion
|
||||||
// bins(query, ctx, fields) -> entries (table), prefix (string)
|
// bins(query, ctx, fields) -> entries (table), prefix (string)
|
||||||
@ -233,23 +230,23 @@ hilbish.complete('command.sudo', function(query, ctx, fields)
|
|||||||
end)
|
end)
|
||||||
#example
|
#example
|
||||||
*/
|
*/
|
||||||
/*
|
func hcmpBins(mlr *moonlight.Runtime) error {
|
||||||
func hcmpBins(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
query, ctx, fds, err := getCompleteParams(mlr)
|
||||||
query, ctx, fds, err := getCompleteParams(t, c)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
completions, pfx := binaryComplete(query, ctx, fds)
|
completions, _ := binaryComplete(query, ctx, fds)
|
||||||
luaComps := rt.NewTable()
|
luaComps := moonlight.NewTable()
|
||||||
|
|
||||||
for i, comp := range completions {
|
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
|
// #interface completion
|
||||||
// call(name, query, ctx, fields) -> completionGroups (table), prefix (string)
|
// call(name, query, ctx, fields) -> completionGroups (table), prefix (string)
|
||||||
@ -352,35 +349,33 @@ function hilbish.completion.handler(line, pos)
|
|||||||
end
|
end
|
||||||
#example
|
#example
|
||||||
*/
|
*/
|
||||||
/*
|
func hcmpHandler(mlr *moonlight.Runtime) error {
|
||||||
func hcmpHandler(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
return nil
|
||||||
return c.Next(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCompleteParams(t *rt.Thread, c *rt.GoCont) (string, string, []string, error) {
|
func getCompleteParams(mlr *moonlight.Runtime) (string, string, []string, error) {
|
||||||
if err := c.CheckNArgs(3); err != nil {
|
if err := mlr.CheckNArgs(3); err != nil {
|
||||||
return "", "", []string{}, err
|
return "", "", []string{}, err
|
||||||
}
|
}
|
||||||
query, err := c.StringArg(0)
|
query, err := mlr.StringArg(0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", []string{}, err
|
return "", "", []string{}, err
|
||||||
}
|
}
|
||||||
ctx, err := c.StringArg(1)
|
ctx, err := mlr.StringArg(1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", []string{}, err
|
return "", "", []string{}, err
|
||||||
}
|
}
|
||||||
fields, err := c.TableArg(2)
|
fields, err := mlr.TableArg(2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", []string{}, err
|
return "", "", []string{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var fds []string
|
var fds []string
|
||||||
util.ForEach(fields, func(k rt.Value, v rt.Value) {
|
moonlight.ForEach(fields, func(k moonlight.Value, v moonlight.Value) {
|
||||||
if v.Type() == rt.StringType {
|
if v.Type() == moonlight.StringType {
|
||||||
fds = append(fds, v.AsString())
|
fds = append(fds, v.AsString())
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return query, ctx, fds, err
|
return query, ctx, fds, err
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
package moonlight
|
package moonlight
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/aarzilli/golua/lua"
|
"github.com/aarzilli/golua/lua"
|
||||||
@ -36,6 +37,11 @@ func (mlr *Runtime) StringArg(num int) (string, error) {
|
|||||||
return mlr.state.CheckString(num + 1), nil
|
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 {
|
func (mlr *Runtime) Arg(c *GoCont, num int) Value {
|
||||||
return c.vals[num]
|
return c.vals[num]
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,8 @@ if not hilbish.midnightEdition then
|
|||||||
-- it didnt work normally, idk
|
-- it didnt work normally, idk
|
||||||
return function() return hilbish.module.load(path) end, path
|
return function() return hilbish.module.load(path) end, path
|
||||||
end)
|
end)
|
||||||
|
else
|
||||||
|
pcall = unsafe_pcall
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'nature.commands'
|
require 'nature.commands'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user