From 3114666a253a93ab8574e5b913a5f28f3ebebcf5 Mon Sep 17 00:00:00 2001 From: "L. Yeung" Date: Thu, 19 Sep 2024 14:58:59 +0800 Subject: [PATCH] fix(prompt): write real newlines for Elvish on non-Windows systems --- src/prompt/engine.go | 17 +++++++++-------- src/prompt/rprompt.go | 7 +++++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/prompt/engine.go b/src/prompt/engine.go index 9ee62ff0..0a2ae2b9 100644 --- a/src/prompt/engine.go +++ b/src/prompt/engine.go @@ -113,8 +113,8 @@ func (e *Engine) getNewline() string { } // Warp terminal will remove a newline character ('\n') from the prompt, so we hack it in. - // For Elvish, we do this to prevent cutting off a right-aligned block. - if e.isWarp() || e.Env.Shell() == shell.ELVISH { + // For Elvish on Windows, we do this to prevent cutting off a right-aligned block. + if e.isWarp() || (e.Env.Shell() == shell.ELVISH && e.Env.GOOS() == runtime.WINDOWS) { return terminal.LineBreak() } @@ -545,16 +545,17 @@ func New(flags *runtime.Flags) *Engine { } switch env.Shell() { - case shell.TCSH: - // In Tcsh, newlines in a prompt are badly translated. - // No silver bullet here. We have to reduce the terminal width by 1 so a right-aligned block will not be broken. - eng.rectifyTerminalWidth(-1) - case shell.ELVISH, shell.XONSH: - // In these shells, the behavior of wrapping at the end of a prompt line is inconsistent across platforms. + case shell.XONSH: + // In Xonsh, the behavior of wrapping at the end of a prompt line is inconsistent across platforms. // On Windows, it wraps before the rightmost cell on the terminal screen, that is, the rightmost cell is never available for a prompt line. if eng.Env.GOOS() == runtime.WINDOWS { eng.rectifyTerminalWidth(-1) } + case shell.TCSH, shell.ELVISH: + // In Tcsh, newlines in a prompt are badly translated. + // No silver bullet here. We have to reduce the terminal width by 1 so a right-aligned block will not be broken. + // In Elvish, the behavior is similar to that in Xonsh, but we do this for all platforms. + eng.rectifyTerminalWidth(-1) case shell.PWSH, shell.PWSH5: // when in PowerShell, and force patching the bleed bug // we need to reduce the terminal width by 1 so the last diff --git a/src/prompt/rprompt.go b/src/prompt/rprompt.go index 359a0f60..99456ae1 100644 --- a/src/prompt/rprompt.go +++ b/src/prompt/rprompt.go @@ -2,6 +2,8 @@ package prompt import ( "github.com/jandedobbeleer/oh-my-posh/src/config" + "github.com/jandedobbeleer/oh-my-posh/src/runtime" + "github.com/jandedobbeleer/oh-my-posh/src/shell" ) func (e *Engine) RPrompt() string { @@ -29,5 +31,10 @@ func (e *Engine) RPrompt() string { text, length := e.renderBlockSegments(rprompt) e.rpromptLength = length + if e.Env.Shell() == shell.ELVISH && e.Env.GOOS() != runtime.WINDOWS { + // Workaround to align with a right-aligned block on non-Windows systems. + text += " " + } + return text }