Skip to content

Instantly share code, notes, and snippets.

@liuwenzhuang
Last active January 11, 2020 12:19
Show Gist options
  • Save liuwenzhuang/19fc89f1d1b1bd1b44a713358c850465 to your computer and use it in GitHub Desktop.
Save liuwenzhuang/19fc89f1d1b1bd1b44a713358c850465 to your computer and use it in GitHub Desktop.
为wsl添加主题.(add schemes from iTerm2-Color-Schemes to wsl profiles). Code from https://andrewpla.dev/Adding-New-Color-Schemes-To-Windows-Terminal/
# Path to the profile when installed from the Windows Store.
$profilePath = "C:\Users\$Env:Username\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\profiles.json"
# remove comments
$profile = (Get-Content $ProfilePath) -replace '(?m)(?<=^([^"]|"[^"]*")*)//.*' -replace '(?ms)/\*.*?\*/' | Out-String | ConvertFrom-Json
$backupProfilePath = "$home\Documents\WindowsTerminalprofiles.json"
Write-Verbose "Backing up profile to $backupProfilePath"
$profile | ConvertTo-Json | Set-Content $backupProfilePath
# Grab all schemes from github
Function Get-WtScheme {
<#
.Description
Returns color schemes from
https://github.com/mbadolato/iTerm2-Color-Schemes/blob/master/windowsterminal
.Parameter Url
Url to the iTerm2 project.
.Parameter Theme
Specify the name of the theme that you want returned. All themes are returned by default
.Example
PS> Get-WtTheme
Returns all available themes
.Example
PS> Get-WtTheme -Filter 'atom.json'
Retrieves the atom.json theme.
.Link
https://github.com/mbadolato/iTerm2-Color-Schemes/blob/master/windowsterminal/
.Link link to blogpost
#>
[cmdletbinding()]
param(
[string]
$Theme = '*',
[string]
$Url = 'https://github.com/mbadolato/iTerm2-Color-Schemes/tree/master/windowsterminal'
)
$page = Invoke-WebRequest $Url -UseBasicParsing
$links = $page.Links | Where-Object title -like "$Theme.json"
Write-Verbose "$($links.count) links found matching $Theme"
foreach ($link in $links) {
# Use the raw url so raw results can be returned and output
$base = 'https://raw.githubusercontent.com'
$href = $link.href
$rawUrl = $base + $href
$rawUrl = $rawUrl.replace('/blob', '')
Invoke-RestMethod $RawUrl
}
}
$schemes = Get-WtScheme
Write-Verbose "We have found $($schemes.count) schemes. Great Success!!"
# This object will contain schemes from our profile and all of the schemes that we just got.
$combinedProperties = [pscustomobject]@()
# loop through the original scheme and export the properties
foreach ($scheme in ($profile.schemes)) {
# Avoid adding duplicate schemes.
if (-not ($combinedProperties.name -like $scheme.name)) {
$combinedProperties += $scheme
}
}
# Add new schemes
foreach ($scheme in $schemes) {
if (-not ($combinedProperties.name -like $scheme.name)) {
$combinedProperties += $scheme
}
}
# Remove the count property from appearing in our json output.
# This only persists for the session
# See https://stackoverflow.com/questions/20848507/why-does-powershell-give-different-result-in-one-liner-than-two-liner-when-conve
Remove-TypeData System.Array -ErrorAction SilentlyContinue
$updatedSchemeObj = [pscustomobject]($combinedProperties)
$profile.schemes = $updatedSchemeObj
Write-Verbose "Updating profile.json with new schemes"
$profile |
ConvertTo-Json -Depth 8 |
Set-Content $profilePath
@liuwenzhuang
Copy link
Author

ConvertFrom-Json can not handle comments in JSON content, so remove comments before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment