fix(prompt): align right block logic with rprompt

resolves #5220
This commit is contained in:
Jan De Dobbeleer 2024-07-10 10:00:41 +02:00 committed by Jan De Dobbeleer
parent 8767dc3252
commit 1d38f36a14
5 changed files with 18 additions and 17 deletions

View file

@ -66,6 +66,7 @@ func printLn(lt logType, args ...string) {
if len(args) == 0 { if len(args) == 0 {
return return
} }
var str Text var str Text
switch lt { switch lt {
case debug: case debug:
@ -75,6 +76,7 @@ func printLn(lt logType, args ...string) {
case trace: case trace:
str = Text("[TRACE] ").Purple() str = Text("[TRACE] ").Purple()
} }
// timestamp 156, 231, 201 // timestamp 156, 231, 201
str += Text(time.Now().Format("15:04:05.000") + " ").Yellow().Plain() str += Text(time.Now().Format("15:04:05.000") + " ").Yellow().Plain()
str += Text(args[0]) str += Text(args[0])

View file

@ -40,7 +40,7 @@ func (e *Engine) string() string {
return text return text
} }
func (e *Engine) canWriteRightBlock(rprompt bool) (int, bool) { func (e *Engine) canWriteRightBlock(length int, rprompt bool) (int, bool) {
if rprompt && (len(e.rprompt) == 0) { if rprompt && (len(e.rprompt) == 0) {
return 0, false return 0, false
} }
@ -50,18 +50,15 @@ func (e *Engine) canWriteRightBlock(rprompt bool) (int, bool) {
return 0, false return 0, false
} }
promptWidth := e.currentLineLength availableSpace := consoleWidth - e.currentLineLength
availableSpace := consoleWidth - promptWidth
// spanning multiple lines // spanning multiple lines
if availableSpace < 0 { if availableSpace < 0 {
overflow := promptWidth % consoleWidth overflow := e.currentLineLength % consoleWidth
availableSpace = consoleWidth - overflow availableSpace = consoleWidth - overflow
} }
if rprompt { availableSpace -= length
availableSpace -= e.rpromptLength
}
promptBreathingRoom := 5 promptBreathingRoom := 5
if rprompt { if rprompt {
@ -140,12 +137,11 @@ func (e *Engine) isIterm() bool {
return terminal.Program == terminal.ITerm return terminal.Program == terminal.ITerm
} }
func (e *Engine) shouldFill(filler string, remaining, blockLength int) (string, bool) { func (e *Engine) shouldFill(filler string, padLength int) (string, bool) {
if len(filler) == 0 { if len(filler) == 0 {
return "", false return "", false
} }
padLength := remaining - blockLength
if padLength <= 0 { if padLength <= 0 {
return "", false return "", false
} }
@ -224,7 +220,8 @@ func (e *Engine) renderBlock(block *config.Block, cancelNewline bool) bool {
return false return false
} }
space, OK := e.canWriteRightBlock(false) space, OK := e.canWriteRightBlock(length, false)
// we can't print the right block as there's not enough room available // we can't print the right block as there's not enough room available
if !OK { if !OK {
switch block.Overflow { switch block.Overflow {
@ -232,9 +229,10 @@ func (e *Engine) renderBlock(block *config.Block, cancelNewline bool) bool {
e.writeNewline() e.writeNewline()
case config.Hide: case config.Hide:
// make sure to fill if needed // make sure to fill if needed
if padText, OK := e.shouldFill(block.Filler, space, 0); OK { if padText, OK := e.shouldFill(block.Filler, space+length); OK {
e.write(padText) e.write(padText)
} }
e.currentLineLength = 0 e.currentLineLength = 0
return true return true
} }
@ -245,14 +243,13 @@ func (e *Engine) renderBlock(block *config.Block, cancelNewline bool) bool {
}() }()
// validate if we have a filler and fill if needed // validate if we have a filler and fill if needed
if padText, OK := e.shouldFill(block.Filler, space, length); OK { if padText, OK := e.shouldFill(block.Filler, space); OK {
e.write(padText) e.write(padText)
e.write(text) e.write(text)
return true return true
} }
var prompt string var prompt string
space -= length
if space > 0 { if space > 0 {
prompt += strings.Repeat(" ", space) prompt += strings.Repeat(" ", space)

View file

@ -42,7 +42,8 @@ func TestCanWriteRPrompt(t *testing.T) {
currentLineLength: tc.PromptLength, currentLineLength: tc.PromptLength,
rprompt: "hello", rprompt: "hello",
} }
_, got := engine.canWriteRightBlock(true)
_, got := engine.canWriteRightBlock(tc.RPromptLength, true)
assert.Equal(t, tc.Expected, got, tc.Case) assert.Equal(t, tc.Expected, got, tc.Case)
} }
} }

View file

@ -74,10 +74,11 @@ func (e *Engine) ExtraPrompt(promptType ExtraPromptType) string {
terminal.Write(background, foreground, promptText) terminal.Write(background, foreground, promptText)
str, length := terminal.String() str, length := terminal.String()
if promptType == Transient {
if promptType == Transient && len(prompt.Filler) != 0 {
consoleWidth, err := e.Env.TerminalWidth() consoleWidth, err := e.Env.TerminalWidth()
if err == nil || consoleWidth != 0 { if err == nil || consoleWidth != 0 {
if padText, OK := e.shouldFill(prompt.Filler, consoleWidth, length); OK { if padText, OK := e.shouldFill(prompt.Filler, consoleWidth-length); OK {
str += padText str += padText
} }
} }

View file

@ -114,7 +114,7 @@ func (e *Engine) needsPrimaryRPrompt() bool {
} }
func (e *Engine) writePrimaryRPrompt() { func (e *Engine) writePrimaryRPrompt() {
space, OK := e.canWriteRightBlock(true) space, OK := e.canWriteRightBlock(e.rpromptLength, true)
if !OK { if !OK {
return return
} }