fix(pwsh): patch PowerShell color bleed

relates to #65
This commit is contained in:
Jan De Dobbeleer 2023-08-09 08:21:04 +02:00 committed by Jan De Dobbeleer
parent 3998e038d9
commit aa51cd65d0
7 changed files with 48 additions and 4 deletions

View file

@ -53,6 +53,7 @@ type Config struct {
PWD string `json:"pwd,omitempty"`
Var map[string]interface{} `json:"var,omitempty"`
DisableCursorPositioning bool `json:"disable_cursor_positioning,omitempty"`
PatchPwshBleed bool `json:"patch_pwsh_bleed,omitempty"`
// Deprecated
OSC99 bool `json:"osc99,omitempty"`

View file

@ -159,6 +159,8 @@ func (e *Engine) getTitleTemplateText() string {
}
func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
defer e.patchPowerShellBleed()
// when in bash, for rprompt blocks we need to write plain
// and wrap in escaped mode or the prompt will not render correctly
if e.Env.Shell() == shell.BASH && block.Type == RPrompt {
@ -248,3 +250,20 @@ func (e *Engine) renderBlock(block *Block, cancelNewline bool) {
e.rprompt, e.rpromptLength = block.RenderSegments()
}
}
func (e *Engine) patchPowerShellBleed() {
// when in PowerShell, we need to clear the line after the prompt
// to avoid the background being printed on the next line
// when at the end of the buffer.
// See https://github.com/JanDeDobbeleer/oh-my-posh/issues/65
if e.Env.Shell() != shell.PWSH && e.Env.Shell() != shell.PWSH5 {
return
}
// only do this when enabled
if !e.Config.PatchPwshBleed {
return
}
e.write(e.Writer.ClearAfter())
}

View file

@ -16,6 +16,11 @@ func New(flags *platform.Flags) *Engine {
env.Init()
cfg := LoadConfig(env)
if cfg.PatchPwshBleed {
patchPowerShellBleed(env.Shell(), flags)
}
env.Var = cfg.Var
flags.HasTransient = cfg.TransientPrompt != nil
@ -36,3 +41,20 @@ func New(flags *platform.Flags) *Engine {
return eng
}
func patchPowerShellBleed(sh string, flags *platform.Flags) {
// when in PowerShell, and force patching the bleed bug
// we need to reduce the terminal width by 1 so the last
// character isn't cut off by the ANSI escape sequences
// See https://github.com/JanDeDobbeleer/oh-my-posh/issues/65
if sh != shell.PWSH && sh != shell.PWSH5 {
return
}
// only do this when relevant
if flags.TerminalWidth <= 0 {
return
}
flags.TerminalWidth--
}

View file

@ -271,7 +271,7 @@ func (c *commandCache) get(command string) (string, bool) {
type Shell struct {
CmdFlags *Flags
Var map[string]interface{}
Var SimpleMap
cwd string
cmdCache *commandCache

View file

@ -66,8 +66,8 @@ func (env *Shell) TerminalWidth() (int, error) {
env.Error(err)
}
env.DebugF("terminal width: %d", width)
env.CmdFlags.TerminalWidth = int(width)
env.DebugF("terminal width: %d", env.CmdFlags.TerminalWidth)
return env.CmdFlags.TerminalWidth, err
}

View file

@ -1,5 +1,5 @@
---
id: overview
id: general
title: General
sidebar_label: General
---
@ -135,6 +135,7 @@ For example, the following is a valid `--config` flag:
| `var` | `map[string]any` | config variables to use in [templates][templates]. Can be any value |
| `shell_integration` | `boolean` | enable shell integration using FinalTerm's OSC sequences. Works in bash, cmd (Clink v1.14.25+), fish, powershell and zsh |
| `disable_cursor_positioning` | `boolean` | disable fetching the cursor position in bash and zsh in case of unwanted side-effects |
| `patch_pwsh_bleed` | `boolean` | patch a PowerShell bug where the background colors bleed into the next line at the end of the buffer (can be removed when [this][pwsh-bleed] is merged) |
### JSON Schema Validation
@ -188,3 +189,4 @@ Converters won't catch this change, so you will need to adjust manually.
[colors]: /docs/configuration/colors
[accent]: /docs/configuration/colors#standard-colors
[templates]: /docs/configuration/templates#config-variables
[pwsh-bleed]: https://github.com/PowerShell/PowerShell/pull/19019

View file

@ -32,7 +32,7 @@ module.exports = {
type: "category",
label: "⚙️ Configuration",
items: [
"configuration/overview",
"configuration/general",
"configuration/block",
"configuration/segment",
"configuration/sample",