Compare commits

..

No commits in common. "b39083fe3265ee998867d5f4b3afa61e4748d5db" and "75b51075b8b7fc82d127467c9e8e3b9853da490e" have entirely different histories.

4 changed files with 36 additions and 55 deletions

View File

@ -69,7 +69,7 @@ func (a *aliasHandler) Resolve(cmdstr string) string {
func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table { func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table {
// create a lua module with our functions // create a lua module with our functions
hshaliasesLua := map[string]util.LuaExport{ hshaliasesLua := map[string]util.LuaExport{
"add": util.LuaExport{hlalias, 2, false}, "add": util.LuaExport{a.luaAdd, 2, false},
"list": util.LuaExport{a.luaList, 0, false}, "list": util.LuaExport{a.luaList, 0, false},
"del": util.LuaExport{a.luaDelete, 1, false}, "del": util.LuaExport{a.luaDelete, 1, false},
} }
@ -80,6 +80,24 @@ func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table {
return mod return mod
} }
func (a *aliasHandler) luaAdd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.CheckNArgs(2); err != nil {
return nil, err
}
alias, err := c.StringArg(0)
if err != nil {
return nil, err
}
cmd, err := c.StringArg(1)
if err != nil {
return nil, err
}
a.Add(alias, cmd)
return c.Next(), nil
}
func (a *aliasHandler) luaList(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (a *aliasHandler) luaList(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
aliasesList := rt.NewTable() aliasesList := rt.NewTable()
for k, v := range a.All() { for k, v := range a.All() {

31
api.go
View File

@ -4,7 +4,6 @@
package main package main
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"os" "os"
@ -39,7 +38,7 @@ var exports = map[string]util.LuaExport{
"inputMode": {hlinputMode, 1, false}, "inputMode": {hlinputMode, 1, false},
"interval": {hlinterval, 2, false}, "interval": {hlinterval, 2, false},
"read": {hlread, 1, false}, "read": {hlread, 1, false},
"run": {hlrun, 1, true}, "run": {hlrun, 1, false},
"timeout": {hltimeout, 2, false}, "timeout": {hltimeout, 2, false},
"which": {hlwhich, 1, false}, "which": {hlwhich, 1, false},
} }
@ -221,10 +220,8 @@ func unsetVimMode() {
util.SetField(l, hshMod, "vimMode", rt.NilValue, "Current Vim mode of Hilbish (nil if not in Vim mode)") util.SetField(l, hshMod, "vimMode", rt.NilValue, "Current Vim mode of Hilbish (nil if not in Vim mode)")
} }
// run(cmd, returnOut) -> exitCode, stdout, stderr // run(cmd)
// Runs `cmd` in Hilbish's sh interpreter. // Runs `cmd` in Hilbish's sh interpreter.
// If returnOut is true, the outputs of `cmd` will be returned as the 2nd and
// 3rd values instead of being outputted to the terminal.
// --- @param cmd string // --- @param cmd string
func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil { if err := c.Check1Arg(); err != nil {
@ -234,21 +231,8 @@ func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
var terminalOut bool
if len(c.Etc()) != 0 {
tout := c.Etc()[0]
termOut, ok := tout.TryBool()
terminalOut = termOut
if !ok {
return nil, errors.New("bad argument to run (expected boolean, got " + tout.TypeName() + ")")
}
} else {
terminalOut = true
}
var exitcode uint8 var exitcode uint8
stdout, stderr, err := execCommand(cmd, terminalOut) err = execCommand(cmd)
if code, ok := interp.IsExitStatus(err); ok { if code, ok := interp.IsExitStatus(err); ok {
exitcode = code exitcode = code
@ -256,14 +240,7 @@ func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
exitcode = 1 exitcode = 1
} }
stdoutStr := "" return c.PushingNext1(t.Runtime, rt.IntValue(int64(exitcode))), nil
stderrStr := ""
if !terminalOut {
stdoutStr = stdout.(*bytes.Buffer).String()
stderrStr = stderr.(*bytes.Buffer).String()
}
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitcode)), rt.StringValue(stdoutStr), rt.StringValue(stderrStr)), nil
} }
// cwd() // cwd()

34
exec.go
View File

@ -6,7 +6,6 @@ import (
"errors" "errors"
"os/exec" "os/exec"
"fmt" "fmt"
"io"
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
@ -14,7 +13,7 @@ import (
"syscall" "syscall"
"time" "time"
"hilbish/util" // "hilbish/util"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
"mvdan.cc/sh/v3/shell" "mvdan.cc/sh/v3/shell"
@ -121,7 +120,7 @@ func handleLua(cmdString string) (uint8, error) {
} }
func handleSh(cmdString string) (uint8, error) { func handleSh(cmdString string) (uint8, error) {
_, _, err := execCommand(cmdString, true) err := execCommand(cmdString)
if err != nil { if err != nil {
// If input is incomplete, start multiline prompting // If input is incomplete, start multiline prompting
if syntax.IsIncomplete(err) { if syntax.IsIncomplete(err) {
@ -130,7 +129,7 @@ func handleSh(cmdString string) (uint8, error) {
if err != nil { if err != nil {
break break
} }
_, _, err = execCommand(cmdString, true) err = execCommand(cmdString)
if syntax.IsIncomplete(err) || strings.HasSuffix(cmdString, "\\") { if syntax.IsIncomplete(err) || strings.HasSuffix(cmdString, "\\") {
continue continue
} else if code, ok := interp.IsExitStatus(err); ok { } else if code, ok := interp.IsExitStatus(err); ok {
@ -154,16 +153,10 @@ func handleSh(cmdString string) (uint8, error) {
} }
// Run command in sh interpreter // Run command in sh interpreter
func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) { func execCommand(cmd string) error {
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "") file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
if err != nil { if err != nil {
return nil, nil, err return err
}
var stdout io.Writer = os.Stdout
var stderr io.Writer = os.Stderr
if !terminalOut {
stdout = new(bytes.Buffer)
stderr = new(bytes.Buffer)
} }
var bg bool var bg bool
@ -192,7 +185,8 @@ func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
if commands[args[0]] != nil { if commands[args[0]] != nil {
luaexitcode, err := rt.Call1(l.MainThread(), rt.FunctionValue(commands[args[0]]), rt.TableValue(luacmdArgs)) luaexitcode, err := rt.Call1(l.MainThread(), rt.FunctionValue(commands[args[0]]), rt.TableValue(luacmdArgs))
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Error in command:\n\n" + err.Error()) fmt.Fprintln(os.Stderr,
"Error in command:\n\n" + err.Error())
return interp.NewExitStatus(1) return interp.NewExitStatus(1)
} }
@ -200,11 +194,7 @@ func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
if code, ok := luaexitcode.TryInt(); ok { if code, ok := luaexitcode.TryInt(); ok {
exitcode = uint8(code) exitcode = uint8(code)
} else if luaexitcode != rt.NilValue { } // TODO: deregister commander if return isnt number
// deregister commander
delete(commands, args[0])
fmt.Fprintf(os.Stderr, "Commander did not return number for exit code. %s, you're fired.\n", args[0])
}
return interp.NewExitStatus(exitcode) return interp.NewExitStatus(exitcode)
} }
@ -330,7 +320,7 @@ func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
} }
runner, _ := interp.New( runner, _ := interp.New(
interp.StdIO(os.Stdin, stdout, stderr), interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
interp.ExecHandler(exechandle), interp.ExecHandler(exechandle),
) )
@ -350,11 +340,11 @@ func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
err = runner.Run(context.TODO(), stmt) err = runner.Run(context.TODO(), stmt)
if err != nil { if err != nil {
return stdout, stderr, err return err
} }
} }
return stdout, stderr, nil return nil
} }
func lookpath(file string) error { // custom lookpath function so we know if a command is found *and* is executable func lookpath(file string) error { // custom lookpath function so we know if a command is found *and* is executable
@ -440,7 +430,7 @@ func cmdFinish(code uint8, cmdstr string, private bool) {
if interactive && !private { if interactive && !private {
handleHistory(cmdstr) handleHistory(cmdstr)
} }
util.SetField(l, hshMod, "exitCode", rt.IntValue(int64(code)), "Exit code of last exected command") // util.SetField(l, hshMod, "exitCode", lua.LNumber(code), "Exit code of last exected command")
// using AsValue (to convert to lua type) on an interface which is an int // using AsValue (to convert to lua type) on an interface which is an int
// results in it being unknown in lua .... ???? // results in it being unknown in lua .... ????
// so we allow the hook handler to take lua runtime Values // so we allow the hook handler to take lua runtime Values

View File

@ -89,25 +89,21 @@ end
ansikit.print = function(text) ansikit.print = function(text)
io.write(ansikit.format(text)) io.write(ansikit.format(text))
io.flush()
return ansikit return ansikit
end end
ansikit.printCode = function(code, terminate) ansikit.printCode = function(code, terminate)
io.write(ansikit.getCode(code, terminate)) io.write(ansikit.getCode(code, terminate))
io.flush()
return ansikit return ansikit
end end
ansikit.printCSI = function(code, endc) ansikit.printCSI = function(code, endc)
io.write(ansikit.getCSI(code, endc)) io.write(ansikit.getCSI(code, endc))
io.flush()
return ansikit return ansikit
end end
ansikit.println = function(text) ansikit.println = function(text)
io.write(ansikit.format(text) .. "\n") print(ansikit.print(text))
io.flush()
return ansikit return ansikit
end end