Skip to content

Instantly share code, notes, and snippets.

@semick-dev
Last active February 11, 2025 08:09
Show Gist options
  • Save semick-dev/6859b9f398b46b5309fcedb9c3df2d41 to your computer and use it in GitHub Desktop.
Save semick-dev/6859b9f398b46b5309fcedb9c3df2d41 to your computer and use it in GitHub Desktop.
Set-Title, Set-Theme, Set-Random-Theme -- Terminal Extensions

Terminal Extensions

This powershell module is used to set your terminal theme to one downloaded from windowsterminalthemes.dev.

/> Import-Module -DisableNameChecking -FullyQualifiedName <path-to-Terminal-Extensions.psm1>
/> Set-Theme "Chalk"
/> Set-Title "docker logging"

The module uses your current terminal tab's title to determine WHICH terminal profile needs to be updated with your newly targeted theme. You cannot Set-Theme on any terminal tab that that has been Set-Title-ed.

<#
.SYNOPSIS
Sets the current windows terminal tab title.
.PARAMETER Title
The string that the host UI tab title will be set to. Users do not need
to surround this in quotes if they have multi-word titles (though they are welcome to),
the function handles combination if it is necessary.
.COMPONENT
locker
#>
Function Set-Title {
param(
[string[]]
[Parameter(Position=0, ValueFromRemainingArguments)]
$Title
)
if ($Title -is [array]){
$Title = ($Title -join " ")
}
$Host.UI.RawUI.WindowTitle = $Title
}
Export-ModuleMember -Function Set-Title
Set-Alias -Name st -Value Set-Title
Export-ModuleMember -Alias st
Function Get-Settings-JSON-Location()
{
$locations = @(
"$($env:USERPROFILE)/AppData/Local/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState/settings.json"
"$($env:USERPROFILE)/AppData/Local/Packages/Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe/LocalState/settings.json"
)
foreach($possibleSettingsLocation in $locations){
if(Test-Path $possibleSettingsLocation){
return $possibleSettingsLocation
}
}
}
Function Edit-Settings-JSON()
{
$settings = Get-Settings-JSON-Location
code $settings
}
Export-ModuleMember -Function Edit-Settings-JSON
Function Get-Settings-JSON()
{
$settings = Get-Settings-JSON-Location
return (Get-Content $settings | ConvertFrom-Json)
}
Export-ModuleMember -Function Get-Settings-JSON
Function Update-Settings-JSON([Parameter(mandatory=$true)] [PSCustomObject] $SettingsJSON)
{
$settingsLocation = Get-Settings-JSON-Location
$today = Get-Date -Format "MM.dd.yyyyHH.mm"
$originalContent = Get-Content $settingsLocation
$cacheFolder = "$($env:USERPROFILE)/.termextensions"
$cacheLocation = "$cacheFolder/backup-$today.json"
# write backup
if(-not (Test-Path $cacheFolder)){
mkdir -p $cacheFolder
}
# write new settings json
Set-Content -Path $cacheLocation -Value $originalContent
$SettingsJSON | ConvertTo-Json -Depth 100 | Out-File $settingsLocation
}
<#
.SYNOPSIS
Returns a json objects instance of the theme index from https://windowsterminalthemes.dev/.
.DESCRIPTION
1. Checks for local cache (expiring after 7 days) of the themeIndex.json.
2. Downloads the themeIndex if necessary to a local file in the home folder.
3. Access the file, return json.
.COMPONENT
locker
#>
Function Get-Theme-Index(){
$cacheLocation = "$($env:USERPROFILE)/.wt_theme_index.json"
$downloadNecessary = $true
$themeIndex = @()
$today = Get-Date
if(Test-Path $cacheLocation){
$lastWriteTime = (Get-ChildItem $cacheLocation).LastWriteTime
if (($today - $lastWriteTime).Days -lt 7){
$downloadNecessary = $false
$themeIndex = Get-Content $cacheLocation | ConvertFrom-Json
}
}
if ($downloadNecessary) {
$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession
$session.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
$themeIndexResponse = Invoke-WebRequest -UseBasicParsing -Uri "https://2zrysvpla9.execute-api.eu-west-2.amazonaws.com/prod/themes" `
-WebSession $session `
-Headers @{
"Pragma"="no-cache"
"Cache-Control"="no-cache"
"sec-ch-ua"="`" Not A;Brand`";v=`"99`", `"Chromium`";v=`"99`", `"Google Chrome`";v=`"99`""
"sec-ch-ua-mobile"="?0"
"sec-ch-ua-platform"="`"Windows`""
"Accept"="*/*"
"Origin"="https://windowsterminalthemes.dev"
"Sec-Fetch-Site"="cross-site"
"Sec-Fetch-Mode"="cors"
"Sec-Fetch-Dest"="empty"
"Referer"="https://windowsterminalthemes.dev/"
"Accept-Encoding"="gzip, deflate, br"
"Accept-Language"="en-US,en;q=0.9"
}
# write content to file
Set-Content -Path $cacheLocation -Value $themeIndexResponse.Content
$themeIndex = $themeIndexResponse.Content | ConvertFrom-Json
}
return $themeIndex
}
Export-ModuleMember -Function Get-Theme-Index
<#
.SYNOPSIS
Activate a terminal theme.
.DESCRIPTION
If not present in your terminal settings.json schemes, it will be added for you! Uses your tab title to find your current running theme.
Don't attempt to use this theme installation on a custom titled terminal window.
.PARAMETER ThemeId
The chosen id! This value will be the name of the theme on windowsterminalthemes.dev.
.COMPONENT
locker
#>
Function Set-Theme([Parameter(mandatory=$false)] [string] $ThemeId)
{
$settings = Get-Settings-JSON
$themes = Get-Theme-Index
$targetProfile = ($themes | Where-Object { $_.name -eq $ThemeId} | Select-Object -First 1)
$possibleCurrentProfileName = $Host.UI.RawUI.WindowTitle
$found = $false
foreach($item in $settings.schemes){
if($item.name -eq $targetProfile.name){
$found = $true
}
}
if(-not $found){
$settings.schemes += $targetProfile
}
foreach($item in $settings.profiles.list){
if($item.name -eq $possibleCurrentProfileName){
$item.colorScheme = $targetProfile.name
}
}
Update-Settings-JSON -SettingsJSON $settings
}
Export-ModuleMember -Function Set-Theme
<#
.SYNOPSIS
Install a terminal theme from a random selection in the index.
.DESCRIPTION
Uses your tab title to find your current running theme. Don't attempt to use this theme installation on a custom titled terminal window.
.COMPONENT
locker
#>
Function Set-Random-Theme()
{
$themes = Get-Theme-Index
$randomProfile = ($themes[(Get-Random -Minimum 0 -Maximum ($themes.length - 1))]).name
Set-Theme -ThemeId $randomProfile
}
Export-ModuleMember -Function Set-Random-Theme
<#
.SYNOPSIS
Gets the name of the currently set theme.
.DESCRIPTION
Uses your tab title to find your current running theme. Don't attempt to use this from within a custom titled terminal window.
.COMPONENT
locker
#>
Function Get-Current-Theme()
{
$settings = Get-Settings-JSON
$possibleCurrentProfileName = $Host.UI.RawUI.WindowTitle
foreach($item in $settings.profiles.list){
if($item.name -eq $possibleCurrentProfileName){
Write-Host $item.colorScheme
}
}
}
Export-ModuleMember -Function Get-Current-Theme
<#
.SYNOPSIS
.DESCRIPTION
.COMPONENT
locker
#>
Function Initialize-Background-Stash()
{
$imageIndex = "https://steamcommunity.com/profiles/m3l7down/screenshots"
$ffUsrAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1"
$indexHtml = Invoke-RestMethod -Method Get -Uri $imageIndex -Headers @{ "User-Agent" = $ffUsrAgent }
}
Export-ModuleMember -Function Initialize-Background-Stash
<#
.SYNOPSIS
Set a random background to the current terminal theme.
.DESCRIPTION
.COMPONENT
locker
#>
Function Set-RandomBackground()
{
}
Export-ModuleMember -Function Set-RandomBackground
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment