From 2cba140522c273d8050843bfce776bd43deab606 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 8 Dec 2022 11:30:21 +0100 Subject: [PATCH] fix(warp): support rprompt --- src/engine/engine.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/engine/engine.go b/src/engine/engine.go index 3a1871a5..8a4b425c 100644 --- a/src/engine/engine.go +++ b/src/engine/engine.go @@ -104,8 +104,7 @@ func (e *Engine) printPWD() { func (e *Engine) newline() { // WARP terminal will remove \n from the prompt, so we hack a newline in - if e.Env.Getenv("TERM_PROGRAM") == "WarpTerminal" { - // fmt.Println("TERM_PROGRAM") + if e.isWarp() { e.write(e.Ansi.LineBreak()) } else { e.write("\n") @@ -113,6 +112,10 @@ func (e *Engine) newline() { e.currentLineLength = 0 } +func (e *Engine) isWarp() bool { + return e.Env.Getenv("TERM_PROGRAM") == "WarpTerminal" +} + func (e *Engine) shouldFill(block *Block, length int) (string, bool) { if len(block.Filler) == 0 { return "", false @@ -271,6 +274,17 @@ func (e *Engine) print() string { if !e.Env.Flags().Eval { break } + // Warp doesn't support RPROMPT so we need to write it manually + if e.isWarp() { + e.write(e.Ansi.SaveCursorPosition()) + e.write(e.Ansi.CarriageForward()) + e.write(e.Ansi.GetCursorForRightWrite(e.rpromptLength, 0)) + e.write(e.rprompt) + e.write(e.Ansi.RestoreCursorPosition()) + // escape double quotes contained in the prompt + prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(e.string(), `"`, `\"`)) + return prompt + } // escape double quotes contained in the prompt prompt := fmt.Sprintf("PS1=\"%s\"", strings.ReplaceAll(e.string(), `"`, `\"`)) prompt += fmt.Sprintf("\nRPROMPT=\"%s\"", e.rprompt)