feat: do not print initial newline

resolves #3388
This commit is contained in:
Jan De Dobbeleer 2023-01-23 07:22:29 +01:00 committed by Jan De Dobbeleer
parent 522a216c00
commit 622e2071f1
2 changed files with 28 additions and 13 deletions

View file

@ -16,10 +16,11 @@ var (
) )
type Engine struct { type Engine struct {
Config *Config Config *Config
Env platform.Environment Env platform.Environment
Writer *ansi.Writer Writer *ansi.Writer
Plain bool Plain bool
PromptCount int
console strings.Builder console strings.Builder
currentLineLength int currentLineLength int
@ -63,8 +64,8 @@ func (e *Engine) canWriteRightBlock(rprompt bool) bool {
func (e *Engine) PrintPrimary() string { func (e *Engine) PrintPrimary() string {
// cache a pointer to the color cycle // cache a pointer to the color cycle
cycle = &e.Config.Cycle cycle = &e.Config.Cycle
for _, block := range e.Config.Blocks { for i, block := range e.Config.Blocks {
e.renderBlock(block) e.renderBlock(block, (i == 0 && e.PromptCount == 1))
} }
if len(e.Config.ConsoleTitleTemplate) > 0 { if len(e.Config.ConsoleTitleTemplate) > 0 {
title := e.getTitleTemplateText() title := e.getTitleTemplateText()
@ -147,10 +148,11 @@ func (e *Engine) getTitleTemplateText() string {
return "" return ""
} }
func (e *Engine) renderBlock(block *Block) { func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
defer func() { defer func() {
e.write(e.Writer.ClearAfter()) e.write(e.Writer.ClearAfter())
}() }()
// when in bash, for rprompt blocks we need to write plain // when in bash, for rprompt blocks we need to write plain
// and wrap in escaped mode or the prompt will not render correctly // and wrap in escaped mode or the prompt will not render correctly
if e.Env.Shell() == shell.BASH && block.Type == RPrompt { if e.Env.Shell() == shell.BASH && block.Type == RPrompt {
@ -158,17 +160,29 @@ func (e *Engine) renderBlock(block *Block) {
} else { } else {
block.Init(e.Env, e.Writer) block.Init(e.Env, e.Writer)
} }
if !block.Enabled() { if !block.Enabled() {
return return
} }
if block.Newline {
// do not print a newline to avoid a leading space
// when we're printin the first primary prompt in
// the shell
if block.Newline && !cancelNewline {
e.newline() e.newline()
} }
switch block.Type { switch block.Type {
// This is deprecated but leave if to not break current configs // This is deprecated but we leave it in to not break configs
// It is encouraged to used "newline": true on block level // It is encouraged to used "newline": true on block level
// rather than the standalone the linebreak block // rather than the standalone the linebreak block
case LineBreak: case LineBreak:
// do not print a newline to avoid a leading space
// when we're printin the first primary prompt in
// the shell
if !cancelNewline {
return
}
e.newline() e.newline()
case Prompt: case Prompt:
if block.VerticalOffset != 0 { if block.VerticalOffset != 0 {

View file

@ -25,10 +25,11 @@ func New(flags *platform.Flags) *Engine {
ansiWriter.Init(env.Shell()) ansiWriter.Init(env.Shell())
eng := &Engine{ eng := &Engine{
Config: cfg, Config: cfg,
Env: env, Env: env,
Writer: ansiWriter, Writer: ansiWriter,
Plain: flags.Plain, Plain: flags.Plain,
PromptCount: env.CmdFlags.PromptCount,
} }
return eng return eng