Skip to content

Commit

Permalink
fix(python): disable Python prompt hijacking by default
Browse files Browse the repository at this point in the history
  • Loading branch information
JanDeDobbeleer committed Jan 7, 2025
1 parent bd0e902 commit 1bc7163
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.bash
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export POSH_SESSION_ID=::SESSION_ID::
export CONDA_PROMPT_MODIFIER=false
export OSTYPE=$OSTYPE

# disable all known python virtual environment prompts
export VIRTUAL_ENV_DISABLE_PROMPT=1
export PYENV_VIRTUALENV_DISABLE_PROMPT=1

# global variables
_omp_start_time=''
_omp_stack_count=0
Expand Down
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.elv
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ set-env POSH_SHELL elvish
set-env POSH_SHELL_VERSION $version
set-env POWERLINE_COMMAND oh-my-posh

# disable all known python virtual environment prompts
set-env VIRTUAL_ENV_DISABLE_PROMPT 1
set-env PYENV_VIRTUALENV_DISABLE_PROMPT 1

var _omp_executable = (external ::OMP::)
var _omp_status = 0
var _omp_no_status = 1
Expand Down
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.fish
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ set --global _omp_ftcs_marks 0
set --global _omp_transient_prompt 0
set --global _omp_prompt_mark 0

# disable all known python virtual environment prompts
set --global VIRTUAL_ENV_DISABLE_PROMPT 1
set --global PYENV_VIRTUALENV_DISABLE_PROMPT 1

# We use this to avoid unnecessary CLI calls for prompt repaint.
set --global _omp_new_prompt 1

Expand Down
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
os.setenv('POSH_SESSION_ID', '::SESSION_ID::')
os.setenv('POSH_SHELL', 'cmd')

-- disable all known python virtual environment prompts
os.setenv('VIRTUAL_ENV_DISABLE_PROMPT', '1')
os.setenv('PYENV_VIRTUALENV_DISABLE_PROMPT', '1')

-- Helper functions

local function get_priority_number(name, default)
Expand Down
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.nu
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ $env.POSH_SESSION_ID = (echo ::SESSION_ID::)
$env.POSH_SHELL = "nu"
$env.POSH_SHELL_VERSION = (version | get version)

# disable all known python virtual environment prompts
$env.VIRTUAL_ENV_DISABLE_PROMPT = 1
$env.PYENV_VIRTUALENV_DISABLE_PROMPT = 1

let _omp_executable: string = (echo ::OMP::)

# PROMPTS
Expand Down
34 changes: 24 additions & 10 deletions src/shell/scripts/omp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ if ($null -ne (Get-Module -Name "oh-my-posh-core")) {
Remove-Module -Name "oh-my-posh-core" -Force
}

# disable all known python virtual environment prompts
$env:VIRTUAL_ENV_DISABLE_PROMPT = 1
$env:PYENV_VIRTUALENV_DISABLE_PROMPT = 1

# Helper functions which need to be defined before the module is loaded
# See https://github.com/JanDeDobbeleer/oh-my-posh/discussions/2300
function global:Get-PoshStackCount {
Expand Down Expand Up @@ -66,7 +70,8 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
# ref-1: https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.argumentlist?view=net-6.0
# ref-2: https://docs.microsoft.com/en-us/powershell/scripting/whats-new/differences-from-windows-powershell?view=powershell-7.2#net-framework-vs-net-core
$Arguments | ForEach-Object -Process { $StartInfo.ArgumentList.Add($_) }
} else {
}
else {
# escape arguments manually in lower versions, refer to https://docs.microsoft.com/en-us/previous-versions/17w5ykft(v=vs.85)
$escapedArgs = $Arguments | ForEach-Object {
# escape N consecutive backslash(es), which are followed by a double quote, to 2N consecutive ones
Expand Down Expand Up @@ -154,7 +159,8 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
$script:TransientPrompt = $true
[Console]::OutputEncoding = [Text.Encoding]::UTF8
[Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()
} finally {
}
finally {
[Console]::OutputEncoding = $previousOutputEncoding
}
}
Expand Down Expand Up @@ -184,14 +190,16 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
if ($global:_ompAzure) {
try {
$env:POSH_AZURE_SUBSCRIPTION = Get-AzContext | ConvertTo-Json
} catch {}
}
catch {}
}

if ($global:_ompPoshGit) {
try {
$global:GitStatus = Get-GitStatus
$env:POSH_GIT_STATUS = $global:GitStatus | ConvertTo-Json
} catch {}
}
catch {}
}
}

Expand All @@ -216,7 +224,8 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
$invocationInfo = try {
# retrieve info of the most recent error
$global:Error[0] | Where-Object { $_ -ne $null } | Select-Object -ExpandProperty InvocationInfo
} catch { $null }
}
catch { $null }

# check if the last command caused the last error
if ($null -ne $invocationInfo -and $lastHistory.CommandLine -eq $invocationInfo.Line) {
Expand Down Expand Up @@ -260,7 +269,8 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
if ($global:NVS_ORIGINAL_LASTEXECUTIONSTATUS -is [bool]) {
# make it compatible with NVS auto-switching, if enabled
$script:OriginalLastExecutionStatus = $global:NVS_ORIGINAL_LASTEXECUTIONSTATUS
} else {
}
else {
$script:OriginalLastExecutionStatus = $?
}
# store the orignal last exit code
Expand Down Expand Up @@ -346,7 +356,8 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
# Workaround to prevent the text after cursor from disappearing when the tooltip is printed.
[Microsoft.PowerShell.PSConsoleReadLine]::Insert(' ')
[Microsoft.PowerShell.PSConsoleReadLine]::Undo()
} finally {}
}
finally {}
}
}

Expand All @@ -364,7 +375,8 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
$script:TooltipCommand = ''
Set-TransientPrompt
}
} finally {
}
finally {
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
if ($global:_ompFTCSMarks -and $executingCommand) {
# Write FTCS_COMMAND_EXECUTED after accepting the input - it should still happen before execution
Expand All @@ -382,7 +394,8 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
$script:TooltipCommand = ''
Set-TransientPrompt
}
} finally {
}
finally {
[Microsoft.PowerShell.PSConsoleReadLine]::CopyOrCancelLine()
}
}
Expand Down Expand Up @@ -479,7 +492,8 @@ New-Module -Name "oh-my-posh-core" -ScriptBlock {
$themes = Get-ChildItem -Path "$Path/*" -Include '*.omp.json' | Sort-Object Name
if ($List -eq $true) {
$themes | Select-Object @{ Name = 'hyperlink'; Expression = { Get-FileHyperlink -Uri $_.FullName } } | Format-Table -HideTableHeaders
} else {
}
else {
$nonFSWD = Get-NonFSWD
$stackCount = Get-PoshStackCount
$terminalWidth = Get-TerminalWidth
Expand Down
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.tcsh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ setenv POSH_SHELL_VERSION "$tcsh";
setenv POSH_SESSION_ID ::SESSION_ID::;
setenv OSTYPE "$OSTYPE";

# disable all known python virtual environment prompts
setenv VIRTUAL_ENV_DISABLE_PROMPT 1;
setenv PYENV_VIRTUALENV_DISABLE_PROMPT 1;

if ( ! $?_omp_enabled ) alias precmd '
set _omp_status = $status;
set _omp_execution_time = -1;
Expand Down
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.xsh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ $POSH_SESSION_ID = ::SESSION_ID::
$POSH_SHELL = "xonsh"
$POSH_SHELL_VERSION = $XONSH_VERSION

# disable all known python virtual environment prompts
$VIRTUAL_ENV_DISABLE_PROMPT = 1
$PYENV_VIRTUALENV_DISABLE_PROMPT = 1

_omp_executable = ::OMP::
_omp_history_length = 0

Expand Down
4 changes: 4 additions & 0 deletions src/shell/scripts/omp.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export CONDA_PROMPT_MODIFIER=false
export ZLE_RPROMPT_INDENT=0
export OSTYPE=$OSTYPE

# disable all known python virtual environment prompts
export VIRTUAL_ENV_DISABLE_PROMPT=1
export PYENV_VIRTUALENV_DISABLE_PROMPT=1

_omp_executable=::OMP::
_omp_tooltip_command=''

Expand Down

0 comments on commit 1bc7163

Please sign in to comment.