forked from JanDeDobbeleer/oh-my-posh2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oh-my-posh.psm1
202 lines (174 loc) · 5.68 KB
/
oh-my-posh.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#requires -Version 2 -Modules posh-git
. "$PSScriptRoot\defaults.ps1"
. "$PSScriptRoot\Helpers\PoshGit.ps1"
. "$PSScriptRoot\Helpers\Prompt.ps1"
<#
.SYNOPSIS
Generates the prompt before each line in the console
#>
function Set-Prompt {
Import-Module $sl.CurrentThemeLocation -Force
[ScriptBlock]$Prompt = {
$lastCommandFailed = $global:error.Count -gt $sl.ErrorCount
$sl.ErrorCount = $global:error.Count
#Start the vanilla posh-git when in a vanilla window, else: go nuts
if(Test-IsVanillaWindow) {
Write-Host -Object ($pwd.ProviderPath) -NoNewline
Write-VcsStatus
}
Reset-CursorPosition
$prompt = (Write-Theme -lastCommandFailed $lastCommandFailed)
if ($env:ConEmuANSI -eq "ON") {
$location = Get-Location
$folder = (Get-Item $location.Path).Name
$prompt += "$([char]27)]2;$($folder)$([char]7)"
if ($location.Provider.Name -eq "FileSystem") {
$prompt += "$([char]27)]9;9;`"$($location.Path)`"$([char]7)"
}
}
$prompt
}
Set-Item -Path Function:prompt -Value $Prompt -Force
}
function global:Write-WithPrompt() {
param(
[string]
$command
)
$lastCommandFailed = $global:error.Count -gt $sl.ErrorCount
$sl.ErrorCount = $global:error.Count
if(Test-IsVanillaWindow) {
Write-ClassicPrompt -command $command
return
}
Write-Theme -lastCommandFailed $lastCommandFailed -with $command
}
function Show-ThemeColors {
##############################
#.SYNOPSIS
# Show Current Theme Colors
#
#.DESCRIPTION
# Good for checking if your current color mappings
# work well with the theme.
#
##############################
Write-Host -Object ''
$sl.Colors.Keys | Sort-Object | ForEach-Object { Write-ColorPreview -text ("{0,-35}" -f $_ ) -color $sl.Colors[$_] }
Write-Host -Object ''
}
function Show-ThemeSymbols {
##############################
#.SYNOPSIS
# Show Current Theme Symbols
#
#.DESCRIPTION
# Good for checking if your current font supports
# all the symbols the theme uses.
#
##############################
Write-Host -Object "`n--PromptSymbols--`n"
$sl.PromptSymbols.Keys | Sort-Object | ForEach-Object { Write-Host -Object ("{0,3} {1}" -f $sl.PromptSymbols[$_], $_) }
Write-Host -Object ''
Write-Host -Object "`n--GitSymbols--`n"
$sl.GitSymbols.Keys | Sort-Object | ForEach-Object { Write-Host -Object ("{0,3} {1}" -f $sl.GitSymbols[$_], $_) }
Write-Host -Object ''
}
function Write-ColorPreview {
param
(
[string]
$text,
[ConsoleColor]
$color
)
Write-Host -Object $text -NoNewline
Write-Host -Object ' ' -BackgroundColor $color
}
function Show-Colors {
for($i = 0; $i -lt 16; $i++) {
$color = [ConsoleColor]$i
Write-Host -Object $color -BackgroundColor $i
}
}
function Set-Theme {
param(
[Parameter(Mandatory=$true)]
[string]
$name
)
if (Test-Path "$($sl.MyThemesLocation)\$($name).psm1") {
$sl.CurrentThemeLocation = "$($sl.MyThemesLocation)\$($name).psm1"
}
elseif (Test-Path "$PSScriptRoot\Themes\$($name).psm1") {
$sl.CurrentThemeLocation = "$PSScriptRoot\Themes\$($name).psm1"
}
elseif (Test-Path "$name") {
$sl.CurrentThemeLocation = "$name"
}
else {
Write-Host ''
Write-Warning "Theme $name not found. Available themes are:"
Get-Theme
}
Set-Prompt
}
# 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 Get-Theme {
##############################
#.SYNOPSIS
# Get available theme(s)
#.DESCRIPTION
# Shows available themes, as well as their type and location
# - Defaults (shipped with module)
# - User (user defined themes)
##############################
$themes = @()
if (Test-Path "$($ThemeSettings.MyThemesLocation)\*") {
Get-ChildItem -Path "$($ThemeSettings.MyThemesLocation)\*" -Include '*.psm1' -Exclude Tools.ps1 | ForEach-Object -Process {
$themes += [PSCustomObject]@{
Name = $_.BaseName
Type = "User"
Location = $_.FullName
}
}
}
Get-ChildItem -Path "$PSScriptRoot\Themes\*" -Include '*.psm1' -Exclude Tools.ps1 | Sort-Object Name | ForEach-Object -Process {
$themes += [PSCustomObject]@{
Name = $_.BaseName
Type = "Defaults"
Location = $_.FullName
}
}
$themes
}
function ThemeCompletion {
param(
$commandName,
$parameterName,
$wordToComplete,
$commandAst,
$fakeBoundParameter
)
$themes = Get-Theme
$themes |
Where-Object { $_.Name.ToLower().StartsWith($wordToComplete.ToLower()); } |
Select-Object -Unique -ExpandProperty Name |
ForEach-Object { New-CompletionResult -CompletionText $_ }
}
Register-ArgumentCompleter `
-CommandName Set-Theme `
-ParameterName name `
-ScriptBlock $function:ThemeCompletion
$sl = $global:ThemeSettings #local settings
$sl.ErrorCount = $global:error.Count