mirror of https://github.com/Hilbis/Hilbish
Compare commits
No commits in common. "e044aeb5edceeb4a79cf625ee0693f85f1f1b5d7" and "ce625aca0cb1b9218b4652216ef76e1f73d6ff3c" have entirely different histories.
e044aeb5ed
...
ce625aca0c
31
api.go
31
api.go
|
@ -35,7 +35,7 @@ var exports = map[string]util.LuaExport{
|
|||
"hinter": {hlhinter, 1, false},
|
||||
"multiprompt": {hlmultiprompt, 1, false},
|
||||
"prependPath": {hlprependPath, 1, false},
|
||||
"prompt": {hlprompt, 1, true},
|
||||
"prompt": {hlprompt, 1, false},
|
||||
"inputMode": {hlinputMode, 1, false},
|
||||
"interval": {hlinterval, 2, false},
|
||||
"read": {hlread, 1, false},
|
||||
|
@ -305,7 +305,7 @@ func hlread(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|||
}
|
||||
|
||||
/*
|
||||
prompt(str, typ?)
|
||||
prompt(str)
|
||||
Changes the shell prompt to `str`
|
||||
There are a few verbs that can be used in the prompt text.
|
||||
These will be formatted and replaced with the appropriate values.
|
||||
|
@ -313,34 +313,17 @@ These will be formatted and replaced with the appropriate values.
|
|||
`%u` - Name of current user
|
||||
`%h` - Hostname of device
|
||||
--- @param str string
|
||||
--- @param typ string Type of prompt, being left or right. Left by default.
|
||||
*/
|
||||
func hlprompt(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
var prompt string
|
||||
err := c.Check1Arg()
|
||||
if err == nil {
|
||||
prompt, err = c.StringArg(0)
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
p, err := c.StringArg(0)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
typ := "left"
|
||||
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() + ")")
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
lr.SetPrompt(fmtPrompt(prompt))
|
||||
|
||||
return c.Next(), nil
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ multiprompt(str) > Changes the continued line prompt to `str`
|
|||
|
||||
prependPath(dir) > Prepends `dir` to $PATH
|
||||
|
||||
prompt(str, typ?) > Changes the shell prompt to `str`
|
||||
prompt(str) > Changes the shell prompt to `str`
|
||||
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
|
||||
|
|
|
@ -20,7 +20,7 @@ An example:
|
|||
hilbish.runnerMode(function(input)
|
||||
local ok = pcall(fennel.eval, input)
|
||||
if ok then
|
||||
return input, 0, nil
|
||||
return 0, nil
|
||||
end
|
||||
|
||||
return hilbish.runner.sh(input)
|
||||
|
@ -28,9 +28,7 @@ end)
|
|||
|
||||
The `hilbish.runner` interface is an alternative to using `hilbish.runnerMode`
|
||||
and also provides the sh and Lua runner functions that Hilbish itself uses.
|
||||
A runner function is expected to return 3 values: the input, exit code, and an error.
|
||||
The input return is there incase you need to prompt for more input.
|
||||
If you don't, just return the input passed to the runner function.
|
||||
A runner function is expected to return 2 values: the exit code, and an error.
|
||||
The exit code has to be a number, it will be 0 otherwise and the error can be
|
||||
`nil` to indicate no error.
|
||||
|
||||
|
@ -38,5 +36,5 @@ The exit code has to be a number, it will be 0 otherwise and the error can be
|
|||
These are the functions for the `hilbish.runner` interface
|
||||
|
||||
+ setMode(mode) > The same as `hilbish.runnerMode`
|
||||
+ sh(input) -> input, code, err > Runs `input` in Hilbish's sh interpreter
|
||||
+ lua(input) -> input, code, err > Evals `input` as Lua code
|
||||
+ sh(input) -> code, err > Runs `input` in Hilbish's sh interpreter
|
||||
+ lua(input) -> code, err > Evals `input` as Lua code
|
||||
|
|
|
@ -73,8 +73,7 @@ function hilbish.prependPath(dir) end
|
|||
--- `%u` - Name of current user
|
||||
--- `%h` - Hostname of device
|
||||
--- @param str string
|
||||
--- @param typ string Type of prompt, being left or right. Left by default.
|
||||
function hilbish.prompt(str, typ) end
|
||||
function hilbish.prompt(str) end
|
||||
|
||||
--- Read input from the user, using Hilbish's line editor/input reader.
|
||||
--- This is a separate instance from the one Hilbish actually uses.
|
||||
|
|
45
exec.go
45
exec.go
|
@ -32,40 +32,38 @@ func runInput(input string, priv bool) {
|
|||
cmdString := aliases.Resolve(input)
|
||||
hooks.Em.Emit("command.preexec", input, cmdString)
|
||||
|
||||
var exitCode uint8
|
||||
var err error
|
||||
if runnerMode.Type() == rt.StringType {
|
||||
switch runnerMode.AsString() {
|
||||
case "hybrid":
|
||||
_, _, err = handleLua(cmdString)
|
||||
_, err := handleLua(cmdString)
|
||||
if err == nil {
|
||||
cmdFinish(0, input, priv)
|
||||
return
|
||||
}
|
||||
input, exitCode, err = handleSh(cmdString)
|
||||
exitCode, err := handleSh(cmdString)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
cmdFinish(exitCode, input, priv)
|
||||
case "hybridRev":
|
||||
_, _, err = handleSh(cmdString)
|
||||
_, err := handleSh(cmdString)
|
||||
if err == nil {
|
||||
cmdFinish(0, input, priv)
|
||||
return
|
||||
}
|
||||
input, exitCode, err = handleLua(cmdString)
|
||||
exitCode, err := handleLua(cmdString)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
cmdFinish(exitCode, input, priv)
|
||||
case "lua":
|
||||
input, exitCode, err = handleLua(cmdString)
|
||||
exitCode, err := handleLua(cmdString)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
cmdFinish(exitCode, input, priv)
|
||||
case "sh":
|
||||
input, exitCode, err = handleSh(cmdString)
|
||||
exitCode, err := handleSh(cmdString)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
|
@ -81,19 +79,14 @@ func runInput(input string, priv bool) {
|
|||
return
|
||||
}
|
||||
|
||||
luaexitcode := term.Get(0)
|
||||
luaexitcode := term.Get(0) // first return value (makes sense right i love stacks)
|
||||
runErr := term.Get(1)
|
||||
luaInput := term.Get(1)
|
||||
|
||||
var exitCode uint8
|
||||
if code, ok := luaexitcode.TryInt(); ok {
|
||||
exitCode = uint8(code)
|
||||
}
|
||||
|
||||
if inp, ok := luaInput.TryString(); ok {
|
||||
input = inp
|
||||
}
|
||||
|
||||
if runErr != rt.NilValue {
|
||||
fmt.Fprintln(os.Stderr, runErr)
|
||||
}
|
||||
|
@ -101,7 +94,7 @@ func runInput(input string, priv bool) {
|
|||
}
|
||||
}
|
||||
|
||||
func handleLua(cmdString string) (string, uint8, error) {
|
||||
func handleLua(cmdString string) (uint8, error) {
|
||||
// First try to load input, essentially compiling to bytecode
|
||||
chunk, err := l.CompileAndLoadLuaChunk("", []byte(cmdString), rt.TableValue(l.GlobalEnv()))
|
||||
if err != nil && noexecute {
|
||||
|
@ -112,7 +105,7 @@ func handleLua(cmdString string) (string, uint8, error) {
|
|||
}
|
||||
}
|
||||
*/
|
||||
return cmdString, 125, err
|
||||
return 125, err
|
||||
}
|
||||
// And if there's no syntax errors and -n isnt provided, run
|
||||
if !noexecute {
|
||||
|
@ -121,19 +114,19 @@ func handleLua(cmdString string) (string, uint8, error) {
|
|||
}
|
||||
}
|
||||
if err == nil {
|
||||
return cmdString, 0, nil
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
return cmdString, 125, err
|
||||
return 125, err
|
||||
}
|
||||
|
||||
func handleSh(cmdString string) (string, uint8, error) {
|
||||
func handleSh(cmdString string) (uint8, error) {
|
||||
_, _, err := execCommand(cmdString, true)
|
||||
if err != nil {
|
||||
// If input is incomplete, start multiline prompting
|
||||
if syntax.IsIncomplete(err) {
|
||||
if !interactive {
|
||||
return cmdString, 126, err
|
||||
return 126, err
|
||||
}
|
||||
for {
|
||||
cmdString, err = continuePrompt(strings.TrimSuffix(cmdString, "\\"))
|
||||
|
@ -144,23 +137,23 @@ func handleSh(cmdString string) (string, uint8, error) {
|
|||
if syntax.IsIncomplete(err) || strings.HasSuffix(cmdString, "\\") {
|
||||
continue
|
||||
} else if code, ok := interp.IsExitStatus(err); ok {
|
||||
return cmdString, code, nil
|
||||
return code, nil
|
||||
} else if err != nil {
|
||||
return cmdString, 126, err
|
||||
return 126, err
|
||||
} else {
|
||||
return cmdString, 0, nil
|
||||
return 0, nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if code, ok := interp.IsExitStatus(err); ok {
|
||||
return cmdString, code, nil
|
||||
return code, nil
|
||||
} else {
|
||||
return cmdString, 126, err
|
||||
return 126, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return cmdString, 0, nil
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
// Run command in sh interpreter
|
||||
|
|
|
@ -30,13 +30,11 @@ type Instance struct {
|
|||
Multiline bool // If set to true, the shell will have a two-line prompt.
|
||||
MultilinePrompt string // If multiline is true, this is the content of the 2nd line.
|
||||
|
||||
mainPrompt string // If multiline true, the full prompt string / If false, the 1st line of the prompt
|
||||
rightPrompt string
|
||||
rightPromptLen int
|
||||
realPrompt []rune // The prompt that is actually on the same line as the beginning of the input line.
|
||||
defaultPrompt []rune
|
||||
promptLen int
|
||||
stillOnRefresh bool // True if some logs have printed asynchronously since last loop. Check refresh prompt funcs
|
||||
mainPrompt string // If multiline true, the full prompt string / If false, the 1st line of the prompt
|
||||
realPrompt []rune // The prompt that is actually on the same line as the beginning of the input line.
|
||||
defaultPrompt []rune
|
||||
promptLen int
|
||||
stillOnRefresh bool // True if some logs have printed asynchronously since last loop. Check refresh prompt funcs
|
||||
|
||||
//
|
||||
// Input Line ---------------------------------------------------------------------------------
|
||||
|
|
|
@ -14,12 +14,6 @@ func (rl *Instance) SetPrompt(s string) {
|
|||
rl.computePrompt()
|
||||
}
|
||||
|
||||
// SetRightPrompt sets the right prompt.
|
||||
func (rl *Instance) SetRightPrompt(s string) {
|
||||
rl.rightPrompt = s + " "
|
||||
rl.computePrompt()
|
||||
}
|
||||
|
||||
// RefreshPromptLog - A simple function to print a string message (a log, or more broadly,
|
||||
// an asynchronous event) without bothering the user, and by "pushing" the prompt below the message.
|
||||
func (rl *Instance) RefreshPromptLog(log string) (err error) {
|
||||
|
@ -191,7 +185,6 @@ func (rl *Instance) computePrompt() (prompt []rune) {
|
|||
|
||||
// Strip color escapes
|
||||
rl.promptLen = getRealLength(string(rl.realPrompt))
|
||||
rl.rightPromptLen = getRealLength(string(rl.rightPrompt))
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -212,11 +205,3 @@ func getRealLength(s string) (l int) {
|
|||
stripped := ansi.Strip(s)
|
||||
return uniseg.GraphemeClusterCount(stripped)
|
||||
}
|
||||
|
||||
func (rl *Instance) echoRightPrompt() {
|
||||
if rl.fullX < GetTermWidth() - rl.rightPromptLen - 1 {
|
||||
moveCursorForwards(GetTermWidth())
|
||||
moveCursorBackwards(rl.rightPromptLen)
|
||||
print(rl.rightPrompt)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -564,8 +564,6 @@ func (rl *Instance) editorInput(r []rune) {
|
|||
rl.writeHintText()
|
||||
}
|
||||
|
||||
rl.echoRightPrompt()
|
||||
|
||||
if len(rl.multisplit) == 0 {
|
||||
rl.syntaxCompletion()
|
||||
}
|
||||
|
|
|
@ -121,8 +121,6 @@ func (rl *Instance) renderHelpers() {
|
|||
rl.writeHintText()
|
||||
}
|
||||
|
||||
rl.echoRightPrompt()
|
||||
|
||||
// Go at beginning of first line after input remainder
|
||||
moveCursorDown(rl.fullY - rl.posY)
|
||||
moveCursorBackwards(GetTermWidth())
|
||||
|
|
7
rl.go
7
rl.go
|
@ -281,13 +281,6 @@ func (lr *lineReader) SetPrompt(p string) {
|
|||
}
|
||||
}
|
||||
|
||||
func (lr *lineReader) SetRightPrompt(p string) {
|
||||
lr.rl.SetRightPrompt(p)
|
||||
if initialized && !running {
|
||||
lr.rl.RefreshPromptInPlace("")
|
||||
}
|
||||
}
|
||||
|
||||
func (lr *lineReader) AddHistory(cmd string) {
|
||||
fileHist.Write(cmd)
|
||||
}
|
||||
|
|
|
@ -28,13 +28,13 @@ func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
input, exitCode, err := handleSh(cmd)
|
||||
exitCode, err := handleSh(cmd)
|
||||
var luaErr rt.Value = rt.NilValue
|
||||
if err != nil {
|
||||
luaErr = rt.StringValue(err.Error())
|
||||
}
|
||||
|
||||
return c.PushingNext(t.Runtime, rt.StringValue(input), rt.IntValue(int64(exitCode)), luaErr), nil
|
||||
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
|
||||
}
|
||||
|
||||
func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
|
@ -46,11 +46,11 @@ func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
input, exitCode, err := handleLua(cmd)
|
||||
exitCode, err := handleLua(cmd)
|
||||
var luaErr rt.Value = rt.NilValue
|
||||
if err != nil {
|
||||
luaErr = rt.StringValue(err.Error())
|
||||
}
|
||||
|
||||
return c.PushingNext(t.Runtime, rt.StringValue(input), rt.IntValue(int64(exitCode)), luaErr), nil
|
||||
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue