2022-12-15 04:00:54 +00:00
|
|
|
// the core Hilbish API
|
|
|
|
// The Hilbish module includes the core API, containing
|
|
|
|
// interfaces and functions which directly relate to shell functionality.
|
|
|
|
// #field ver The version of Hilbish
|
2023-07-11 01:56:35 +00:00
|
|
|
// #field goVersion The version of Go that Hilbish was compiled with
|
2022-12-15 04:00:54 +00:00
|
|
|
// #field user Username of the user
|
|
|
|
// #field host Hostname of the machine
|
|
|
|
// #field dataDir Directory for Hilbish data files, including the docs and default modules
|
|
|
|
// #field interactive Is Hilbish in an interactive shell?
|
|
|
|
// #field login Is Hilbish the login shell?
|
|
|
|
// #field vimMode Current Vim input mode of Hilbish (will be nil if not in Vim input mode)
|
2023-12-26 03:08:29 +00:00
|
|
|
// #field exitCode Exit code of the last executed command
|
2021-05-16 19:53:21 +00:00
|
|
|
package main
|
|
|
|
|
2021-05-16 21:13:28 +00:00
|
|
|
import (
|
2022-04-04 10:40:02 +00:00
|
|
|
"bytes"
|
|
|
|
"errors"
|
2024-07-19 20:54:15 +00:00
|
|
|
//"fmt"
|
2024-04-28 01:03:54 +00:00
|
|
|
"io"
|
2021-05-16 21:13:28 +00:00
|
|
|
"os"
|
2024-07-19 20:54:15 +00:00
|
|
|
//"os/exec"
|
2021-09-26 01:39:06 +00:00
|
|
|
"runtime"
|
2021-07-08 10:44:57 +00:00
|
|
|
"strings"
|
2024-07-19 20:54:15 +00:00
|
|
|
//"syscall"
|
|
|
|
//"time"
|
2021-05-16 21:13:28 +00:00
|
|
|
|
2024-07-19 20:54:15 +00:00
|
|
|
//"hilbish/util"
|
|
|
|
"hilbish/moonlight"
|
2021-10-16 19:40:08 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
rt "github.com/arnodel/golua/runtime"
|
2024-07-19 20:54:15 +00:00
|
|
|
//"github.com/arnodel/golua/lib/packagelib"
|
2024-04-28 01:03:54 +00:00
|
|
|
"github.com/arnodel/golua/lib/iolib"
|
2022-03-02 02:00:46 +00:00
|
|
|
"github.com/maxlandon/readline"
|
2021-05-16 21:13:28 +00:00
|
|
|
"mvdan.cc/sh/v3/interp"
|
|
|
|
)
|
|
|
|
|
2024-07-19 20:54:15 +00:00
|
|
|
var hshMod *moonlight.Table
|
|
|
|
|
|
|
|
func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
|
|
|
|
var exports = map[string]moonlight.Export{
|
|
|
|
"alias": {hlalias, 2, false},
|
|
|
|
"appendPath": {hlappendPath, 1, false},
|
2024-07-20 19:11:45 +00:00
|
|
|
/*
|
2024-07-19 20:54:15 +00:00
|
|
|
"complete": {hlcomplete, 2, false},
|
2024-07-20 15:47:59 +00:00
|
|
|
*/
|
2024-07-19 20:54:15 +00:00
|
|
|
"cwd": {hlcwd, 0, false},
|
2024-07-20 15:47:59 +00:00
|
|
|
/*
|
2024-07-19 20:54:15 +00:00
|
|
|
"exec": {hlexec, 1, false},
|
2024-07-20 18:49:43 +00:00
|
|
|
*/
|
2024-07-19 20:54:15 +00:00
|
|
|
"runnerMode": {hlrunnerMode, 1, false},
|
2024-07-20 18:49:43 +00:00
|
|
|
/*
|
2024-07-19 20:54:15 +00:00
|
|
|
"goro": {hlgoro, 1, true},
|
|
|
|
"highlighter": {hlhighlighter, 1, false},
|
|
|
|
"hinter": {hlhinter, 1, false},
|
|
|
|
"multiprompt": {hlmultiprompt, 1, false},
|
|
|
|
"prependPath": {hlprependPath, 1, false},
|
2024-07-20 16:12:06 +00:00
|
|
|
*/
|
2024-07-19 20:54:15 +00:00
|
|
|
"prompt": {hlprompt, 1, true},
|
2024-07-20 16:12:06 +00:00
|
|
|
/*
|
2024-07-19 20:54:15 +00:00
|
|
|
"inputMode": {hlinputMode, 1, false},
|
|
|
|
"interval": {hlinterval, 2, false},
|
|
|
|
"read": {hlread, 1, false},
|
|
|
|
"run": {hlrun, 1, true},
|
|
|
|
"timeout": {hltimeout, 2, false},
|
|
|
|
"which": {hlwhich, 1, false},
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
hshMod = moonlight.NewTable()
|
|
|
|
mlr.SetExports(hshMod, exports)
|
2021-05-16 21:13:28 +00:00
|
|
|
|
|
|
|
host, _ := os.Hostname()
|
2021-09-26 01:39:06 +00:00
|
|
|
username := curuser.Username
|
2021-12-31 17:25:53 +00:00
|
|
|
|
2021-09-26 01:39:06 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
|
|
|
|
}
|
2021-05-16 21:13:28 +00:00
|
|
|
|
2024-07-19 20:54:15 +00:00
|
|
|
hshMod.SetField("ver", moonlight.StringValue(getVersion()))
|
|
|
|
hshMod.SetField("goVersion", moonlight.StringValue(runtime.Version()))
|
|
|
|
hshMod.SetField("user", moonlight.StringValue(username))
|
|
|
|
hshMod.SetField("host", moonlight.StringValue(host))
|
|
|
|
hshMod.SetField("home", moonlight.StringValue(curuser.HomeDir))
|
|
|
|
hshMod.SetField("dataDir", moonlight.StringValue(dataDir))
|
|
|
|
hshMod.SetField("interactive", moonlight.BoolValue(interactive))
|
|
|
|
hshMod.SetField("login", moonlight.BoolValue(login))
|
|
|
|
hshMod.SetField("exitCode", moonlight.IntValue(0))
|
|
|
|
//util.SetField(rtm, mod, "vimMode", rt.NilValue)
|
2021-05-16 21:13:28 +00:00
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
// hilbish.userDir table
|
2024-07-20 14:53:49 +00:00
|
|
|
hshuser := userDirLoader()
|
|
|
|
hshMod.SetField("userDir", moonlight.TableValue(hshuser))
|
2022-03-05 19:59:00 +00:00
|
|
|
|
|
|
|
// hilbish.os table
|
2024-07-19 20:54:15 +00:00
|
|
|
//hshos := hshosLoader(rtm)
|
|
|
|
//mod.Set(rt.StringValue("os"), rt.TableValue(hshos))
|
2021-10-08 00:58:07 +00:00
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
// hilbish.aliases table
|
2024-07-20 18:15:26 +00:00
|
|
|
aliases = newAliases()
|
2024-07-19 20:54:15 +00:00
|
|
|
//aliasesModule := aliases.Loader(rtm)
|
|
|
|
//mod.Set(rt.StringValue("aliases"), rt.TableValue(aliasesModule))
|
2021-12-15 00:54:23 +00:00
|
|
|
|
2022-01-27 21:02:21 +00:00
|
|
|
// hilbish.history table
|
2024-07-19 20:54:15 +00:00
|
|
|
//historyModule := lr.Loader(rtm)
|
|
|
|
//mod.Set(rt.StringValue("history"), rt.TableValue(historyModule))
|
2022-01-27 21:02:21 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
// hilbish.completion table
|
2024-07-19 20:54:15 +00:00
|
|
|
//hshcomp := completionLoader(rtm)
|
2023-12-02 15:11:28 +00:00
|
|
|
// TODO: REMOVE "completion" AND ONLY USE "completions" WITH AN S
|
2024-07-19 20:54:15 +00:00
|
|
|
//mod.Set(rt.StringValue("completion"), rt.TableValue(hshcomp))
|
|
|
|
//mod.Set(rt.StringValue("completions"), rt.TableValue(hshcomp))
|
2022-03-05 19:59:00 +00:00
|
|
|
|
2022-03-20 23:10:12 +00:00
|
|
|
// hilbish.runner table
|
2024-07-20 18:49:43 +00:00
|
|
|
runnerModule := runnerModeLoader(mlr)
|
|
|
|
hshMod.SetField("runner", moonlight.TableValue(runnerModule))
|
2022-03-20 19:15:44 +00:00
|
|
|
|
2022-03-20 23:10:12 +00:00
|
|
|
// hilbish.jobs table
|
2024-07-20 18:15:26 +00:00
|
|
|
jobs = newJobHandler()
|
2024-07-19 20:54:15 +00:00
|
|
|
//jobModule := jobs.loader(rtm)
|
|
|
|
//mod.Set(rt.StringValue("jobs"), rt.TableValue(jobModule))
|
2022-05-14 00:43:40 +00:00
|
|
|
|
|
|
|
// hilbish.timers table
|
2024-07-20 18:15:26 +00:00
|
|
|
timers = newTimersModule()
|
2024-07-19 20:54:15 +00:00
|
|
|
//timersModule := timers.loader(rtm)
|
|
|
|
//mod.Set(rt.StringValue("timers"), rt.TableValue(timersModule))
|
2021-05-16 21:13:28 +00:00
|
|
|
|
2024-07-19 20:54:15 +00:00
|
|
|
//editorModule := editorLoader(rtm)
|
|
|
|
//mod.Set(rt.StringValue("editor"), rt.TableValue(editorModule))
|
2022-05-14 00:43:40 +00:00
|
|
|
|
2024-07-19 20:54:15 +00:00
|
|
|
//versionModule := rt.NewTable()
|
|
|
|
//util.SetField(rtm, versionModule, "branch", rt.StringValue(gitBranch))
|
|
|
|
//util.SetField(rtm, versionModule, "full", rt.StringValue(getVersion()))
|
|
|
|
//util.SetField(rtm, versionModule, "commit", rt.StringValue(gitCommit))
|
|
|
|
//util.SetField(rtm, versionModule, "release", rt.StringValue(releaseName))
|
|
|
|
//mod.Set(rt.StringValue("version"), rt.TableValue(versionModule))
|
2022-05-17 10:39:30 +00:00
|
|
|
|
2024-07-20 15:34:39 +00:00
|
|
|
// very meta
|
|
|
|
moduleModule := moduleLoader(mlr)
|
|
|
|
hshMod.SetField("module", moonlight.TableValue(moduleModule))
|
2023-11-12 00:39:31 +00:00
|
|
|
|
2024-07-19 20:54:15 +00:00
|
|
|
return moonlight.TableValue(hshMod)
|
2021-05-16 21:13:28 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
func getenv(key, fallback string) string {
|
|
|
|
value := os.Getenv(key)
|
|
|
|
if len(value) == 0 {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
2022-03-05 19:59:00 +00:00
|
|
|
|
2022-03-02 02:00:46 +00:00
|
|
|
func setVimMode(mode string) {
|
2024-07-21 15:37:08 +00:00
|
|
|
hshMod.SetField("vimMode", moonlight.StringValue(mode))
|
2022-08-17 22:01:32 +00:00
|
|
|
hooks.Emit("hilbish.vimMode", mode)
|
2022-03-02 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unsetVimMode() {
|
2024-07-21 15:37:08 +00:00
|
|
|
hshMod.SetField("vimMode", moonlight.NilValue)
|
2022-03-02 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
2024-04-28 01:03:54 +00:00
|
|
|
func handleStream(v rt.Value, strms *streams, errStream bool) error {
|
|
|
|
ud, ok := v.TryUserData()
|
|
|
|
if !ok {
|
|
|
|
return errors.New("expected metatable argument")
|
|
|
|
}
|
|
|
|
|
|
|
|
val := ud.Value()
|
|
|
|
var varstrm io.Writer
|
|
|
|
if f, ok := val.(*iolib.File); ok {
|
|
|
|
varstrm = f.Handle()
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, ok := val.(*sink); ok {
|
|
|
|
varstrm = f.writer
|
|
|
|
}
|
|
|
|
|
|
|
|
if varstrm == nil {
|
|
|
|
return errors.New("expected either a sink or file")
|
|
|
|
}
|
|
|
|
|
|
|
|
if errStream {
|
|
|
|
strms.stderr = varstrm
|
|
|
|
} else {
|
|
|
|
strms.stdout = varstrm
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// run(cmd, streams) -> exitCode (number), stdout (string), stderr (string)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Runs `cmd` in Hilbish's shell script interpreter.
|
2024-04-28 01:03:54 +00:00
|
|
|
// The `streams` parameter specifies the output and input streams the command should use.
|
|
|
|
// For example, to write command output to a sink.
|
|
|
|
// As a table, the caller can directly specify the standard output, error, and input
|
|
|
|
// streams of the command with the table keys `out`, `err`, and `input` respectively.
|
|
|
|
// As a boolean, it specifies whether the command should use standard output or return its output streams.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #param cmd string
|
2024-04-28 01:03:54 +00:00
|
|
|
// #param streams table|boolean
|
2023-12-26 03:08:29 +00:00
|
|
|
// #returns number, string, string
|
2024-04-28 01:03:54 +00:00
|
|
|
// #example
|
|
|
|
/*
|
|
|
|
// This code is the same as `ls -l | wc -l`
|
|
|
|
local fs = require 'fs'
|
|
|
|
local pr, pw = fs.pipe()
|
|
|
|
hilbish.run('ls -l', {
|
|
|
|
stdout = pw,
|
|
|
|
stderr = pw,
|
|
|
|
})
|
|
|
|
|
|
|
|
pw:close()
|
|
|
|
|
|
|
|
hilbish.run('wc -l', {
|
|
|
|
stdin = pr
|
|
|
|
})
|
|
|
|
*/
|
|
|
|
// #example
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2024-04-28 01:03:54 +00:00
|
|
|
// TODO: ON BREAKING RELEASE, DO NOT ACCEPT `streams` AS A BOOLEAN.
|
2022-04-04 10:40:02 +00:00
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmd, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-28 01:03:54 +00:00
|
|
|
strms := &streams{}
|
2022-04-04 10:40:02 +00:00
|
|
|
var terminalOut bool
|
|
|
|
if len(c.Etc()) != 0 {
|
|
|
|
tout := c.Etc()[0]
|
2024-04-28 01:03:54 +00:00
|
|
|
|
|
|
|
var ok bool
|
|
|
|
terminalOut, ok = tout.TryBool()
|
2022-04-04 10:40:02 +00:00
|
|
|
if !ok {
|
2024-04-28 01:03:54 +00:00
|
|
|
luastreams, ok := tout.TryTable()
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("bad argument to run (expected boolean or table, got " + tout.TypeName() + ")")
|
|
|
|
}
|
|
|
|
|
|
|
|
handleStream(luastreams.Get(rt.StringValue("out")), strms, false)
|
|
|
|
handleStream(luastreams.Get(rt.StringValue("err")), strms, true)
|
|
|
|
|
|
|
|
stdinstrm := luastreams.Get(rt.StringValue("input"))
|
|
|
|
if !stdinstrm.IsNil() {
|
|
|
|
ud, ok := stdinstrm.TryUserData()
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("bad type as run stdin stream (expected userdata as either sink or file, got " + stdinstrm.TypeName() + ")")
|
|
|
|
}
|
|
|
|
|
|
|
|
val := ud.Value()
|
|
|
|
var varstrm io.Reader
|
|
|
|
if f, ok := val.(*iolib.File); ok {
|
|
|
|
varstrm = f.Handle()
|
|
|
|
}
|
|
|
|
|
|
|
|
if f, ok := val.(*sink); ok {
|
|
|
|
varstrm = f.reader
|
|
|
|
}
|
|
|
|
|
|
|
|
if varstrm == nil {
|
|
|
|
return nil, errors.New("bad type as run stdin stream (expected userdata as either sink or file)")
|
|
|
|
}
|
|
|
|
|
|
|
|
strms.stdin = varstrm
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !terminalOut {
|
|
|
|
strms = &streams{
|
|
|
|
stdout: new(bytes.Buffer),
|
|
|
|
stderr: new(bytes.Buffer),
|
|
|
|
}
|
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 21:27:07 +00:00
|
|
|
var exitcode uint8
|
2024-04-28 01:03:54 +00:00
|
|
|
stdout, stderr, err := execCommand(cmd, strms)
|
2021-05-16 21:13:28 +00:00
|
|
|
|
|
|
|
if code, ok := interp.IsExitStatus(err); ok {
|
|
|
|
exitcode = code
|
|
|
|
} else if err != nil {
|
|
|
|
exitcode = 1
|
|
|
|
}
|
|
|
|
|
2024-04-28 01:03:54 +00:00
|
|
|
var stdoutStr, stderrStr string
|
|
|
|
if stdoutBuf, ok := stdout.(*bytes.Buffer); ok {
|
|
|
|
stdoutStr = stdoutBuf.String()
|
|
|
|
}
|
|
|
|
if stderrBuf, ok := stderr.(*bytes.Buffer); ok {
|
|
|
|
stderrStr = stderrBuf.String()
|
2022-04-04 10:40:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitcode)), rt.StringValue(stdoutStr), rt.StringValue(stderrStr)), nil
|
2021-05-16 19:53:21 +00:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:39:26 +00:00
|
|
|
// cwd() -> string
|
2023-12-26 03:08:29 +00:00
|
|
|
// Returns the current directory of the shell.
|
|
|
|
// #returns string
|
2024-07-20 15:47:59 +00:00
|
|
|
func hlcwd(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
2021-05-27 23:06:17 +00:00
|
|
|
cwd, _ := os.Getwd()
|
|
|
|
|
2024-07-20 15:47:59 +00:00
|
|
|
return mlr.PushNext1(c, moonlight.StringValue(cwd)), nil
|
2021-05-27 23:06:17 +00:00
|
|
|
}
|
|
|
|
|
2021-10-30 23:53:42 +00:00
|
|
|
|
2022-12-21 00:59:55 +00:00
|
|
|
// read(prompt) -> input (string)
|
2021-10-30 23:53:42 +00:00
|
|
|
// Read input from the user, using Hilbish's line editor/input reader.
|
|
|
|
// This is a separate instance from the one Hilbish actually uses.
|
2023-12-26 03:08:29 +00:00
|
|
|
// Returns `input`, will be nil if Ctrl-D is pressed, or an error occurs.
|
|
|
|
// #param prompt? string Text to print before input, can be empty.
|
|
|
|
// #returns string|nil
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlread(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-10-10 22:40:29 +00:00
|
|
|
luaprompt := c.Arg(0)
|
|
|
|
if typ := luaprompt.Type(); typ != rt.StringType && typ != rt.NilType {
|
|
|
|
return nil, errors.New("expected #1 to be a string")
|
2022-04-04 10:40:02 +00:00
|
|
|
}
|
2022-10-10 22:40:29 +00:00
|
|
|
prompt, ok := luaprompt.TryString()
|
|
|
|
if !ok {
|
|
|
|
// if we are here and `luaprompt` is not a string, it's nil
|
|
|
|
// substitute with an empty string
|
|
|
|
prompt = ""
|
2022-04-04 10:40:02 +00:00
|
|
|
}
|
2022-10-10 22:40:29 +00:00
|
|
|
|
2022-10-10 22:26:54 +00:00
|
|
|
lualr := &lineReader{
|
|
|
|
rl: readline.NewInstance(),
|
|
|
|
}
|
2022-10-10 22:40:29 +00:00
|
|
|
lualr.SetPrompt(prompt)
|
2021-10-30 23:53:42 +00:00
|
|
|
|
|
|
|
input, err := lualr.Read()
|
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2021-10-30 23:53:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.PushingNext1(t.Runtime, rt.StringValue(input)), nil
|
2021-10-30 23:53:42 +00:00
|
|
|
}
|
|
|
|
|
2022-02-26 15:36:04 +00:00
|
|
|
/*
|
2022-12-15 04:00:54 +00:00
|
|
|
prompt(str, typ)
|
2023-12-26 03:08:29 +00:00
|
|
|
Changes the shell prompt to the provided string.
|
2022-01-26 19:51:52 +00:00
|
|
|
There are a few verbs that can be used in the prompt text.
|
|
|
|
These will be formatted and replaced with the appropriate values.
|
|
|
|
`%d` - Current working directory
|
|
|
|
`%u` - Name of current user
|
2022-02-26 15:36:04 +00:00
|
|
|
`%h` - Hostname of device
|
2023-12-26 03:08:29 +00:00
|
|
|
#param str string
|
|
|
|
#param typ? string Type of prompt, being left or right. Left by default.
|
|
|
|
#example
|
|
|
|
-- the default hilbish prompt without color
|
|
|
|
hilbish.prompt '%u %d ∆'
|
|
|
|
-- or something of old:
|
|
|
|
hilbish.prompt '%u@%h :%d $'
|
|
|
|
-- prompt: user@hostname: ~/directory $
|
|
|
|
#example
|
2022-02-26 15:36:04 +00:00
|
|
|
*/
|
2024-07-20 16:12:06 +00:00
|
|
|
func hlprompt(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
|
|
|
err := mlr.Check1Arg(c)
|
2022-04-13 14:13:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-04-04 10:40:02 +00:00
|
|
|
}
|
2024-07-20 16:12:06 +00:00
|
|
|
p, err := mlr.StringArg(c, 0)
|
2022-04-04 10:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-13 14:13:46 +00:00
|
|
|
typ := "left"
|
2022-04-18 03:39:53 +00:00
|
|
|
// optional 2nd arg
|
2024-07-20 16:12:06 +00:00
|
|
|
/*
|
2022-04-13 14:13:46 +00:00
|
|
|
if len(c.Etc()) != 0 {
|
|
|
|
ltyp := c.Etc()[0]
|
|
|
|
var ok bool
|
|
|
|
typ, ok = ltyp.TryString()
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("bad argument to run (expected string, got " + ltyp.TypeName() + ")")
|
|
|
|
}
|
|
|
|
}
|
2024-07-20 16:12:06 +00:00
|
|
|
*/
|
2022-04-13 14:13:46 +00:00
|
|
|
|
|
|
|
switch typ {
|
|
|
|
case "left":
|
|
|
|
prompt = p
|
|
|
|
lr.SetPrompt(fmtPrompt(prompt))
|
|
|
|
case "right": lr.SetRightPrompt(fmtPrompt(p))
|
|
|
|
default: return nil, errors.New("expected prompt type to be right or left, got " + typ)
|
|
|
|
}
|
2022-01-26 19:51:52 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// multiprompt(str)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Changes the text prompt when Hilbish asks for more input.
|
|
|
|
// This will show up when text is incomplete, like a missing quote
|
|
|
|
// #param str string
|
|
|
|
/*
|
|
|
|
#example
|
|
|
|
--[[
|
|
|
|
imagine this is your text input:
|
|
|
|
user ~ ∆ echo "hey
|
|
|
|
|
|
|
|
but there's a missing quote! hilbish will now prompt you so the terminal
|
|
|
|
will look like:
|
|
|
|
user ~ ∆ echo "hey
|
|
|
|
--> ...!"
|
|
|
|
|
|
|
|
so then you get
|
|
|
|
user ~ ∆ echo "hey
|
|
|
|
--> ...!"
|
|
|
|
hey ...!
|
|
|
|
]]--
|
|
|
|
hilbish.multiprompt '-->'
|
|
|
|
#example
|
|
|
|
*/
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlmultiprompt(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
prompt, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
multilinePrompt = prompt
|
2022-01-26 19:51:52 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// alias(cmd, orig)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Sets an alias, with a name of `cmd` to another command.
|
|
|
|
// #param cmd string Name of the alias
|
|
|
|
// #param orig string Command that will be aliased
|
|
|
|
/*
|
|
|
|
#example
|
|
|
|
-- With this, "ga file" will turn into "git add file"
|
|
|
|
hilbish.alias('ga', 'git add')
|
|
|
|
|
|
|
|
-- Numbered substitutions are supported here!
|
|
|
|
hilbish.alias('dircount', 'ls %1 | wc -l')
|
|
|
|
-- "dircount ~" would count how many files are in ~ (home directory).
|
|
|
|
#example
|
|
|
|
*/
|
2024-07-19 20:54:15 +00:00
|
|
|
//func hlalias(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
func hlalias(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
|
|
|
if err := mlr.CheckNArgs(c, 2); err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-19 20:54:15 +00:00
|
|
|
|
|
|
|
cmd, err := mlr.StringArg(c, 0)
|
2022-04-04 10:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-19 20:54:15 +00:00
|
|
|
orig, err := mlr.StringArg(c, 1)
|
2022-04-04 10:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-26 19:51:52 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
aliases.Add(cmd, orig)
|
2022-01-26 19:51:52 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// appendPath(dir)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Appends the provided dir to the command path (`$PATH`)
|
|
|
|
// #param dir string|table Directory (or directories) to append to path
|
|
|
|
/*
|
|
|
|
#example
|
|
|
|
hilbish.appendPath '~/go/bin'
|
|
|
|
-- Will add ~/go/bin to the command path.
|
|
|
|
|
|
|
|
-- Or do multiple:
|
|
|
|
hilbish.appendPath {
|
|
|
|
'~/go/bin',
|
|
|
|
'~/.local/bin'
|
|
|
|
}
|
|
|
|
#example
|
2024-07-20 19:11:45 +00:00
|
|
|
*/
|
|
|
|
func hlappendPath(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
|
|
|
if err := mlr.Check1Arg(c); err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-20 19:11:45 +00:00
|
|
|
arg := mlr.Arg(c, 0)
|
2022-04-04 10:40:02 +00:00
|
|
|
|
2022-01-26 19:51:52 +00:00
|
|
|
// check if dir is a table or a string
|
2024-07-20 19:11:45 +00:00
|
|
|
if moonlight.Type(arg) == moonlight.TableType {
|
|
|
|
moonlight.ForEach(moonlight.ToTable(arg), func(_ moonlight.Value, v moonlight.Value) {
|
|
|
|
if moonlight.Type(v) == moonlight.StringType {
|
|
|
|
appendPath(moonlight.ToString(v))
|
2022-04-04 10:40:02 +00:00
|
|
|
}
|
2022-04-21 18:01:59 +00:00
|
|
|
})
|
2024-07-20 19:11:45 +00:00
|
|
|
} else if moonlight.Type(arg) == moonlight.StringType {
|
2022-04-04 10:40:02 +00:00
|
|
|
appendPath(arg.AsString())
|
2022-01-26 19:51:52 +00:00
|
|
|
} else {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, errors.New("bad argument to appendPath (expected string or table, got " + arg.TypeName() + ")")
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func appendPath(dir string) {
|
|
|
|
dir = strings.Replace(dir, "~", curuser.HomeDir, 1)
|
|
|
|
pathenv := os.Getenv("PATH")
|
|
|
|
|
|
|
|
// if dir isnt already in $PATH, add it
|
|
|
|
if !strings.Contains(pathenv, dir) {
|
|
|
|
os.Setenv("PATH", pathenv + string(os.PathListSeparator) + dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-20 19:11:45 +00:00
|
|
|
/*
|
2022-01-26 19:51:52 +00:00
|
|
|
// exec(cmd)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Replaces the currently running Hilbish instance with the supplied command.
|
|
|
|
// This can be used to do an in-place restart.
|
|
|
|
// #param cmd string
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlexec(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cmd, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-26 19:51:52 +00:00
|
|
|
cmdArgs, _ := splitInput(cmd)
|
2022-03-02 02:14:55 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
cmdPath, err := exec.LookPath(cmdArgs[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
// if we get here, cmdPath will be nothing
|
|
|
|
// therefore nothing will run
|
|
|
|
}
|
|
|
|
|
|
|
|
// syscall.Exec requires an absolute path to a binary
|
|
|
|
// path, args, string slice of environments
|
|
|
|
syscall.Exec(cmdPath, cmdArgs, os.Environ())
|
|
|
|
} else {
|
|
|
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Run()
|
|
|
|
os.Exit(0)
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
2024-07-19 20:54:15 +00:00
|
|
|
*/
|
2022-01-26 19:51:52 +00:00
|
|
|
|
2024-07-19 20:54:15 +00:00
|
|
|
/*
|
2022-01-26 19:51:52 +00:00
|
|
|
// goro(fn)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Puts `fn` in a Goroutine.
|
|
|
|
// This can be used to run any function in another thread at the same time as other Lua code.
|
|
|
|
// **NOTE: THIS FUNCTION MAY CRASH HILBISH IF OUTSIDE VARIABLES ARE ACCESSED.**
|
|
|
|
// **This is a limitation of the Lua runtime.**
|
|
|
|
// #param fn function
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlgoro(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fn, err := c.ClosureArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// call fn
|
|
|
|
go func() {
|
2023-12-26 16:32:32 +00:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
// do something here?
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
_, err := rt.Call1(l.MainThread(), rt.FunctionValue(fn), c.Etc()...)
|
|
|
|
if err != nil {
|
2022-03-05 02:21:34 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Error in goro function:\n\n", err)
|
|
|
|
}
|
2022-01-26 19:51:52 +00:00
|
|
|
}()
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:39:26 +00:00
|
|
|
// timeout(cb, time) -> @Timer
|
2023-12-26 03:08:29 +00:00
|
|
|
// Executed the `cb` function after a period of `time`.
|
|
|
|
// This creates a Timer that starts ticking immediately.
|
|
|
|
// #param cb function
|
|
|
|
// #param time number Time to run in milliseconds.
|
|
|
|
// #returns Timer
|
2022-04-04 10:40:02 +00:00
|
|
|
func hltimeout(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.CheckNArgs(2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cb, err := c.ClosureArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ms, err := c.IntArg(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-26 19:51:52 +00:00
|
|
|
|
2022-04-12 23:28:25 +00:00
|
|
|
interval := time.Duration(ms) * time.Millisecond
|
|
|
|
timer := timers.create(timerTimeout, interval, cb)
|
|
|
|
timer.start()
|
|
|
|
|
2022-05-28 23:03:44 +00:00
|
|
|
return c.PushingNext1(t.Runtime, rt.UserDataValue(timer.ud)), nil
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
2023-01-18 10:39:26 +00:00
|
|
|
// interval(cb, time) -> @Timer
|
2023-12-26 03:08:29 +00:00
|
|
|
// Runs the `cb` function every specified amount of `time`.
|
|
|
|
// This creates a timer that ticking immediately.
|
|
|
|
// #param cb function
|
|
|
|
// #param time number Time in milliseconds.
|
|
|
|
// #return Timer
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlinterval(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.CheckNArgs(2); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
cb, err := c.ClosureArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ms, err := c.IntArg(1)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-26 19:51:52 +00:00
|
|
|
|
2022-04-12 23:28:25 +00:00
|
|
|
interval := time.Duration(ms) * time.Millisecond
|
2022-04-12 23:41:50 +00:00
|
|
|
timer := timers.create(timerInterval, interval, cb)
|
2022-04-12 23:28:25 +00:00
|
|
|
timer.start()
|
2022-01-26 19:51:52 +00:00
|
|
|
|
2022-05-28 23:03:44 +00:00
|
|
|
return c.PushingNext1(t.Runtime, rt.UserDataValue(timer.ud)), nil
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
2024-07-19 20:54:15 +00:00
|
|
|
*/
|
2022-01-26 19:51:52 +00:00
|
|
|
|
|
|
|
// complete(scope, cb)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Registers a completion handler for the specified scope.
|
|
|
|
// A `scope` is expected to be `command.<cmd>`,
|
2022-01-26 19:51:52 +00:00
|
|
|
// replacing <cmd> with the name of the command (for example `command.git`).
|
2023-12-26 03:08:29 +00:00
|
|
|
// The documentation for completions, under Features/Completions or `doc completions`
|
|
|
|
// provides more details.
|
|
|
|
// #param scope string
|
|
|
|
// #param cb function
|
|
|
|
/*
|
|
|
|
#example
|
|
|
|
-- This is a very simple example. Read the full doc for completions for details.
|
|
|
|
hilbish.complete('command.sudo', function(query, ctx, fields)
|
|
|
|
if #fields == 0 then
|
|
|
|
-- complete for commands
|
|
|
|
local comps, pfx = hilbish.completion.bins(query, ctx, fields)
|
|
|
|
local compGroup = {
|
|
|
|
items = comps, -- our list of items to complete
|
|
|
|
type = 'grid' -- what our completions will look like.
|
|
|
|
}
|
|
|
|
|
|
|
|
return {compGroup}, pfx
|
|
|
|
end
|
|
|
|
|
|
|
|
-- otherwise just be boring and return files
|
|
|
|
|
|
|
|
local comps, pfx = hilbish.completion.files(query, ctx, fields)
|
|
|
|
local compGroup = {
|
|
|
|
items = comps,
|
|
|
|
type = 'grid'
|
|
|
|
}
|
|
|
|
|
|
|
|
return {compGroup}, pfx
|
|
|
|
end)
|
|
|
|
#example
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlcomplete(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
scope, cb, err := util.HandleStrCallback(t, c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-26 19:51:52 +00:00
|
|
|
luaCompletions[scope] = cb
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
2024-07-19 20:54:15 +00:00
|
|
|
*/
|
2022-01-26 19:51:52 +00:00
|
|
|
|
2024-07-19 20:54:15 +00:00
|
|
|
/*
|
2022-01-26 19:51:52 +00:00
|
|
|
// prependPath(dir)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Prepends `dir` to $PATH.
|
|
|
|
// #param dir string
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlprependPath(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
dir, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-01-26 19:51:52 +00:00
|
|
|
dir = strings.Replace(dir, "~", curuser.HomeDir, 1)
|
|
|
|
pathenv := os.Getenv("PATH")
|
|
|
|
|
|
|
|
// if dir isnt already in $PATH, add in
|
|
|
|
if !strings.Contains(pathenv, dir) {
|
|
|
|
os.Setenv("PATH", dir + string(os.PathListSeparator) + pathenv)
|
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
2022-03-01 22:59:44 +00:00
|
|
|
|
2023-01-18 10:39:26 +00:00
|
|
|
// which(name) -> string
|
|
|
|
// Checks if `name` is a valid command.
|
|
|
|
// Will return the path of the binary, or a basename if it's a commander.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #param name string
|
|
|
|
// #returns string
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlwhich(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-22 02:16:04 +00:00
|
|
|
name, err := c.StringArg(0)
|
2022-04-04 10:40:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-04-22 02:16:04 +00:00
|
|
|
|
2023-01-18 10:52:22 +00:00
|
|
|
// itll return either the original command or what was passed
|
|
|
|
// if name isnt empty its not an issue
|
|
|
|
alias := aliases.Resolve(name)
|
|
|
|
cmd := strings.Split(alias, " ")[0]
|
2022-04-22 02:16:04 +00:00
|
|
|
|
|
|
|
// check for commander
|
2024-04-28 01:29:06 +00:00
|
|
|
if cmds.Commands[cmd] != nil {
|
2022-04-22 02:16:04 +00:00
|
|
|
// they dont resolve to a path, so just send the cmd
|
|
|
|
return c.PushingNext1(t.Runtime, rt.StringValue(cmd)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path, err := exec.LookPath(cmd)
|
2022-03-01 22:59:44 +00:00
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-03-01 22:59:44 +00:00
|
|
|
}
|
2022-03-05 19:46:38 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.PushingNext1(t.Runtime, rt.StringValue(path)), nil
|
2022-03-01 22:59:44 +00:00
|
|
|
}
|
2022-03-02 02:00:46 +00:00
|
|
|
|
|
|
|
// inputMode(mode)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Sets the input mode for Hilbish's line reader.
|
|
|
|
// `emacs` is the default. Setting it to `vim` changes behavior of input to be
|
|
|
|
// Vim-like with modes and Vim keybinds.
|
|
|
|
// #param mode string Can be set to either `emacs` or `vim`
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlinputMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mode, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-02 02:00:46 +00:00
|
|
|
switch mode {
|
|
|
|
case "emacs":
|
|
|
|
unsetVimMode()
|
|
|
|
lr.rl.InputMode = readline.Emacs
|
|
|
|
case "vim":
|
|
|
|
setVimMode("insert")
|
|
|
|
lr.rl.InputMode = readline.Vim
|
2022-04-04 10:40:02 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.New("inputMode: expected vim or emacs, received " + mode)
|
2022-03-02 02:00:46 +00:00
|
|
|
}
|
2022-04-04 10:40:02 +00:00
|
|
|
|
|
|
|
return c.Next(), nil
|
2022-03-02 02:00:46 +00:00
|
|
|
}
|
2024-07-20 18:49:43 +00:00
|
|
|
*/
|
2022-03-20 19:15:44 +00:00
|
|
|
|
|
|
|
// runnerMode(mode)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Sets the execution/runner mode for interactive Hilbish.
|
|
|
|
// This determines whether Hilbish wll try to run input as Lua
|
|
|
|
// and/or sh or only do one of either.
|
2022-03-20 19:15:44 +00:00
|
|
|
// Accepted values for mode are hybrid (the default), hybridRev (sh first then Lua),
|
|
|
|
// sh, and lua. It also accepts a function, to which if it is passed one
|
|
|
|
// will call it to execute user input instead.
|
2023-12-26 03:08:29 +00:00
|
|
|
// Read [about runner mode](../features/runner-mode) for more information.
|
|
|
|
// #param mode string|function
|
2024-07-20 18:49:43 +00:00
|
|
|
func hlrunnerMode(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
|
|
|
if err := mlr.Check1Arg(c); err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-07-20 18:49:43 +00:00
|
|
|
mode := mlr.Arg(c, 0)
|
2022-04-04 10:40:02 +00:00
|
|
|
|
2024-07-20 18:49:43 +00:00
|
|
|
switch moonlight.Type(mode) {
|
|
|
|
case moonlight.StringType:
|
2022-04-04 10:40:02 +00:00
|
|
|
switch mode.AsString() {
|
|
|
|
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())
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
2024-07-20 18:49:43 +00:00
|
|
|
case moonlight.FunctionType: runnerMode = mode
|
2022-04-04 10:40:02 +00:00
|
|
|
default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName())
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.Next(), nil
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
2022-03-26 21:34:09 +00:00
|
|
|
|
2022-04-22 02:19:27 +00:00
|
|
|
// hinter(line, pos)
|
|
|
|
// The command line hint handler. It gets called on every key insert to
|
|
|
|
// determine what text to use as an inline hint. It is passed the current
|
|
|
|
// line and cursor position. It is expected to return a string which is used
|
|
|
|
// as the text for the hint. This is by default a shim. To set hints,
|
|
|
|
// override this function with your custom handler.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #param line string
|
|
|
|
// #param pos number Position of cursor in line. Usually equals string.len(line)
|
|
|
|
/*
|
|
|
|
#example
|
|
|
|
-- this will display "hi" after the cursor in a dimmed color.
|
|
|
|
function hilbish.hinter(line, pos)
|
|
|
|
return 'hi'
|
|
|
|
end
|
|
|
|
#example
|
|
|
|
*/
|
2024-07-19 20:54:15 +00:00
|
|
|
/*
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlhinter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2022-04-22 02:19:27 +00:00
|
|
|
return c.Next(), nil
|
2022-03-26 21:34:09 +00:00
|
|
|
}
|
2022-03-26 22:25:19 +00:00
|
|
|
|
2022-04-22 02:19:27 +00:00
|
|
|
// highlighter(line)
|
2023-12-26 03:08:29 +00:00
|
|
|
// Line highlighter handler.
|
|
|
|
// This is mainly for syntax highlighting, but in reality could set the input
|
|
|
|
// of the prompt to *display* anything. The callback is passed the current line
|
|
|
|
// and is expected to return a line that will be used as the input display.
|
2023-04-15 14:36:21 +00:00
|
|
|
// Note that to set a highlighter, one has to override this function.
|
2023-12-26 03:08:29 +00:00
|
|
|
// #example
|
|
|
|
// --This code will highlight all double quoted strings in green.
|
2023-04-15 14:36:21 +00:00
|
|
|
// function hilbish.highlighter(line)
|
|
|
|
// return line:gsub('"%w+"', function(c) return lunacolors.green(c) end)
|
|
|
|
// end
|
2023-12-26 03:08:29 +00:00
|
|
|
// #example
|
|
|
|
// #param line string
|
2022-04-04 10:40:02 +00:00
|
|
|
func hlhighlighter(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
2024-04-19 11:54:46 +00:00
|
|
|
if err := c.Check1Arg(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
line, err := c.StringArg(0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.PushingNext1(t.Runtime, rt.StringValue(line)), nil
|
2022-03-26 22:25:19 +00:00
|
|
|
}
|
2024-07-19 20:54:15 +00:00
|
|
|
*/
|