fix(bash): support multiline prompt

This commit is contained in:
Jan De Dobbeleer 2024-07-15 21:58:40 +02:00 committed by Jan De Dobbeleer
parent 2585b90946
commit 17134cdb29

View file

@ -11,17 +11,20 @@ import (
) )
func (e *Engine) RPrompt() string { func (e *Engine) RPrompt() string {
filterRPromptBlock := func(blocks []*config.Block) *config.Block { var rprompt *config.Block
for _, block := range blocks { var lineCount int
for _, block := range e.Config.Blocks {
if block.Type == config.RPrompt { if block.Type == config.RPrompt {
return block rprompt = block
}
}
return nil
} }
block := filterRPromptBlock(e.Config.Blocks) if block.Newline || block.Type == config.LineBreak {
if block == nil { lineCount++
}
}
if rprompt == nil {
return "" return ""
} }
@ -29,13 +32,13 @@ func (e *Engine) RPrompt() string {
terminal.Init(shell.GENERIC) terminal.Init(shell.GENERIC)
} }
block.Init(e.Env) rprompt.Init(e.Env)
if !block.Enabled() { if !rprompt.Enabled() {
return "" return ""
} }
text, length := e.renderBlockSegments(block) text, length := e.renderBlockSegments(rprompt)
e.rpromptLength = length e.rpromptLength = length
if e.Env.Shell() != shell.BASH { if e.Env.Shell() != shell.BASH {
@ -55,5 +58,11 @@ func (e *Engine) RPrompt() string {
text = fmt.Sprintf("%s%s\r", strings.Repeat(" ", padding), text) text = fmt.Sprintf("%s%s\r", strings.Repeat(" ", padding), text)
// bash prints this on the same line as the prompt so we need to move the cursor down
// in case the prompt spans multiple lines
if lineCount > 0 {
return terminal.SaveCursorPosition() + strings.Repeat("\n", lineCount) + text + terminal.RestoreCursorPosition()
}
return text return text
} }