From a9e2ba61fa3ebffe0155086bc7674f1a7007722a Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Thu, 31 Aug 2023 10:51:40 +0200 Subject: [PATCH] fix(windows): escape PWD in emulated shells relates to #4208 --- src/engine/engine.go | 6 ++++++ src/engine/engine_test.go | 30 +++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/engine/engine.go b/src/engine/engine.go index 870935ed..1191c633 100644 --- a/src/engine/engine.go +++ b/src/engine/engine.go @@ -91,6 +91,12 @@ func (e *Engine) pwd() { } cwd := e.Env.Pwd() + + // in BASH, we need to escape the path + if e.Env.Shell() == shell.BASH { + cwd = strings.ReplaceAll(cwd, `\`, `\\`) + } + // Backwards compatibility for deprecated OSC99 if e.Config.OSC99 { e.write(e.Writer.ConsolePwd(ansi.OSC99, "", "", cwd)) diff --git a/src/engine/engine_test.go b/src/engine/engine_test.go index 87c0447f..4f45603c 100644 --- a/src/engine/engine_test.go +++ b/src/engine/engine_test.go @@ -49,22 +49,34 @@ func TestPrintPWD(t *testing.T) { cases := []struct { Case string Expected string - PWD string + Config string + Pwd string + Shell string OSC99 bool }{ {Case: "Empty PWD"}, - {Case: "OSC99", PWD: ansi.OSC99, Expected: "\x1b]9;9;pwd\x1b\\"}, - {Case: "OSC7", PWD: ansi.OSC7, Expected: "\x1b]7;file://host/pwd\x1b\\"}, - {Case: "OSC51", PWD: ansi.OSC51, Expected: "\x1b]51;Auser@host:pwd\x1b\\"}, + {Case: "OSC99", Config: ansi.OSC99, Expected: "\x1b]9;9;pwd\x1b\\"}, + {Case: "OSC7", Config: ansi.OSC7, Expected: "\x1b]7;file://host/pwd\x1b\\"}, + {Case: "OSC51", Config: ansi.OSC51, Expected: "\x1b]51;Auser@host:pwd\x1b\\"}, {Case: "Deprecated OSC99", OSC99: true, Expected: "\x1b]9;9;pwd\x1b\\"}, - {Case: "Template (empty)", PWD: "{{ if eq .Shell \"pwsh\" }}osc7{{ end }}"}, - {Case: "Template (non empty)", PWD: "{{ if eq .Shell \"shell\" }}osc7{{ end }}", Expected: "\x1b]7;file://host/pwd\x1b\\"}, + {Case: "Template (empty)", Config: "{{ if eq .Shell \"pwsh\" }}osc7{{ end }}"}, + {Case: "Template (non empty)", Config: "{{ if eq .Shell \"shell\" }}osc7{{ end }}", Expected: "\x1b]7;file://host/pwd\x1b\\"}, + { + Case: "OSC99 Bash", + Pwd: `C:\Users\user\Documents\GitHub\oh-my-posh`, + Config: ansi.OSC99, + Shell: shell.BASH, + Expected: "\x1b]9;9;C:\\\\Users\\\\user\\\\Documents\\\\GitHub\\\\oh-my-posh\x1b\\", + }, } for _, tc := range cases { env := new(mock.MockedEnvironment) - env.On("Pwd").Return("pwd") - env.On("Shell").Return("shell") + if len(tc.Pwd) == 0 { + tc.Pwd = "pwd" + } + env.On("Pwd").Return(tc.Pwd) + env.On("Shell").Return(tc.Shell) env.On("User").Return("user") env.On("Host").Return("host", nil) env.On("DebugF", mock2.Anything, mock2.Anything).Return(nil) @@ -78,7 +90,7 @@ func TestPrintPWD(t *testing.T) { engine := &Engine{ Env: env, Config: &Config{ - PWD: tc.PWD, + PWD: tc.Config, OSC99: tc.OSC99, }, Writer: writer,