2020-09-11 23:42:26 -07:00
|
|
|
<#
|
|
|
|
.SYNOPSIS
|
|
|
|
Generates the prompt before each line in the console
|
|
|
|
#>
|
|
|
|
|
|
|
|
$global:PoshSettings = New-Object -TypeName PSObject -Property @{
|
2020-09-24 23:42:47 -07:00
|
|
|
Theme = "$PSScriptRoot\themes\jandedobbeleer.json"
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
# Set the right binary to executable before doing anything else
|
|
|
|
if (!$IsWindows) {
|
|
|
|
$executable = Get-PoshCommand
|
|
|
|
Invoke-Expression -Command "chmod +x $executable"
|
|
|
|
}
|
2020-09-18 05:50:56 -07:00
|
|
|
if ($IsWindows) {
|
|
|
|
# When this is not set, outputted fonts aren't rendered correctly in some terminals for the prompt function
|
|
|
|
[console]::OutputEncoding = New-Object System.Text.UTF8Encoding
|
2020-09-19 23:53:54 -07:00
|
|
|
# Not running it beforehand in the terminal will fail the prompt somehow
|
|
|
|
$poshCommand = Get-PoshCommand
|
2020-09-20 22:49:34 -07:00
|
|
|
& $poshCommand | Out-Null
|
2020-09-18 05:50:56 -07:00
|
|
|
}
|
2020-09-11 23:42:26 -07:00
|
|
|
|
|
|
|
function Set-PoshPrompt {
|
|
|
|
param(
|
2020-09-19 23:53:14 -07:00
|
|
|
[Parameter(Mandatory = $false)]
|
2020-09-11 23:42:26 -07:00
|
|
|
[string]
|
|
|
|
$Theme
|
|
|
|
)
|
|
|
|
|
2020-09-24 23:42:47 -07:00
|
|
|
if (Test-Path "$PSScriptRoot/themes/$Theme.json") {
|
|
|
|
$global:PoshSettings.Theme = "$PSScriptRoot/themes/$Theme.json"
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|
|
|
|
elseif (Test-Path $Theme) {
|
2020-09-19 23:53:14 -07:00
|
|
|
$global:PoshSettings.Theme = $Theme
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|
2020-09-20 10:39:25 -07:00
|
|
|
else {
|
2020-09-24 23:42:47 -07:00
|
|
|
$global:PoshSettings.Theme = "$PSScriptRoot/themes/jandedobbeleer.json"
|
2020-09-20 10:39:25 -07:00
|
|
|
}
|
2020-09-11 23:42:26 -07:00
|
|
|
|
|
|
|
[ScriptBlock]$Prompt = {
|
|
|
|
$realLASTEXITCODE = $global:LASTEXITCODE
|
|
|
|
$poshCommand = Get-PoshCommand
|
|
|
|
$config = $global:PoshSettings.Theme
|
2020-09-20 08:49:20 -07:00
|
|
|
& $poshCommand -config $config -error $realLASTEXITCODE
|
2020-09-11 23:42:26 -07:00
|
|
|
$global:LASTEXITCODE = $realLASTEXITCODE
|
|
|
|
Remove-Variable realLASTEXITCODE -Confirm:$false
|
|
|
|
}
|
|
|
|
Set-Item -Path Function:prompt -Value $Prompt -Force
|
|
|
|
}
|
|
|
|
|
|
|
|
function Get-PoshThemes {
|
|
|
|
$esc = [char]27
|
|
|
|
$consoleWidth = $Host.UI.RawUI.WindowSize.Width
|
|
|
|
$logo = @'
|
|
|
|
__ _ __
|
|
|
|
/ / | | \ \
|
|
|
|
/ / __ _ ___ _ __ ___ _ _ _ __ ___ ___| |__ \ \
|
|
|
|
< < / _` |/ _ \ | '_ ` _ \| | | | | '_ \ / _ \/ __| '_ \ > >
|
|
|
|
\ \ | (_| | (_) | | | | | | | |_| | | |_) | (_) \__ \ | | | / /
|
|
|
|
\_\ \__, |\___/ |_| |_| |_|\__, | | .__/ \___/|___/_| |_| /_/
|
|
|
|
__/ | __/ | | |
|
|
|
|
|___/ |___/ |_|
|
|
|
|
|
|
|
|
'@
|
|
|
|
Write-Host $logo
|
|
|
|
$poshCommand = Get-PoshCommand
|
2020-09-24 23:42:47 -07:00
|
|
|
Get-ChildItem -Path "$PSScriptRoot\themes\*" -Include '*.json' | Sort-Object Name | ForEach-Object -Process {
|
2020-09-11 23:42:26 -07:00
|
|
|
Write-Host ("=" * $consoleWidth)
|
|
|
|
Write-Host "$esc[1m$($_.BaseName)$esc[0m"
|
|
|
|
Write-Host ""
|
2020-09-20 08:49:20 -07:00
|
|
|
& $poshCommand -config $($_.FullName)
|
2020-09-11 23:42:26 -07:00
|
|
|
Write-Host ""
|
|
|
|
}
|
|
|
|
Write-Host ("=" * $consoleWidth)
|
|
|
|
}
|
|
|
|
|
|
|
|
function Write-PoshTheme {
|
2020-09-20 22:50:11 -07:00
|
|
|
$config = $global:PoshSettings.Theme
|
2020-09-11 23:42:26 -07:00
|
|
|
$poshCommand = Get-PoshCommand
|
2020-09-20 22:50:11 -07:00
|
|
|
& $poshCommand -config $config -print-config
|
2020-09-11 23:42:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
# 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
|
|
|
|
)
|
2020-09-24 23:42:47 -07:00
|
|
|
$themes = Get-ChildItem -Path "$PSScriptRoot\themes\*" -Include '*.json' | Sort-Object Name | Select-Object -Property BaseName
|
2020-09-11 23:42:26 -07:00
|
|
|
$themes |
|
|
|
|
Where-Object { $_.BaseName.ToLower().StartsWith($wordToComplete.ToLower()); } |
|
|
|
|
Select-Object -Unique -ExpandProperty BaseName |
|
|
|
|
ForEach-Object { New-CompletionResult -CompletionText $_ }
|
|
|
|
}
|
|
|
|
|
|
|
|
Register-ArgumentCompleter `
|
|
|
|
-CommandName Set-PoshPrompt `
|
|
|
|
-ParameterName Theme `
|
|
|
|
-ScriptBlock $function:ThemeCompletion
|