-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathwinwal.psm1
197 lines (161 loc) · 6.39 KB
/
winwal.psm1
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
<#
.DESCRIPTION
Updates the desktop wallpaper.
#>
function Set-Wallpaper {
param(
# Path to image to set as background, if not set current wallpaper is used
[Parameter(Mandatory = $true)][string]$Image
)
# Trigger update of wallpaper
# modified from https://www.joseespitia.com/2017/09/15/set-wallpaper-powershell-function/
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
public class PInvoke
{
[DllImport("User32.dll",CharSet=CharSet.Unicode)]
public static extern int SystemParametersInfo(UInt32 action, UInt32 iParam, String sParam, UInt32 winIniFlags);
}
"@
# Setting the wallpaper requires an absolute path, so pass image into resolve-path
[PInvoke]::SystemParametersInfo(0x0014, 0, $($Image | Resolve-Path), 0x0003) -eq 1
}
<#
.DESCRIPTION
Gets the location of the module.
#>
function Get-ScriptDirectory {
Split-Path $script:MyInvocation.MyCommand.Path
}
<#
.DESCRIPTION
Copies the contents of ./templates to ~/.config/wal/templates, will clobber templates with matching names
#>
function Add-WalTemplates {
$sourceDir = "$(Get-ScriptDirectory)/templates"
if (!(Test-Path -Path "$HOME/.config/wal/templates")) {
New-Item -Path "$HOME/.config/wal/templates" -ItemType Directory -ErrorAction SilentlyContinue
}
Get-ChildItem -Path $sourceDir | ForEach-Object {
if (!(Test-Path -Path "$HOME/.config/wal/templates/$($_.Name)")) {
Copy-Item -Path $_.FullName -Destination "$HOME/.config/wal/templates"
}
}
}
function Update-WalCommandPrompt {
$scriptDir = Get-ScriptDirectory
$colorToolDir = "$scriptDir/colortool"
$colorTool = "$colorToolDir/ColorTool.exe"
$schemesDir = "$colorToolDir/schemes/"
# Install color tool if needed
if (!(Test-Path -Path $colorTool)) {
$colorToolZip = "$scriptDir/colortool.zip"
Invoke-WebRequest -Uri "https://github.com/microsoft/terminal/releases/download/1904.29002/ColorTool.zip" -OutFile $colorToolZip
Expand-Archive -Path $colorToolZip -DestinationPath $colorToolDir
Remove-Item -Path $colorToolZip
}
# Make sure it was created
$walprompt = "$HOME/.cache/wal/wal-prompt.ini"
if (Test-Path -Path $walprompt) {
Copy-Item -Path $walprompt -Destination "$schemesDir/wal.ini"
& $colorTool -b wal.ini
}
}
function Update-WalTerminal {
if (!(Test-Path -Path "$HOME/.cache/wal/windows-terminal.json")) {
return
}
@(
# Stable
"$HOME/AppData/Local/Packages/Microsoft.WindowsTerminal_8wekyb3d8bbwe/LocalState",
# Preview
"$HOME/AppData/Local/Packages/Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe/LocalState",
# Stable - Scoop
"$HOME/scoop/persist/windows-terminal/settings",
# Preview - Scoop
"$HOME/scoop/persist/windows-terminal-preview/settings"
) | ForEach-Object {
$terminalDir = "$_"
$terminalProfile = "$terminalDir/settings.json"
# This version of windows terminal isn't installed
if (!(Test-Path -Path $terminalProfile)) {
return
}
Copy-Item -Path $terminalProfile -Destination "$terminalDir/settings.json.bak"
# Load existing profile
$configData = (Get-Content -Path $terminalProfile | ConvertFrom-Json) | Where-Object { $_ -ne $null }
# Create a new list to store schemes
$schemes = New-Object Collections.Generic.List[Object]
$configData.schemes | Where-Object { $_.name -ne "wal" } | ForEach-Object { $schemes.Add($_) }
$walTheme = $(Get-Content "$HOME/.cache/wal/windows-terminal.json" | ConvertFrom-Json)
$schemes.Add($walTheme)
# Update color schemes
$configData.schemes = $schemes
# Set default theme as wal
$configData.profiles.defaults | Add-Member -MemberType NoteProperty -Name colorScheme -Value 'wal' -Force
# Set cursor to foreground color
$configData.profiles.defaults | Add-Member -MemberType NoteProperty -Name cursorColor -Value $walTheme.foreground -Force
# Write config to disk
$configData | ConvertTo-Json -Depth 32 | Set-Content -Path $terminalProfile
}
}
<#
.DESCRIPTION
Updates wal templates and themes using a new image or the existing desktop image
#>
function Update-WalTheme {
param(
# Path to image to set as background, if not set current wallpaper is used
[string]$Image,
[string]$Backend = 'wal'
)
$img = (Get-ItemProperty -Path 'HKCU:/Control Panel/Desktop' -Name Wallpaper).Wallpaper
if ($Image) {
$img = $Image
}
# Add our templates to wal configuration
Add-WalTemplates
$tempImg = "$env:TEMP/$(Split-Path $img -leaf)"
# Use temp location, default backgrounds are in a write protected directory
Copy-Item -Path $img -Destination $tempImg
if (Get-Command 'wal.exe' -ErrorAction SilentlyContinue) {
# Invoke wal and don't set the wallpaper (wal will fail)
$light = $(Get-ItemProperty -Path 'HKCU:/SOFTWARE/Microsoft/Windows/CurrentVersion/Themes/Personalize' -Name AppsUseLightTheme).AppsUseLightTheme
if ($light -gt 0) {
wal -n -e -l -s -t -i $tempImg --backend $Backend
}
else {
wal -n -e -s -t -i $tempImg --backend $Backend
}
}
else {
Write-Error "Pywal not found, please install python and pywal and add it to your PATH`n`twinget install Python.Python.3.11`n`tpip install pywal"
return
}
# Return if wal failed
if ($LastExitCode -ne 0) {
return
}
# Set the wallpaper
if ($Image) {
Set-Wallpaper -Image $Image
}
# Update Windows Terminal
Update-WalTerminal
# Update prompt defaults
Update-WalCommandPrompt
# New oh-my-posh
if ((Get-Command oh-my-posh -ErrorAction SilentlyContinue) -and (Test-Path -Path "$HOME/.cache/wal/posh-wal-agnoster.omp.json")) {
oh-my-posh init pwsh --config "$HOME/.cache/wal/posh-wal-agnoster.omp.json" | Invoke-Expression
}
# Check if pywal fox needs to update
if (Get-Command pywalfox -ErrorAction SilentlyContinue) {
pywalfox update
}
# Terminal Icons
if ((Get-Module -ListAvailable -Name Terminal-Icons) -and (Test-Path -Path "$HOME/.cache/wal/wal-icons.psd1")) {
Add-TerminalIconsColorTheme -Path "$HOME/.cache/wal/wal-icons.psd1"
Set-TerminalIconsTheme -ColorTheme wal
}
}