oh-my-posh/packages/powershell/oh-my-posh/oh-my-posh.psm1
Jan De Dobbeleer 7e1a5ecdc5 refactor(pwsh): remove Set-Theme from V2 deprecation list
this conflicts with other tooling such as Terminal-Icons that also
has a Set-Theme cmdlet. Overriding this will break that functionality.

relates to #382
2021-02-07 07:39:50 +01:00

167 lines
5.3 KiB
PowerShell

<#
.SYNOPSIS
Generates the prompt before each line in the console
#>
function Get-PoshCommand {
$poshCommand = "$PSScriptRoot/bin/posh-windows-amd64.exe"
if ($IsMacOS) {
$poshCommand = "$PSScriptRoot/bin/posh-darwin-amd64"
}
if ($IsLinux) {
$poshCommand = "$PSScriptRoot/bin/posh-linux-amd64"
}
return $poshCommand
}
function Set-ExecutablePermissions {
# Set the right binary to executable before doing anything else
# Permissions don't need to be set on Windows
if ($PSVersionTable.PSEdition -ne "Core" -or $IsWindows) {
return
}
$executable = Get-PoshCommand
if (-Not (Test-Path $executable)) {
# This should only happend with a corrupt installation
Write-Warning "Executable at $executable was not found"
return
}
# Check the permissions on the file
$permissions = ((ls -l $executable) -split ' ')[0] # $permissions will be something like '-rw-r--r--'
if ((id -u) -eq 0) {
# Running as root, give global executable permissions if needed
$hasWrite = $permissions[2] -eq 'w'
$hasExecutable = $permissions[3] -eq 'x'
if ($hasWrite -and -not $hasExecutable) {
Invoke-Expression -Command "chmod g+x $executable"
}
return
}
# Running as user, give user executable permissions if needed
$hasWrite = $permissions[8] -eq 'w'
$hasExecutable = $permissions[9] -eq 'x'
if ($hasWrite -and -not $hasExecutable) {
Invoke-Expression -Command "chmod +x $executable"
}
}
function Set-PoshPrompt {
param(
[Parameter(Mandatory = $false)]
[string]
$Theme
)
$config = ""
if (Test-Path "$PSScriptRoot/themes/$Theme.omp.json") {
$config = "$PSScriptRoot/themes/$Theme.omp.json"
}
elseif (Test-Path $Theme) {
$config = (Resolve-Path -Path $Theme).Path
}
else {
$config = "$PSScriptRoot/themes/jandedobbeleer.omp.json"
}
$poshCommand = Get-PoshCommand
Invoke-Expression (& $poshCommand --init --shell=pwsh --config="$config")
}
function Get-PoshThemes {
$esc = [char]27
$consoleWidth = $Host.UI.RawUI.WindowSize.Width
$logo = @'
__ _____ _ ___ ___ ______ _ __
/ / | _ | | | \/ | | ___ \ | | \ \
/ / | | | | |__ | . . |_ _ | |_/ /__ ___| |__ \ \
< < | | | | '_ \ | |\/| | | | | | __/ _ \/ __| '_ \ > >
\ \ \ \_/ / | | | | | | | |_| | | | | (_) \__ \ | | | / /
\_\ \___/|_| |_| \_| |_/\__, | \_| \___/|___/_| |_| /_/
__/ |
|___/
'@
Write-Host $logo
$poshCommand = Get-PoshCommand
Get-ChildItem -Path "$PSScriptRoot\themes\*" -Include '*.omp.json' | Sort-Object Name | ForEach-Object -Process {
Write-Host ("=" * $consoleWidth)
Write-Host "$esc[1m$($_.BaseName)$esc[0m"
Write-Host ""
& $poshCommand -config $($_.FullName) -pwd $PWD
Write-Host ""
}
Write-Host ("=" * $consoleWidth)
}
function Write-PoshTheme {
$config = $global:PoshSettings.Theme
$poshCommand = Get-PoshCommand
& $poshCommand -config $config -print-config
}
# Helper function to create argument completion results
function New-CompletionResult {
param(
[Parameter(Mandatory)]
[string]$CompletionText,
[string]$ListItemText = $CompletionText,
[System.Management.Automation.CompletionResultType]$CompletionResultType = [System.Management.Automation.CompletionResultType]::ParameterValue,
[string]$ToolTip = $CompletionText
)
New-Object System.Management.Automation.CompletionResult $CompletionText, $ListItemText, $CompletionResultType, $ToolTip
}
function ThemeCompletion {
param(
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameter
)
$themes = Get-ChildItem -Path "$PSScriptRoot\themes\*" -Include '*.omp.json' | Sort-Object Name | Select-Object -Property @{
label='BaseName'
expression={$_.BaseName.Replace('.omp', '')}
}
$themes |
Where-Object { $_.BaseName.ToLower().StartsWith($wordToComplete.ToLower()); } |
Select-Object -Unique -ExpandProperty BaseName |
ForEach-Object { New-CompletionResult -CompletionText $_ }
}
Set-ExecutablePermissions
Register-ArgumentCompleter `
-CommandName Set-PoshPrompt `
-ParameterName Theme `
-ScriptBlock $function:ThemeCompletion
# V2 compatibility functions
# These should be removed at a certain point in time
# but to facilitate ease of transition they are kept
# as long as issues/feature requests keep popping up
function Get-PoshInfoForV2Users {
Write-Host @'
Hi there!
It seems you're using an oh-my-posh V2 cmdlet while running V3.
To migrate your current setup to V3, have a look the documentation.
https://ohmyposh.dev/docs/upgrading
'@
}
Set-Alias -Name Set-Prompt -Value Get-PoshInfoForV2Users -Force
Set-Alias -Name Get-ThemesLocation -Value Get-PoshInfoForV2Users -Force
Set-Alias -Name Get-Theme -Value Get-PoshInfoForV2Users -Force
Set-Alias -Name Show-ThemeSymbols -Value Get-PoshInfoForV2Users -Force
Set-Alias -Name Show-ThemeColors -Value Get-PoshInfoForV2Users -Force
Set-Alias -Name Show-Colors -Value Get-PoshInfoForV2Users -Force
Set-Alias -Name Write-ColorPreview -Value Get-PoshInfoForV2Users -Force