Toggle showing venv with Oh my posh

I have already tried Oh My Posh to make my PowerShell look cool, simple, and essentials
and this is my conf.
{
"$schema": "https://raw.githubusercontent.com/JanDeDobbeleer/oh-my-posh/main/themes/schema.json",
"blocks": [
{
"alignment": "left",
"segments": [
{
"background": "#83769c",
"foreground": "#1C1E21",
"properties": {
"always_enabled": true
},
"leading_diamond": "\ue0b6",
"style": "diamond",
"template": "\ueba2 {{ .FormattedMs }}\u2800",
"type": "executiontime"
},
{
"background": "#609EA2",
"foreground": "#1C1E21",
"leading_diamond": "\ue0b6",
"powerline_symbol": "\ue0b0",
"properties": {
"folder_separator_icon": " \ue0b1 ",
"home_icon": "~",
"style": "folder"
},
"style": "powerline",
"template": " \uea83 {{ .Path }} ",
"type": "path"
},
{
"background": "#FF7F50",
"foreground": "#1C1E21",
"powerline_symbol": "\ue0b0",
"style": "powerline",
"template": " {{ .HEAD }}",
"type": "git"
},
{
"background": "#6CA35E",
"foreground": "#1C1E21",
"powerline_symbol": "\ue0b0",
"properties": {
"fetch_version": true
},
"style": "powerline",
"template": " \ue718 {{ if .PackageManagerIcon }}{{ .PackageManagerIcon }} {{ end }}{{ .Full }} ",
"type": "node"
},
{
"background": "#6495ED",
"foreground": "#1C1E21",
"powerline_symbol": "\ue0b0",
"properties": {
"display_mode": "files",
"fetch_virtual_env": false
},
"style": "powerline",
"template": " \ue235 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} ",
"type": "python"
},
{
"background": "#ffff66",
"foreground": "#1C1E21",
"powerline_symbol": "\ue0b0",
"style": "powerline",
"template": " \uf0ad ",
"type": "root"
}
],
"type": "prompt"
}
],
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 3
}
The Venv problem
when I go to the directory that has Python I see the Python full version, but when I activate a Venv I don’t know if it’s active or not.
so first thing I started to debug the Python segment block in Oh My Posh conf and after I saw the docs about Python I found that the default value of fetch_virtual_env
is right so I deleted it and I changed the display_mode
to context to display the segment when the environment or files is active
"properties": {
"display_mode": "context",
},
now let’s go to the template variable "template": " \ue235 {{ if .Error }}{{ .Error }}{{ else }}{{ .Full }}{{ end }} ",
it writes the Python logo then if there is no error it inserts the full Python version I only want the {{ .Major }}.{{ .Minor }}
instead of {{ .Full }}
now in docs
|
| the virtual environment name (if present) |
which seems promising I tried it {{ if .Error }}{{ .Error }}{{ else }}{{ if .Venv }}(venv) {{ end }}{{ .Major }}.{{ .Minor }}{{ end }}
it will show (venv) if . Venv is present.
Nice, it works
It doesn’t go away 😭
this is because I’m on PowerShell and .bat files don’t work here it works in CMD and I can’t find a deactivate script ending with .ps1
How Activate.ps1 and deactivate works
After some frustration, I discover that typing deactivate
in PowerShell works perfectly. Here’s why
When you run Activate.ps
1
to activate the venv in PowerShell, it does two things:
Sets
VIRTUAL_ENV: $env:VIRTUAL_ENV = "C:\path\to\venv"
defines a
deactivate
function in your session: This function modifies the environment variables directly in PowerShell, ensuringVIRTUAL_ENV
is unset and thePATH
is cleaned up.
Good Bye 😁
Subscribe to my newsletter
Read articles from Abdulhamed Eid directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
