-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathShow-WT.ps1
282 lines (236 loc) · 10.2 KB
/
Show-WT.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
function Show-WT
{
<#
.Synopsis
Shows Images in the Windows Terminal
.Description
Shows Images in the Windows Terminal.
By default, .GIF files will play once, and non-GIFs will stay for 15 seconds.
.Example
Show-WT -ImagePath .\My.gif # Shows My.gif in the current Windows Terminal profile.
.Example
Show-WT -ImagePath .\My.gif -Wait -1 # Shows My.gif forever
.Link
Get-WTProfile
.Link
Set-WTProfile
#>
[OutputType([Management.Automation.Job], [Nullable])]
param(
# The path to an image file.
[Parameter(ValueFromPipelineByPropertyName,Position=0)]
[ValidatePattern('\.(gif|jpg|jpeg|png)$')]
[Alias('FullName','Image','BackgroundImage')]
[string]
$ImagePath,
# If set, will display content in a given profile.
# If not set, will attempt to auto-detect the profile.
# If the profile cannot be automatically detected, content will be displayed using the default profile settings.
# (this will not override an existing image)
[string]
$ProfileName,
# Sets the alignment of the Image to draw over the window background.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateSet('bottom','bottomLeft','bottomRight','center','left','right','top','topLeft','topRight')]
[Alias('BackgroundImageAlignment', 'ImageAlignment')]
[string]
$Alignment= 'center',
# Sets the opacity of the Image to draw over the window background.
[Parameter(ParameterSetName='ImageFile',ValueFromPipelineByPropertyName)]
[Alias('BackgroundImageOpacity','ImageOpacity')]
[float]
$Opacity = .9,
# Sets how the background image is resized to fill the window.
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateSet('fill','none','uniform','uniformToFill')]
[Alias('BackgroundImageStretchMode', 'ImageStretchMode')]
[string]
$StretchMode= 'uniformToFill',
# How long to wait before making the change.
# By default, the change will be as quick as possible.
[Parameter(ValueFromPipelineByPropertyName)]
[timespan]
$Wait = 0,
# Sets how long the image should be displayed.
# If the duration is negative, the image will not be automatically cleared.
[Parameter(ValueFromPipelineByPropertyName)]
[Timespan]
$Duration,
# Sets the number of times an animated .gif should be looped.
[Parameter(ValueFromPipelineByPropertyName)]
[int]
$LoopCount = 1,
<#
When useAcrylic is set to true, it sets the transparency of the window for the profile.
Accepts floating point values from 0-1 (default 0.5).
#>
[Parameter(ValueFromPipelineByPropertyName)]
[ValidateRange(0,1)]
[float]
$AcrylicOpacity,
<#
When set to true, the window will have an acrylic background.
When set to false, the window will have a plain, untextured background.
#>
[Parameter(ValueFromPipelineByPropertyName)]
[switch]
$UseAcrylic,
<#
If provided, will use a pixel shader.
#>
[Parameter(ValueFromPipelineByPropertyName)]
[string]
$PixelShader,
# If set, will run in a background job.
[switch]
$AsJob
)
begin {
#region Prepare Background Job
if ($AsJob) {
$jobCmd = $ExecutionContext.SessionState.InvokeCommand.GetCommand('Start-ThreadJob','Alias,Cmdlet,Function')
if (-not $jobCmd) {
$jobCmd = $ExecutionContext.SessionState.InvokeCommand.GetCommand('Start-Job','Alias,Cmdlet,Function')
}
$jobDef = [ScriptBlock]::Create(@"
param([Collections.IDictionary]`$parameters)
Import-Module '$($MyInvocation.MyCommand.Module.Path | Split-Path)'
$($MyInvocation.MyCommand.Name) @parameters
"@)
}
#endregion Prepare Background Job
$getGifLength = {
param([string]$resolvedPath)
if (-not ('Drawing.Image' -as [type])) {
Add-Type -AssemblyName System.Drawing
}
$img = [Drawing.Image]::FromFile($resolvedPath)
$frameCount =
try {
$img.GetFrameCount([Drawing.Imaging.FrameDimension]::Time)
} catch {0}
$frameTimes = try { $img.GetPropertyItem(20736).Value } catch { 0 }
if ($frameTimes) {
$totalMS = 0
for ($i=0; $i -lt $frameCount; $i++) {
$totalMS+=[BitConverter]::ToInt32($frameTimes,$i * 4) * 10
}
[Timespan]::FromMilliseconds($totalMS)
}
$img.Dispose()
}
$accumulateArgs = [Collections.Generic.List[Collections.IDictionary]]::new()
}
process {
$accumulateArgs.Add((@{} + $PSBoundParameters))
}
end {
foreach ($acc in $accumulateArgs) {
foreach ($kv in $acc.GetEnumerator()) {
$ExecutionContext.SessionState.PSVariable.Set($kv.Key, $kv.Value)
}
if (-not $targetProfile) {
$targetProfile =
if (-not $ProfileName) {
Get-WTProfile -Current
} else {
Get-WTProfile -ProfileName $ProfileName
}
}
if (-not $targetProfile) {
$targetProfile = Get-WTProfile -Default
}
if (-not $targetProfile) {
Write-Error "No target profile - WT_PROFILE_ID '$env:WT_PROFILE_ID'"
return
}
if ($wait.TotalMilliseconds) {
Start-Sleep -Milliseconds $wait.TotalMilliseconds
}
$targetProfileJson = $targetProfile | ConvertTo-Json -Depth 10
$targetProfileBackup = $targetProfileJson | ConvertFrom-Json
$myParameters = @{} + $PSBoundParameters
if ($ImagePath) {
$imageFileUri = $imagePath -as [uri]
if ($imageFileUri.Authority) {
$imageDest =
if ($PSVersionTable.OS -and $PSVersionTable.OS -notlike '*windows*') {
Join-Path "/home/$($env:USER)/Pictures" $imageFileUri.Segments[-1]
} else {
Join-Path "$home\Pictures" $imageFileUri.Segments[-1]
}
$newFile = New-Item -ItemType File -Path $imageDest -Force
[Net.Webclient]::new().DownloadFile($imageFileUri, $newFile.FullName)
$imagePath = $newFile.FullName
}
$resolvedPath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($ImagePath)
if (-not $resolvedPath) { return }
$resolvedPath = Get-Item -LiteralPath $resolvedPath | Select-Object -ExpandProperty Fullname
$myParameters['ImagePath'] = "$resolvedPath"
}
if (-not $ProfileName -and $ENV:WT_PROFILE_ID) {
$ProfileName = $myParameters['ProfileName'] = $ENV:WT_PROFILE_ID
}
if ($AsJob) {
$myParameters.Remove('AsJob')
& $jobCmd -ScriptBlock $jobDef -ArgumentList $MyParameters
return
}
$wasUsingAcrylic = ($targetProfile.useAcrylic -as [bool])
$oldAcrylicOpacity = ($targetProfile.acrylicOpacity -as [float])
$realPath =
if ($resolvedPath -like '/mnt/*') { # If we're trying to show an image with a mounted path
$resolvedPath -replace '/mnt/(?<Letter>[a-z])', '${Letter}:\' -replace '/', '\'
} elseif ($PSVersionTable.OS -and
$PSVersionTable.OS -notlike '*Windows*' -and $env:WSL_DISTRO_NAME) {
"\\wsl$\$($env:WSL_DISTRO_NAME)\" + ($resolvedPath -replace '/','\')
} else {
$resolvedPath
}
$updatedProfile = $targetProfile
if ($ImagePath) {
$updatedProfile|
Add-Member backgroundImage "$realPath" -Force -PassThru |
Add-Member backgroundImageOpacity $Opacity -Force -PassThru |
Add-Member backgroundImageAlignment $Alignment -Force -PassThru |
Add-Member backgroundImageStrechMode $StretchMode -Force
}
if ($PixelShader) {
$resolvedPixelShaderPath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath("$PixelShader")
$updatedProfile |
Add-Member "experimental.pixelShaderPath" "$resolvedPixelShaderPath" -Force
} elseif ($PSBoundParameters.ContainsKey('PixelShader')) {
$updatedProfile |
Add-Member "experimental.pixelShaderPath" "" -Force
}
if ($UseAcrylic.IsPresent) {
$updatedProfile = $targetProfile |
Add-Member useAcrylic ([bool]$UseAcrylic) -Force -PassThru
}
if ($AcrylicOpacity) {
$updatedProfile = $updatedProfile |
Add-Member acrylicOpacity $AcrylicOpacity -Force -PassThru
}
if ($targetProfile.guid) {
Set-WTProfile -ProfileName $targetProfile.guid -Confirm:$false -InputObject $updatedProfile
} else {
Set-WTProfile -Default -Confirm:$false -InputObject $updatedProfile
}
if (-not $PSBoundParameters['Wait'] -and $ImagePath -like '*.gif') {
$Duration = try {
& $getGifLength $resolvedPath
} catch {
[Timespan]::FromSeconds(2.5 * (Get-Item -LiteralPath $resolvedPath).Length /1mb)
}
}
if ($LoopCount -ne 1 -and $ImagePath) {
$Duration = [Timespan]::FromMilliseconds((& $getGifLength $resolvedPath).TotalMilliseconds * $LoopCount)
}
if ($Duration.TotalMilliseconds -ge 0) {
Start-Sleep -milliseconds $Duration.TotalMilliseconds
$targetProfileBackup |
Set-WTProfile -ProfileName $targetProfile.guid -Confirm:$false -Overwrite
}
}
}
}