Skip to content

Instantly share code, notes, and snippets.

@jesserobertson
Last active September 7, 2020 22:46
Show Gist options
  • Save jesserobertson/9374e22e1bfe4effa03a6bb98d228812 to your computer and use it in GitHub Desktop.
Save jesserobertson/9374e22e1bfe4effa03a6bb98d228812 to your computer and use it in GitHub Desktop.
Mod of posh pure theme to include conda env information in prompt (a bit slow but ¯\_(ツ)_/¯)
#requires -Version 2 -Modules posh-git
function Get-CondaEnv {
$condaEnvInfo = (
Invoke-Conda env list
| Select-String -Pattern '^(\w+)\s+\*\s+(.*)$' # name, then prefix
)
return $condaEnvInfo.matches.groups[1]
}
function Test-CommandExists {
param(
[string] $Command,
[switch] $Verbose
)
# Set error action preference
$oldPreference = $ErrorActionPreference
$ErrorActionPreference = 'stop'
# Check for command
try {
if (Get-Command $Command) {
if ($Verbose) {
Write-Host "Found ${Command} in $($(Get-Command $Command).Source)"
}
return $true
}
}
catch {
if ($Verbose) {
Write-Host "$Command does not exist"
}
return $false
}
finally {
$ErrorActionPreference = $oldPreference
}
}
function Write-Theme {
param(
[bool] $lastCommandFailed,
[string] $with
)
#check the last command state and indicate if failed
$promtSymbolColor = $sl.Colors.PromptSymbolColor
If ($lastCommandFailed) {
$promtSymbolColor = $sl.Colors.WithForegroundColor
}
# Writes the drive portion
$drive = Get-FullPath -dir $pwd
$prompt += Write-Prompt -Object $drive -ForegroundColor $sl.Colors.DriveForegroundColor
$prompt += Write-Prompt -Object ' '
$status = Get-VCSStatus
if ($status) {
$prompt += Write-Prompt -Object "$($status.Branch)" -ForegroundColor $sl.Colors.WithForegroundColor
if ($status.Working.Length -gt 0) {
$prompt += Write-Prompt -Object (" " + $sl.PromptSymbols.GitDirtyIndicator) -ForegroundColor $sl.Colors.GitDefaultColor
}
}
# Write out conda environment
if (Test-CommandExists Invoke-Conda) {
if (!$env:CONDA_ENV) { $env:CONDA_ENV = Get-CondaEnv }
$prompt += Write-Prompt -Object " (🐍 $env:CONDA_ENV)" -ForegroundColor $sl.Colors.DriveForegroundColor
}
# New line
$prompt += Set-Newline
# Writes the postfixes to the prompt
$prompt += Write-Prompt -Object ($sl.PromptSymbols.PromptIndicator) -ForegroundColor $promtSymbolColor
$prompt += ' '
$prompt
}
$sl = $global:ThemeSettings #local settings
$sl.PromptSymbols.PromptIndicator = [char]::ConvertFromUtf32(0x276f)
$sl.Colors.PromptSymbolColor = [ConsoleColor]::Green
$sl.Colors.PromptHighlightColor = [ConsoleColor]::Blue
$sl.Colors.DriveForegroundColor = [ConsoleColor]::Cyan
$sl.Colors.WithForegroundColor = [ConsoleColor]::Red
$sl.PromptSymbols.GitDirtyIndicator = [char]::ConvertFromUtf32(10007)
$sl.Colors.GitDefaultColor = [ConsoleColor]::Yellow
@jesserobertson
Copy link
Author

Stick this in \path\to\PoshThemes\pure-python.psm1. Find the path to your posh themes by printing $ThemeSettings.

@jesserobertson
Copy link
Author

example:

~\Developer\someCode master ✗ (🐍 someCodeConda)
❯

@jesserobertson
Copy link
Author

Now stores the conda environment in $env:CONDA_ENV rather than running a query every time the prompt refreshes. Much better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment