Skip to content

Commit 8155c4b

Browse files
fix: powershell invocation
1 parent 9c1bf12 commit 8155c4b

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

docs/docs/installation.mdx

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,30 @@ Edit `$PROFILE` in your preferred PowerShell version and add the following lines
8484

8585
```powershell
8686
[ScriptBlock]$Prompt = {
87-
$realLASTEXITCODE = $global:LASTEXITCODE
88-
& "C:\tools\oh-my-posh.exe" -config "~/downloadedtheme.json" -error $realLASTEXITCODE -pwd $PWD
89-
$global:LASTEXITCODE = $realLASTEXITCODE
90-
Remove-Variable realLASTEXITCODE -Confirm:$false
87+
$realLASTEXITCODE = $global:LASTEXITCODE
88+
if ($realLASTEXITCODE -isnot [int]) {
89+
$realLASTEXITCODE = 0
90+
}
91+
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
92+
$startInfo.FileName = "C:\tools\oh-my-posh.exe"
93+
$config = $global:PoshSettings.Theme
94+
$startInfo.Arguments = "-config C:\Users\User\Downloads\downloadedtheme.json -pwd $PWD -error $realLASTEXITCODE"
95+
$startInfo.Environment["TERM"] = "xterm-256color"
96+
$startInfo.CreateNoWindow = $true
97+
$startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8
98+
$startInfo.RedirectStandardOutput = $true
99+
$startInfo.UseShellExecute = $false
100+
if ($PWD.Provider.Name -eq "FileSystem") {
101+
$startInfo.WorkingDirectory = $PWD
102+
}
103+
$process = New-Object System.Diagnostics.Process
104+
$process.StartInfo = $startInfo
105+
$process.Start() | Out-Null
106+
$standardOut = $process.StandardOutput.ReadToEnd()
107+
$process.WaitForExit()
108+
$standardOut
109+
$global:LASTEXITCODE = $realLASTEXITCODE
110+
Remove-Variable realLASTEXITCODE -Confirm:$false
91111
}
92112
Set-Item -Path Function:prompt -Value $Prompt -Force
93113
```

engine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func (e *engine) render() {
122122
for _, block := range e.settings.Blocks {
123123
// if line break, append a line break
124124
if block.Type == LineBreak {
125-
fmt.Print(e.renderer.lineBreak())
125+
fmt.Print("\n")
126126
continue
127127
}
128128
if block.VerticalOffset != 0 {

packages/powershell/oh-my-posh/oh-my-posh.psm1

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@ if (!$IsWindows) {
2323
$executable = Get-PoshCommand
2424
Invoke-Expression -Command "chmod +x $executable"
2525
}
26-
if ($IsWindows) {
27-
# When this is not set, outputted fonts aren't rendered correctly in some terminals for the prompt function
28-
# It can also fail when we're not running in a console (and then, well, you're on your own)
29-
try {
30-
[console]::OutputEncoding = New-Object System.Text.UTF8Encoding
31-
}
32-
catch {
33-
Write-Host "oh-my-posh: unable to set output encoding to UTF8, fonts might be rendered incorrectly."
34-
}
35-
# Not running it beforehand in the terminal will fail the prompt somehow
36-
$poshCommand = Get-PoshCommand
37-
& $poshCommand | Out-Null
38-
}
3926

4027
function Set-PoshContext {}
4128

@@ -58,10 +45,28 @@ function Set-PoshPrompt {
5845

5946
[ScriptBlock]$Prompt = {
6047
$realLASTEXITCODE = $global:LASTEXITCODE
61-
$poshCommand = Get-PoshCommand
48+
if ($realLASTEXITCODE -isnot [int]) {
49+
$realLASTEXITCODE = 0
50+
}
51+
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
52+
$startInfo.FileName = Get-PoshCommand
6253
$config = $global:PoshSettings.Theme
54+
$startInfo.Arguments = "-config $config -pwd $PWD -error $realLASTEXITCODE"
55+
$startInfo.Environment["TERM"] = "xterm-256color"
56+
$startInfo.CreateNoWindow = $true
57+
$startInfo.StandardOutputEncoding = [System.Text.Encoding]::UTF8
58+
$startInfo.RedirectStandardOutput = $true
59+
$startInfo.UseShellExecute = $false
60+
if ($PWD.Provider.Name -eq "FileSystem") {
61+
$startInfo.WorkingDirectory = $PWD
62+
}
63+
$process = New-Object System.Diagnostics.Process
64+
$process.StartInfo = $startInfo
6365
Set-PoshContext
64-
& $poshCommand -config $config -error $realLASTEXITCODE -pwd $PWD
66+
$process.Start() | Out-Null
67+
$standardOut = $process.StandardOutput.ReadToEnd()
68+
$process.WaitForExit()
69+
$standardOut
6570
$global:LASTEXITCODE = $realLASTEXITCODE
6671
Remove-Variable realLASTEXITCODE -Confirm:$false
6772
}

renderer.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,6 @@ func (r *Renderer) lenWithoutANSI(str string) int {
130130
return count
131131
}
132132

133-
func (r *Renderer) lineBreak() string {
134-
if r.shell == "pwsh" {
135-
return "\x1b[1000C "
136-
}
137-
return "\n"
138-
}
139-
140133
func (r *Renderer) carriageForward() string {
141134
return fmt.Sprintf(r.formats.left, 1000)
142135
}

0 commit comments

Comments
 (0)