fix(prompt): print right block using spaces

This commit is contained in:
Jan De Dobbeleer 2023-08-04 18:43:23 +02:00 committed by Jan De Dobbeleer
parent 181b789186
commit c23069b185
4 changed files with 23 additions and 19 deletions

View file

@ -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()

View file

@ -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
}
}
}

View file

@ -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()

View file

@ -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)