diff --git a/src/engine/engine.go b/src/engine/engine.go index 176efd99..2864b559 100644 --- a/src/engine/engine.go +++ b/src/engine/engine.go @@ -126,17 +126,12 @@ func (e *Engine) isWarp() bool { return e.Env.Getenv("TERM_PROGRAM") == "WarpTerminal" } -func (e *Engine) shouldFill(filler string, blockLength int) (string, bool) { +func (e *Engine) shouldFill(filler string, remaining, blockLength int) (string, bool) { if len(filler) == 0 { return "", false } - terminalWidth, err := e.Env.TerminalWidth() - if err != nil || terminalWidth == 0 { - return "", false - } - - padLength := terminalWidth - e.currentLineLength - blockLength + padLength := remaining - blockLength if padLength <= 0 { return "", false } @@ -221,32 +216,36 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) { } text, length := block.RenderSegments() - e.rpromptLength = length - if _, OK := e.canWriteRightBlock(false); OK { + space, OK := e.canWriteRightBlock(false) + // we can't print the right block as there's not enough room available + if !OK { switch block.Overflow { case Break: e.newline() case Hide: // make sure to fill if needed - if padText, OK := e.shouldFill(block.Filler, 0); OK { + if padText, OK := e.shouldFill(block.Filler, space, 0); OK { e.write(padText) } + e.currentLineLength = 0 return } } - if padText, OK := e.shouldFill(block.Filler, length); OK { - // in this case we can print plain + defer func() { + e.currentLineLength = 0 + }() + + // validate if we have a filler and fill if needed + if padText, OK := e.shouldFill(block.Filler, space, length); OK { e.write(padText) e.write(text) return } - prompt := e.Writer.CarriageForward() - prompt += e.Writer.GetCursorForRightWrite(length, block.HorizontalOffset) + prompt := strings.Repeat(" ", space-length) prompt += text - e.currentLineLength = 0 e.write(prompt) case RPrompt: e.rprompt, e.rpromptLength = block.RenderSegments() diff --git a/src/engine/prompt.go b/src/engine/prompt.go index 8e095fb9..b7e43262 100644 --- a/src/engine/prompt.go +++ b/src/engine/prompt.go @@ -152,8 +152,11 @@ func (e *Engine) ExtraPrompt(promptType ExtraPromptType) string { str, length := e.Writer.String() if promptType == Transient { - if padText, OK := e.shouldFill(prompt.Filler, length); OK { - str += padText + consoleWidth, err := e.Env.TerminalWidth() + if err == nil || consoleWidth != 0 { + if padText, OK := e.shouldFill(prompt.Filler, consoleWidth, length); OK { + str += padText + } } } diff --git a/src/platform/shell_unix.go b/src/platform/shell_unix.go index f39c08ec..3907a379 100644 --- a/src/platform/shell_unix.go +++ b/src/platform/shell_unix.go @@ -55,7 +55,8 @@ func (env *Shell) IsWsl2() bool { func (env *Shell) TerminalWidth() (int, error) { defer env.Trace(time.Now()) - if env.CmdFlags.TerminalWidth != 0 { + if env.CmdFlags.TerminalWidth > 0 { + env.DebugF("terminal width: %d", env.CmdFlags.TerminalWidth) return env.CmdFlags.TerminalWidth, nil } width, err := terminal.Width() diff --git a/src/platform/shell_windows.go b/src/platform/shell_windows.go index bcc14a6a..24cb066d 100644 --- a/src/platform/shell_windows.go +++ b/src/platform/shell_windows.go @@ -87,7 +87,8 @@ func (env *Shell) IsWsl2() bool { func (env *Shell) TerminalWidth() (int, error) { defer env.Trace(time.Now()) - if env.CmdFlags.TerminalWidth != 0 { + if env.CmdFlags.TerminalWidth > 0 { + env.DebugF("terminal width: %d", env.CmdFlags.TerminalWidth) return env.CmdFlags.TerminalWidth, nil } handle, err := syscall.Open("CONOUT$", syscall.O_RDWR, 0)