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 {
|
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{a.luaAdd, 2, false},
|
"add": util.LuaExport{hlalias, 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,24 +80,6 @@ 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
31
api.go
|
@ -4,6 +4,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
@ -38,7 +39,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, false},
|
"run": {hlrun, 1, true},
|
||||||
"timeout": {hltimeout, 2, false},
|
"timeout": {hltimeout, 2, false},
|
||||||
"which": {hlwhich, 1, 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)")
|
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.
|
// 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 {
|
||||||
|
@ -231,8 +234,21 @@ 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
|
||||||
err = execCommand(cmd)
|
stdout, stderr, err := execCommand(cmd, terminalOut)
|
||||||
|
|
||||||
if code, ok := interp.IsExitStatus(err); ok {
|
if code, ok := interp.IsExitStatus(err); ok {
|
||||||
exitcode = code
|
exitcode = code
|
||||||
|
@ -240,7 +256,14 @@ func hlrun(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
exitcode = 1
|
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()
|
// cwd()
|
||||||
|
|
34
exec.go
34
exec.go
|
@ -6,6 +6,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
@ -13,7 +14,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"
|
||||||
|
@ -120,7 +121,7 @@ func handleLua(cmdString string) (uint8, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func handleSh(cmdString string) (uint8, error) {
|
func handleSh(cmdString string) (uint8, error) {
|
||||||
err := execCommand(cmdString)
|
_, _, err := execCommand(cmdString, true)
|
||||||
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) {
|
||||||
|
@ -129,7 +130,7 @@ func handleSh(cmdString string) (uint8, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
err = execCommand(cmdString)
|
_, _, err = execCommand(cmdString, true)
|
||||||
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 {
|
||||||
|
@ -153,10 +154,16 @@ func handleSh(cmdString string) (uint8, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run command in sh interpreter
|
// 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), "")
|
file, err := syntax.NewParser().Parse(strings.NewReader(cmd), "")
|
||||||
if err != nil {
|
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
|
var bg bool
|
||||||
|
@ -185,8 +192,7 @@ func execCommand(cmd string) 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,
|
fmt.Fprintln(os.Stderr, "Error in command:\n\n" + err.Error())
|
||||||
"Error in command:\n\n" + err.Error())
|
|
||||||
return interp.NewExitStatus(1)
|
return interp.NewExitStatus(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,7 +200,11 @@ func execCommand(cmd string) error {
|
||||||
|
|
||||||
if code, ok := luaexitcode.TryInt(); ok {
|
if code, ok := luaexitcode.TryInt(); ok {
|
||||||
exitcode = uint8(code)
|
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)
|
return interp.NewExitStatus(exitcode)
|
||||||
}
|
}
|
||||||
|
@ -320,7 +330,7 @@ func execCommand(cmd string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
runner, _ := interp.New(
|
runner, _ := interp.New(
|
||||||
interp.StdIO(os.Stdin, os.Stdout, os.Stderr),
|
interp.StdIO(os.Stdin, stdout, stderr),
|
||||||
interp.ExecHandler(exechandle),
|
interp.ExecHandler(exechandle),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -340,11 +350,11 @@ func execCommand(cmd string) error {
|
||||||
|
|
||||||
err = runner.Run(context.TODO(), stmt)
|
err = runner.Run(context.TODO(), stmt)
|
||||||
if err != nil {
|
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
|
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 {
|
if interactive && !private {
|
||||||
handleHistory(cmdstr)
|
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
|
// 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
|
||||||
|
|
|
@ -89,21 +89,25 @@ 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)
|
||||||
print(ansikit.print(text))
|
io.write(ansikit.format(text) .. "\n")
|
||||||
|
io.flush()
|
||||||
return ansikit
|
return ansikit
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue