Skip to content

Instantly share code, notes, and snippets.

@Kieranties
Last active June 26, 2019 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kieranties/e78f8cc26f87c3dec3ab155816bce372 to your computer and use it in GitHub Desktop.
Save Kieranties/e78f8cc26f87c3dec3ab155816bce372 to your computer and use it in GitHub Desktop.
Hacky script to set gifs as the background to the new Windows Terminal => iwr http://bit.ly/gifterm | iex
<#
iwr http://bit.ly/gifterm | iex
$env:gifq='cats and dogs'; iwr http://bit.ly/gifterm | iex
#>
using namespace System.IO
param(
[string]$Query = ($env:gifq),
[string]$QueryRoot = 'https://giphy.com/search'
)
Clear-Host
$script:ErrorActionPreference = 'Ignore'
$script:ProgressPreference = 'SilentlyContinue'
try {
if(!$Query) { $Query = 'cats' }
else { $Query = $Query.Replace(' ', '-') }
function GetGifs {
$result = Invoke-WebRequest "$Queryroot/$query" -UseBasicParsing
$gifsLine = $result.Content -split '\n' | Select-String -Pattern 'gifs:' | Select-Object -ExpandProperty Line
($gifsLine.Trim().Substring(5).Trim().Trim(',') | ConvertFrom-Json).embed_url
}
# Check there is a Windows Terminal install
$profilesJson = [Path]::Combine($env:LOCALAPPDATA, 'Packages', 'Microsoft.WindowsTerminal_8wekyb3d8bbwe', 'RoamingState', 'profiles.json')
if(Test-Path $profilesJson){
$json = Get-Content $profilesJson -Raw | ConvertFrom-Json
$gifs = GetGifs
$gifsDir = [Directory]::GetParent($profilesJson)
$json.profiles | ForEach-Object {
$gif = $gifs[(Get-Random -Minimum 0 -Maximum $gifs.Length)]
$gifId = $gif.Substring($gif.LastIndexOf('/')+1)
$gifPath = Join-Path $gifsDir "$gifId.gif"
if(!(Test-Path $gifPath)){
Invoke-WebRequest "https://i.giphy.com/media/$gifId/giphy.gif" -OutFile $gifPath
}
if($_.psobject.properties['backgroundImage']){
$_.backgroundImage = $gifPath
} else {
$_ | Add-Member -NotePropertyName 'backgroundImage' -NotePropertyValue $gifPath
}
# useAcrylic must be set to false enable the background image
if($_.psobject.properties['useAcrylic']){
$_.useAcrylic = $false
} else {
$_ | Add-Member -NotePropertyName 'useAcrylic' -NotePropertyValue $false
}
}
$json | ConvertTo-Json -Depth 100 | Set-Content $profilesJson
}
} finally {
Clear-History
Get-Process -PID $pid | Stop-Process
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment