-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAdd-WTColorScheme.ps1
207 lines (181 loc) · 7.5 KB
/
Add-WTColorScheme.ps1
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
203
204
205
206
207
function Add-WTColorScheme
{
<#
.Synopsis
Adds a Windows Terminal Color Scheme
.Description
Adds a Windows Terminal Color Scheme, including any scheme from https://iterm2colorschemes.com/
.Example
Add-WTColorScheme -Name AdventureTime
.Link
https://iterm2colorschemes.com/
#>
[OutputType([Nullable])]
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='Medium')]
param(
# The name of the color scheme.
# If the name exists on https://iterm2colorschemes.com/, then no other parameters are needed.
[Parameter(Mandatory,ValueFromPipelineByPropertyName)]
[string]
$Name,
# The scheme's definition of the color Black.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$Black = '#010101',
# The scheme's definition of the color Red.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$Red = '#dd0000',
# The scheme's definition of the color Green.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$Green = '#00dd00',
# The scheme's definition of the color Yellow.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$Yellow = '#dddd00',
# The scheme's definition of the color Blue.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$Blue = '#0000dd',
# The scheme's definition of the color Cyan.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$Cyan = '#00dddd',
# The scheme's definition of the color Purple.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[Alias('Magenta')]
[string]
$Purple = '#dd00dd',
# The scheme's definition of the color White.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$White = '#efefef',
# The scheme's definition of the color BrightRed.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$BrightRed = '#ff0000',
# The scheme's definition of the color BrightGreen.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$BrightGreen = '#00ff00',
# The scheme's definition of the color BrightYellow.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$BrightYellow = '#ffff00',
# The scheme's definition of the color BrightBlue.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$BrightBlue = '#0000ff',
# The scheme's definition of the color BrightPurple.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$BrightPurple = '#ff00ff',
# The scheme's definition of the color BrightCyan.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$BrightCyan = '#00ffff',
# The scheme's definition of the color BrightWhite.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$BrightWhite = '#ffffff',
# The scheme's definition of the color BrightBlack.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$BrightBlack = '#0d0d0d',
# The scheme's foreground color.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$Foreground = '#fefefe',
# The scheme's background color.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidatePattern('^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$')]
[string]
$Background = '#012456'
)
process {
#region Download iTerm2 Theme
$iTermRepo = 'mbadolato/iTerm2-Color-Schemes/master/windowsterminal'
$iTermThemeExists =
Invoke-RestMethod -Uri "https://raw.githubusercontent.com/$iTermRepo/$Name.json" -ErrorAction Ignore
#endregion Download iTerm2 Theme
#region Create Color Scheme Object
$newScheme =
if ($iTermThemeExists)
{
# If the theme exists, let them customize it.
foreach ($k in $PSBoundParameters.Keys) {
# Any supplied parameter
if ($k -ne 'Name' -and # except name
$iTermThemeExists.$k # that exists in the theme
) {
$iTermThemeExists.$k = # will be overwritten
$PSBoundParameters[$k] # with the supplied parameter.
}
}
$iTermThemeExists
}
else
{
# If the theme does not exist,
if ($PSBoundParameters.Count -eq 1)
{
# and no other parameters are supplied, warn them.
Write-Warning "Theme $Name does not exist on https://iterm2colorschemes.com/"
}
$myCmdMetaData = [Management.Automation.CommandMetaData]$MyInvocation.MyCommand
# Create a color scheme object
$newColorScheme = [Ordered]@{}
# by walking over each parameter in this command.
foreach ($p in $myCmdMetaData.Parameters.Keys) {
# lowercase the first letter of the name, because JSON can be picky.
$k = $p.Substring(0,1).ToLower() + $p.Substring(1)
$newColorScheme[$k] = # and assign it to the variable value,
$ExecutionContext.SessionState.PSVariable.Get($p).Value # because this will handle defaults.
}
[PSCustomObject]$newColorScheme
}
#endregion Create Color Scheme Object
#region Update Windows Terminal Profile
$wtProfile = Get-WTProfile -Global # Get our profile
$wtProfile | # Add out color scheme to the list,
Add-Member NoteProperty schemes @(
$newScheme
foreach ($_ in $wtProfile.schemes) {
if ($_.Name -ne $Name) # and don't forget all of the other ones.
{ $_ }
}
) -Force
$wtPath = $wtProfile.Path
if ($PSCmdlet.ShouldProcess("Add ColorScheme $Name to $wtPath")) # If we ShouldProcess
{
$wtProfile.psobject.properties.Remove('Path')
$wtProfile | # turn the profile
ConvertTo-Json -Depth 100 | # back into JSON
Set-Content -Path $wtPath -Encoding UTF8 # and write it to disk.
$wtProfile.psobject.properties.add([PSNoteProperty]::new('Path', $wtPath))
}
elseif ($WhatIfPreference) # if we wanted to know -WhatIf
{
return $wtProfile # return the JSON object
}
#endregion Update Windows Terminal Profile
}
}