mirror of https://github.com/Hilbis/Hilbish
Compare commits
3 Commits
ce625aca0c
...
e044aeb5ed
Author | SHA1 | Date |
---|---|---|
TorchedSammy | e044aeb5ed | |
sammyette | 0a2046e985 | |
TorchedSammy | 626b036b4b |
29
api.go
29
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, false},
|
||||
"prompt": {hlprompt, 1, true},
|
||||
"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)
|
||||
prompt(str, typ?)
|
||||
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,17 +313,34 @@ 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)
|
||||
}
|
||||
|
||||
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) > Changes the shell prompt to `str`
|
||||
prompt(str, typ?) > 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 0, nil
|
||||
return input, 0, nil
|
||||
end
|
||||
|
||||
return hilbish.runner.sh(input)
|
||||
|
@ -28,7 +28,9 @@ 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 2 values: the exit code, and an error.
|
||||
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.
|
||||
The exit code has to be a number, it will be 0 otherwise and the error can be
|
||||
`nil` to indicate no error.
|
||||
|
||||
|
@ -36,5 +38,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) -> code, err > Runs `input` in Hilbish's sh interpreter
|
||||
+ lua(input) -> code, err > Evals `input` as Lua code
|
||||
+ sh(input) -> input, code, err > Runs `input` in Hilbish's sh interpreter
|
||||
+ lua(input) -> input, code, err > Evals `input` as Lua code
|
||||
|
|
|
@ -73,7 +73,8 @@ function hilbish.prependPath(dir) end
|
|||
--- `%u` - Name of current user
|
||||
--- `%h` - Hostname of device
|
||||
--- @param str string
|
||||
function hilbish.prompt(str) end
|
||||
--- @param typ string Type of prompt, being left or right. Left by default.
|
||||
function hilbish.prompt(str, typ) 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,38 +32,40 @@ 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
|
||||
}
|
||||
exitCode, err := handleSh(cmdString)
|
||||
input, 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
|
||||
}
|
||||
exitCode, err := handleLua(cmdString)
|
||||
input, exitCode, err = handleLua(cmdString)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
cmdFinish(exitCode, input, priv)
|
||||
case "lua":
|
||||
exitCode, err := handleLua(cmdString)
|
||||
input, exitCode, err = handleLua(cmdString)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
cmdFinish(exitCode, input, priv)
|
||||
case "sh":
|
||||
exitCode, err := handleSh(cmdString)
|
||||
input, exitCode, err = handleSh(cmdString)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
|
@ -79,14 +81,19 @@ func runInput(input string, priv bool) {
|
|||
return
|
||||
}
|
||||
|
||||
luaexitcode := term.Get(0) // first return value (makes sense right i love stacks)
|
||||
luaexitcode := term.Get(0)
|
||||
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)
|
||||
}
|
||||
|
@ -94,7 +101,7 @@ func runInput(input string, priv bool) {
|
|||
}
|
||||
}
|
||||
|
||||
func handleLua(cmdString string) (uint8, error) {
|
||||
func handleLua(cmdString string) (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 {
|
||||
|
@ -105,7 +112,7 @@ func handleLua(cmdString string) (uint8, error) {
|
|||
}
|
||||
}
|
||||
*/
|
||||
return 125, err
|
||||
return cmdString, 125, err
|
||||
}
|
||||
// And if there's no syntax errors and -n isnt provided, run
|
||||
if !noexecute {
|
||||
|
@ -114,19 +121,19 @@ func handleLua(cmdString string) (uint8, error) {
|
|||
}
|
||||
}
|
||||
if err == nil {
|
||||
return 0, nil
|
||||
return cmdString, 0, nil
|
||||
}
|
||||
|
||||
return 125, err
|
||||
return cmdString, 125, err
|
||||
}
|
||||
|
||||
func handleSh(cmdString string) (uint8, error) {
|
||||
func handleSh(cmdString string) (string, uint8, error) {
|
||||
_, _, err := execCommand(cmdString, true)
|
||||
if err != nil {
|
||||
// If input is incomplete, start multiline prompting
|
||||
if syntax.IsIncomplete(err) {
|
||||
if !interactive {
|
||||
return 126, err
|
||||
return cmdString, 126, err
|
||||
}
|
||||
for {
|
||||
cmdString, err = continuePrompt(strings.TrimSuffix(cmdString, "\\"))
|
||||
|
@ -137,23 +144,23 @@ func handleSh(cmdString string) (uint8, error) {
|
|||
if syntax.IsIncomplete(err) || strings.HasSuffix(cmdString, "\\") {
|
||||
continue
|
||||
} else if code, ok := interp.IsExitStatus(err); ok {
|
||||
return code, nil
|
||||
return cmdString, code, nil
|
||||
} else if err != nil {
|
||||
return 126, err
|
||||
return cmdString, 126, err
|
||||
} else {
|
||||
return 0, nil
|
||||
return cmdString, 0, nil
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if code, ok := interp.IsExitStatus(err); ok {
|
||||
return code, nil
|
||||
return cmdString, code, nil
|
||||
} else {
|
||||
return 126, err
|
||||
return cmdString, 126, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0, nil
|
||||
return cmdString, 0, nil
|
||||
}
|
||||
|
||||
// Run command in sh interpreter
|
||||
|
|
|
@ -31,6 +31,8 @@ type Instance struct {
|
|||
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
|
||||
|
|
|
@ -14,6 +14,12 @@ 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) {
|
||||
|
@ -185,6 +191,7 @@ func (rl *Instance) computePrompt() (prompt []rune) {
|
|||
|
||||
// Strip color escapes
|
||||
rl.promptLen = getRealLength(string(rl.realPrompt))
|
||||
rl.rightPromptLen = getRealLength(string(rl.rightPrompt))
|
||||
|
||||
return
|
||||
}
|
||||
|
@ -205,3 +212,11 @@ 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,6 +564,8 @@ func (rl *Instance) editorInput(r []rune) {
|
|||
rl.writeHintText()
|
||||
}
|
||||
|
||||
rl.echoRightPrompt()
|
||||
|
||||
if len(rl.multisplit) == 0 {
|
||||
rl.syntaxCompletion()
|
||||
}
|
||||
|
|
|
@ -121,6 +121,8 @@ 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,6 +281,13 @@ 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
|
||||
}
|
||||
|
||||
exitCode, err := handleSh(cmd)
|
||||
input, exitCode, err := handleSh(cmd)
|
||||
var luaErr rt.Value = rt.NilValue
|
||||
if err != nil {
|
||||
luaErr = rt.StringValue(err.Error())
|
||||
}
|
||||
|
||||
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
|
||||
return c.PushingNext(t.Runtime, rt.StringValue(input), 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
|
||||
}
|
||||
|
||||
exitCode, err := handleLua(cmd)
|
||||
input, exitCode, err := handleLua(cmd)
|
||||
var luaErr rt.Value = rt.NilValue
|
||||
if err != nil {
|
||||
luaErr = rt.StringValue(err.Error())
|
||||
}
|
||||
|
||||
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
|
||||
return c.PushingNext(t.Runtime, rt.StringValue(input), rt.IntValue(int64(exitCode)), luaErr), nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue