fix(engine): do not print sequential newlines on no content

relates to #4182
This commit is contained in:
Jan De Dobbeleer 2023-09-03 08:54:39 +02:00 committed by Jan De Dobbeleer
parent 7eef6ef82d
commit 669521c8f7
2 changed files with 22 additions and 10 deletions

View file

@ -164,7 +164,7 @@ func (e *Engine) getTitleTemplateText() string {
return ""
}
func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
func (e *Engine) renderBlock(block *Block, cancelNewline bool) bool {
defer e.patchPowerShellBleed()
// This is deprecated but we leave it in to not break configs
@ -177,7 +177,7 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
if !cancelNewline {
e.newline()
}
return
return false
}
// when in bash, for rprompt blocks we need to write plain
@ -189,7 +189,7 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
}
if !block.Enabled() {
return
return false
}
// do not print a newline to avoid a leading space
@ -203,7 +203,7 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
// do not print anything when we don't have any text
if length == 0 {
return
return false
}
switch block.Type { //nolint:exhaustive
@ -215,11 +215,11 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
if block.Alignment == Left {
e.currentLineLength += length
e.write(text)
return
return true
}
if block.Alignment != Right {
return
return false
}
space, OK := e.canWriteRightBlock(false)
@ -234,7 +234,7 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
e.write(padText)
}
e.currentLineLength = 0
return
return true
}
}
@ -246,7 +246,7 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
if padText, OK := e.shouldFill(block.Filler, space, length); OK {
e.write(padText)
e.write(text)
return
return true
}
var prompt string
@ -262,6 +262,8 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
e.rprompt = text
e.rpromptLength = length
}
return true
}
func (e *Engine) patchPowerShellBleed() {

View file

@ -28,13 +28,23 @@ func (e *Engine) Primary() string {
// cache a pointer to the color cycle
cycle = &e.Config.Cycle
var cancelNewline, didRender bool
for i, block := range e.Config.Blocks {
var cancelNewline bool
// do not print a leading newline when we're at the first row and the prompt is cleared
if i == 0 {
row, _ := e.Env.CursorPosition()
cancelNewline = e.Env.Flags().Cleared || e.Env.Flags().PromptCount == 1 || row == 1
}
e.renderBlock(block, cancelNewline)
// skip setting a newline when we didn't print anything yet
if i != 0 {
cancelNewline = !didRender
}
if e.renderBlock(block, cancelNewline) {
didRender = true
}
}
if len(e.Config.ConsoleTitleTemplate) > 0 && !e.Env.Flags().Plain {