Skip to content

Instantly share code, notes, and snippets.

@Kieranties Kieranties/gifterm.ps1
Last active Jun 26, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.