mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "b39083fe3265ee998867d5f4b3afa61e4748d5db" and "75b51075b8b7fc82d127467c9e8e3b9853da490e" have entirely different histories.
b39083fe32
...
75b51075b8
20
aliases.go
20
aliases.go
|
@ -69,7 +69,7 @@ func (a *aliasHandler) Resolve(cmdstr string) string {
|
|||
func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table {
|
||||
// create a lua module with our functions
|
||||
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},
|
||||
"del": util.LuaExport{a.luaDelete, 1, false},
|
||||
}
|
||||
|
@ -80,6 +80,24 @@ func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table {
|
|||
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) {
|
||||
aliasesList := rt.NewTable()
|
||||
for k, v := range a.All() {
|
||||
|
|
31
api.go
31
api.go
|
@ -4,7 +4,6 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
@ -39,7 +38,7 @@ var exports = map[string]util.LuaExport{
|
|||
"inputMode": {hlinputMode, 1, false},
|
||||
"interval": {hlinterval, 2, false},
|
||||
"read": {hlread, 1, false},
|
||||
"run": {hlrun, 1, true},
|
||||
"run": {hlrun, 1, false},
|
||||
"timeout": {hltimeout, 2, 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)")
|
||||
}
|
||||
|
||||
// run(cmd, returnOut) -> exitCode, stdout, stderr
|
||||
// run(cmd)
|
||||
// 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
|
||||
func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
if err := c.Check1Arg(); err != nil {
|
||||
|
@ -234,21 +231,8 @@ func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|||
if err != nil {
|
||||
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
|
||||
stdout, stderr, err := execCommand(cmd, terminalOut)
|
||||
err = execCommand(cmd)
|
||||
|
||||
if code, ok := interp.IsExitStatus(err); ok {
|
||||
exitcode = code
|
||||
|
@ -256,14 +240,7 @@ func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|||
exitcode = 1
|
||||
}
|
||||
|
||||
stdoutStr := ""
|
||||
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
|
||||
return c.PushingNext1(t.Runtime, rt.IntValue(int64(exitcode))), nil
|
||||
}
|
||||
|
||||
// cwd()
|
||||
|
|
34
exec.go
34
exec.go
|
@ -6,7 +6,6 @@ import (
|
|||
"errors"
|
||||
"os/exec"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
@ -14,7 +13,7 @@ import (
|
|||
"syscall"
|
||||
"time"
|
||||
|
||||
"hilbish/util"
|
||||
// "hilbish/util"
|
||||
|
||||
rt "github.com/arnodel/golua/runtime"
|
||||
"mvdan.cc/sh/v3/shell"
|
||||
|
@ -121,7 +120,7 @@ func handleLua(cmdString string) (uint8, error) {
|
|||
}
|
||||
|
||||
func handleSh(cmdString string) (uint8, error) {
|
||||
_, _, err := execCommand(cmdString, true)
|
||||
err := execCommand(cmdString)
|
||||
if err != nil {
|
||||
// If input is incomplete, start multiline prompting
|
||||
if syntax.IsIncomplete(err) {
|
||||
|
@ -130,7 +129,7 @@ func handleSh(cmdString string) (uint8, error) {
|
|||
if err != nil {
|
||||
break
|
||||
}
|
||||
_, _, err = execCommand(cmdString, true)
|
||||
err = execCommand(cmdString)
|
||||
if syntax.IsIncomplete(err) || strings.HasSuffix(cmdString, "\\") {
|
||||
continue
|
||||
} else if code, ok := interp.IsExitStatus(err); ok {
|
||||
|
@ -154,16 +153,10 @@ func handleSh(cmdString string) (uint8, error) {
|
|||
}
|
||||
|
||||
// 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), "")
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
var stdout io.Writer = os.Stdout
|
||||
var stderr io.Writer = os.Stderr
|
||||
if !terminalOut {
|
||||
stdout = new(bytes.Buffer)
|
||||
stderr = new(bytes.Buffer)
|
||||
return err
|
||||
}
|
||||
|
||||
var bg bool
|
||||
|
@ -192,7 +185,8 @@ func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
|
|||
if commands[args[0]] != nil {
|
||||
luaexitcode, err := rt.Call1(l.MainThread(), rt.FunctionValue(commands[args[0]]), rt.TableValue(luacmdArgs))
|
||||
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)
|
||||
}
|
||||
|
||||
|
@ -200,11 +194,7 @@ func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
|
|||
|
||||
if code, ok := luaexitcode.TryInt(); ok {
|
||||
exitcode = uint8(code)
|
||||
} else if luaexitcode != rt.NilValue {
|
||||
// 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])
|
||||
}
|
||||
} // TODO: deregister commander if return isnt number
|
||||
|
||||
return interp.NewExitStatus(exitcode)
|
||||
}
|
||||
|
@ -330,7 +320,7 @@ func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
|
|||
}
|
||||
|
||||
runner, _ := interp.New(
|
||||
interp.StdIO(os.Stdin, stdout, stderr),
|
||||
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
|
||||
interp.ExecHandler(exechandle),
|
||||
)
|
||||
|
||||
|
@ -350,11 +340,11 @@ func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
|
|||
|
||||
err = runner.Run(context.TODO(), stmt)
|
||||
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
|
||||
|
@ -440,7 +430,7 @@ func cmdFinish(code uint8, cmdstr string, private bool) {
|
|||
if interactive && !private {
|
||||
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
|
||||
// results in it being unknown in lua .... ????
|
||||
// so we allow the hook handler to take lua runtime Values
|
||||
|
|
|
@ -89,25 +89,21 @@ end
|
|||
|
||||
ansikit.print = function(text)
|
||||
io.write(ansikit.format(text))
|
||||
io.flush()
|
||||
return ansikit
|
||||
end
|
||||
|
||||
ansikit.printCode = function(code, terminate)
|
||||
io.write(ansikit.getCode(code, terminate))
|
||||
io.flush()
|
||||
return ansikit
|
||||
end
|
||||
|
||||
ansikit.printCSI = function(code, endc)
|
||||
io.write(ansikit.getCSI(code, endc))
|
||||
io.flush()
|
||||
return ansikit
|
||||
end
|
||||
|
||||
ansikit.println = function(text)
|
||||
io.write(ansikit.format(text) .. "\n")
|
||||
io.flush()
|
||||
print(ansikit.print(text))
|
||||
return ansikit
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue