mirror of https://github.com/Hilbis/Hilbish
Compare commits
5 Commits
75b51075b8
...
b39083fe32
Author | SHA1 | Date |
---|---|---|
TorchedSammy | b39083fe32 | |
TorchedSammy | 0826839846 | |
TorchedSammy | 24d0b8ead7 | |
TorchedSammy | dd52fd2ad9 | |
TorchedSammy | 37cfae3423 |
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{a.luaAdd, 2, false},
|
||||
"add": util.LuaExport{hlalias, 2, false},
|
||||
"list": util.LuaExport{a.luaList, 0, false},
|
||||
"del": util.LuaExport{a.luaDelete, 1, false},
|
||||
}
|
||||
|
@ -80,24 +80,6 @@ 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,6 +4,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
@ -38,7 +39,7 @@ var exports = map[string]util.LuaExport{
|
|||
"inputMode": {hlinputMode, 1, false},
|
||||
"interval": {hlinterval, 2, false},
|
||||
"read": {hlread, 1, false},
|
||||
"run": {hlrun, 1, false},
|
||||
"run": {hlrun, 1, true},
|
||||
"timeout": {hltimeout, 2, false},
|
||||
"which": {hlwhich, 1, false},
|
||||
}
|
||||
|
@ -220,8 +221,10 @@ func unsetVimMode() {
|
|||
util.SetField(l, hshMod, "vimMode", rt.NilValue, "Current Vim mode of Hilbish (nil if not in Vim mode)")
|
||||
}
|
||||
|
||||
// run(cmd)
|
||||
// run(cmd, returnOut) -> exitCode, stdout, stderr
|
||||
// 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 {
|
||||
|
@ -231,8 +234,21 @@ 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
|
||||
err = execCommand(cmd)
|
||||
stdout, stderr, err := execCommand(cmd, terminalOut)
|
||||
|
||||
if code, ok := interp.IsExitStatus(err); ok {
|
||||
exitcode = code
|
||||
|
@ -240,7 +256,14 @@ func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|||
exitcode = 1
|
||||
}
|
||||
|
||||
return c.PushingNext1(t.Runtime, rt.IntValue(int64(exitcode))), nil
|
||||
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
|
||||
}
|
||||
|
||||
// cwd()
|
||||
|
|
34
exec.go
34
exec.go
|
@ -6,6 +6,7 @@ import (
|
|||
"errors"
|
||||
"os/exec"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
|
@ -13,7 +14,7 @@ import (
|
|||
"syscall"
|
||||
"time"
|
||||
|
||||
// "hilbish/util"
|
||||
"hilbish/util"
|
||||
|
||||
rt "github.com/arnodel/golua/runtime"
|
||||
"mvdan.cc/sh/v3/shell"
|
||||
|
@ -120,7 +121,7 @@ func handleLua(cmdString string) (uint8, error) {
|
|||
}
|
||||
|
||||
func handleSh(cmdString string) (uint8, error) {
|
||||
err := execCommand(cmdString)
|
||||
_, _, err := execCommand(cmdString, true)
|
||||
if err != nil {
|
||||
// If input is incomplete, start multiline prompting
|
||||
if syntax.IsIncomplete(err) {
|
||||
|
@ -129,7 +130,7 @@ func handleSh(cmdString string) (uint8, error) {
|
|||
if err != nil {
|
||||
break
|
||||
}
|
||||
err = execCommand(cmdString)
|
||||
_, _, err = execCommand(cmdString, true)
|
||||
if syntax.IsIncomplete(err) || strings.HasSuffix(cmdString, "\\") {
|
||||
continue
|
||||
} else if code, ok := interp.IsExitStatus(err); ok {
|
||||
|
@ -153,10 +154,16 @@ func handleSh(cmdString string) (uint8, error) {
|
|||
}
|
||||
|
||||
// Run command in sh interpreter
|
||||
func execCommand(cmd string) error {
|
||||
func execCommand(cmd string, terminalOut bool) (io.Writer, io.Writer, error) {
|
||||
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
||||
if err != nil {
|
||||
return err
|
||||
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)
|
||||
}
|
||||
|
||||
var bg bool
|
||||
|
@ -185,8 +192,7 @@ func execCommand(cmd string) 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)
|
||||
}
|
||||
|
||||
|
@ -194,7 +200,11 @@ func execCommand(cmd string) error {
|
|||
|
||||
if code, ok := luaexitcode.TryInt(); ok {
|
||||
exitcode = uint8(code)
|
||||
} // TODO: deregister commander if return isnt number
|
||||
} 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])
|
||||
}
|
||||
|
||||
return interp.NewExitStatus(exitcode)
|
||||
}
|
||||
|
@ -320,7 +330,7 @@ func execCommand(cmd string) error {
|
|||
}
|
||||
|
||||
runner, _ := interp.New(
|
||||
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
|
||||
interp.StdIO(os.Stdin, stdout, stderr),
|
||||
interp.ExecHandler(exechandle),
|
||||
)
|
||||
|
||||
|
@ -340,11 +350,11 @@ func execCommand(cmd string) error {
|
|||
|
||||
err = runner.Run(context.TODO(), stmt)
|
||||
if err != nil {
|
||||
return err
|
||||
return stdout, stderr, err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
return stdout, stderr, nil
|
||||
}
|
||||
|
||||
func lookpath(file string) error { // custom lookpath function so we know if a command is found *and* is executable
|
||||
|
@ -430,7 +440,7 @@ func cmdFinish(code uint8, cmdstr string, private bool) {
|
|||
if interactive && !private {
|
||||
handleHistory(cmdstr)
|
||||
}
|
||||
// util.SetField(l, hshMod, "exitCode", lua.LNumber(code), "Exit code of last exected command")
|
||||
util.SetField(l, hshMod, "exitCode", rt.IntValue(int64(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,21 +89,25 @@ 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)
|
||||
print(ansikit.print(text))
|
||||
io.write(ansikit.format(text) .. "\n")
|
||||
io.flush()
|
||||
return ansikit
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue