2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-07-06 19:12:02 +00:00

Compare commits

..

No commits in common. "754bb6c9c28db85c7d28e2cab93f64e2eb6220ce" and "d01655e8035c9b2b89ff6cc3298074387f2715fc" have entirely different histories.

9 changed files with 91 additions and 135 deletions

38
api.go
View File

@ -44,7 +44,9 @@ 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},
@ -53,9 +55,7 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
"multiprompt": {hlmultiprompt, 1, false},
"prependPath": {hlprependPath, 1, false},
*/
"appendPath": {hlappendPath, 1, false},
"cwd": {hlcwd, 0, false},
"prompt": {hlprompt, 1, true},
"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(mlr)
//hshcomp := completionLoader(rtm)
// TODO: REMOVE "completion" AND ONLY USE "completions" WITH AN S
hshMod.SetField("completion", moonlight.TableValue(hshcomp))
hshMod.SetField("completions", moonlight.TableValue(hshcomp))
//mod.Set(rt.StringValue("completion"), rt.TableValue(hshcomp))
//mod.Set(rt.StringValue("completions"), rt.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) error {
func hlappendPath(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
if err := mlr.Check1Arg(); err != nil {
return err
return nil, err
}
arg := mlr.Arg(0)
arg := mlr.Arg(c, 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) error {
} else if moonlight.Type(arg) == moonlight.StringType {
appendPath(arg.AsString())
} else {
return errors.New("bad argument to appendPath (expected string or table, got " + arg.TypeName() + ")")
return nil, errors.New("bad argument to appendPath (expected string or table, got " + arg.TypeName() + ")")
}
return nil
return c.Next(), 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) error {
func hlrunnerMode(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
if err := mlr.Check1Arg(); err != nil {
return err
return nil, err
}
mode := mlr.Arg(0)
mode := mlr.Arg(c, 0)
switch moonlight.Type(mode) {
case moonlight.StringType:
@ -782,15 +782,15 @@ func hlrunnerMode(mlr *moonlight.Runtime) error {
case "hybrid", "hybridRev", "lua", "sh":
runnerMode = mode
default:
return errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.AsString())
return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.AsString())
}
case moonlight.FunctionType:
runnerMode = mode
default:
return errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName())
return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName())
}
return nil
return c.Next(), nil
}
// hinter(line, pos)

View File

@ -2,12 +2,13 @@ package main
import (
//"errors"
"os"
"path/filepath"
"strings"
"os"
"hilbish/moonlight"
"hilbish/util"
//rt "github.com/arnodel/golua/runtime"
)
var charEscapeMap = []string{
@ -33,9 +34,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]
}
}
@ -51,7 +52,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())
@ -75,7 +76,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)
@ -85,7 +86,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
@ -110,7 +111,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
@ -158,7 +159,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)
@ -190,19 +191,21 @@ func escapeFilename(fname string) string {
// #interface completion
// tab completions
// The completions interface deals with tab completions.
func completionLoader(mlr *moonlight.Runtime) *moonlight.Table {
exports := map[string]moonlight.Export{
/*
func completionLoader(rtm *rt.Runtime) *rt.Table {
exports := map[string]util.LuaExport{
"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 := moonlight.NewTable()
mlr.SetExports(mod, exports)
mod := rt.NewTable()
util.SetExports(rtm, mod, exports)
return mod
}
*/
// #interface completion
// bins(query, ctx, fields) -> entries (table), prefix (string)
@ -230,23 +233,23 @@ hilbish.complete('command.sudo', function(query, ctx, fields)
end)
#example
*/
func hcmpBins(mlr *moonlight.Runtime) error {
query, ctx, fds, err := getCompleteParams(mlr)
/*
func hcmpBins(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
query, ctx, fds, err := getCompleteParams(t, c)
if err != nil {
return err
return nil, err
}
completions, _ := binaryComplete(query, ctx, fds)
luaComps := moonlight.NewTable()
completions, pfx := binaryComplete(query, ctx, fds)
luaComps := rt.NewTable()
for i, comp := range completions {
luaComps.Set(moonlight.IntValue(int64(i+1)), moonlight.StringValue(comp))
luaComps.Set(rt.IntValue(int64(i + 1)), rt.StringValue(comp))
}
mlr.PushNext1(moonlight.TableValue(luaComps))
//return c.PushingNext(t.Runtime, rt.TableValue(luaComps), rt.StringValue(pfx)), nil
return nil
return c.PushingNext(t.Runtime, rt.TableValue(luaComps), rt.StringValue(pfx)), nil
}
*/
// #interface completion
// call(name, query, ctx, fields) -> completionGroups (table), prefix (string)
@ -349,33 +352,35 @@ function hilbish.completion.handler(line, pos)
end
#example
*/
func hcmpHandler(mlr *moonlight.Runtime) error {
return nil
/*
func hcmpHandler(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil
}
func getCompleteParams(mlr *moonlight.Runtime) (string, string, []string, error) {
if err := mlr.CheckNArgs(3); err != nil {
func getCompleteParams(t *rt.Thread, c *rt.GoCont) (string, string, []string, error) {
if err := c.CheckNArgs(3); err != nil {
return "", "", []string{}, err
}
query, err := mlr.StringArg(0)
query, err := c.StringArg(0)
if err != nil {
return "", "", []string{}, err
}
ctx, err := mlr.StringArg(1)
ctx, err := c.StringArg(1)
if err != nil {
return "", "", []string{}, err
}
fields, err := mlr.TableArg(2)
fields, err := c.TableArg(2)
if err != nil {
return "", "", []string{}, err
}
var fds []string
moonlight.ForEach(fields, func(k moonlight.Value, v moonlight.Value) {
if v.Type() == moonlight.StringType {
util.ForEach(fields, func(k rt.Value, v rt.Value) {
if v.Type() == rt.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) error {
func (b *Bait) bcatch(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
name, catcher, err := util.HandleStrCallback(mlr)
if err != nil {
return err
return nil, err
}
b.OnLua(name, catcher)
return nil
return c.Next(), nil
}
/*

View File

@ -2,6 +2,10 @@
package moonlight
import (
"fmt"
)
type Callable interface {
Continuation(*Runtime, Cont) Cont
}
@ -12,6 +16,8 @@ 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,7 +3,6 @@
package moonlight
import (
"errors"
"fmt"
"github.com/aarzilli/golua/lua"
@ -37,13 +36,8 @@ 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(num int) Value {
return NilValue
func (mlr *Runtime) Arg(c *GoCont, num int) Value {
return c.vals[num]
}
func (mlr *Runtime) GoFunction(fun GoToLuaFunc) *GoFunctionFunc {

View File

@ -20,22 +20,11 @@ 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(num int) Value {
func (mlr *Runtime) Arg(c *GoCont, num int) Value {
return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).Arg(num)
}

View File

@ -4,7 +4,6 @@ package moonlight
import (
"fmt"
"os"
"github.com/aarzilli/golua/lua"
)
@ -18,39 +17,9 @@ func NewRuntime() *Runtime {
L := lua.NewState()
L.OpenLibs()
mlr := &Runtime{
return &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,8 +18,6 @@ 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'
@ -66,7 +64,6 @@ do
})
end
--[[
do
local startSearchPath = hilbish.userDir.data .. '/hilbish/start/?/init.lua;'
.. hilbish.userDir.data .. '/hilbish/start/?.lua'
@ -83,7 +80,6 @@ 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,7 +1,5 @@
package main
import "hilbish/moonlight"
// #interface runner
// interactive command runner customization
/* The runner interface contains functions that allow the user to change
@ -44,11 +42,10 @@ 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},
}
@ -72,13 +69,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) error {
if err := mlr.Check1Arg(); err != nil {
return err
func shRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
if err := mlr.Check1Arg(c); err != nil {
return nil, err
}
cmd, err := mlr.StringArg(0)
cmd, err := mlr.StringArg(c, 0)
if err != nil {
return err
return nil, err
}
_, exitCode, cont, err := execSh(aliases.Resolve(cmd))
@ -92,8 +89,7 @@ func shRunner(mlr *moonlight.Runtime) error {
runnerRet.SetField("continue", moonlight.BoolValue(cont))
runnerRet.SetField("err", luaErr)
mlr.PushNext1(moonlight.TableValue(runnerRet))
return nil
return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil
}
// #interface runner
@ -101,13 +97,13 @@ func shRunner(mlr *moonlight.Runtime) error {
// 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) error {
if err := mlr.Check1Arg(); err != nil {
return err
func luaRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
if err := mlr.Check1Arg(c); err != nil {
return nil, err
}
cmd, err := mlr.StringArg(0)
cmd, err := mlr.StringArg(c, 0)
if err != nil {
return err
return nil, err
}
input, exitCode, err := handleLua(cmd)
@ -120,6 +116,7 @@ func luaRunner(mlr *moonlight.Runtime) error {
runnerRet.SetField("exitCode", moonlight.IntValue(int64(exitCode)))
runnerRet.SetField("err", luaErr)
mlr.PushNext1(moonlight.TableValue(runnerRet))
return nil
return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil
}
*/