Repository navigation
Expand file tree
/
Copy pathPSClock.psm1
More file actions
99 lines (88 loc) · 3.67 KB
/
Copy pathPSClock.psm1
File metadata and controls
99 lines (88 loc) · 3.67 KB
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
if ((Get-Culture).Name -match '\w+') {
Import-LocalizedData -BindingVariable strings
}
else {
#force using En-US if no culture found, which might happen on non-Windows systems.
Import-LocalizedData -BindingVariable strings -FileName PSClock.psd1 -BaseDirectory $PSScriptRoot\en-us
}
#Turn on additional verbose messaging when importing the module
if ($MyInvocation.line -match '-verbose') {
$VerbosePreference = 'Continue'
$manifest = Import-PowerShellDataFile $PSScriptRoot\PSClock.psd1
Write-Verbose ($strings.Importing -f $manifest.ModuleVersion)
}
Write-Verbose $strings.DotSource
Get-ChildItem -Path $PSScriptRoot\functions\*.ps1 |
ForEach-Object {
Write-Verbose $_.FullName
. $_.FullName
}
#define module-scoped variables to the path for Save-PSClock
$SavePath = Join-Path -Path $home -ChildPath PSClockSettings.xml
$FlagPath = Join-Path -Path $env:temp -ChildPath psclock-flag.txt
Write-Verbose ($strings.UsingPath -f $SavePath)
Write-Verbose ($strings.UsingFlag -f $FlagPath)
Write-Verbose $strings.RegisterFontCompleter
Register-ArgumentCompleter -CommandName 'Start-PSClock', 'Set-PSClock' -ParameterName 'FontFamily' -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
[System.Drawing.Text.InstalledFontCollection]::new().Families.Name |
Where-Object { $_ -like "$($WordToComplete)*" } |
ForEach-Object {
# completion text,listItem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new("'$($_)'", $_, 'ParameterValue', $_)
}
}
Write-Verbose $strings.RegisterColorCompleter
Register-ArgumentCompleter -CommandName 'Start-PSClock', 'Set-PSClock' -ParameterName 'Color' -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
#skipping Transparent as a font color
[System.Drawing.Brushes].GetProperties().name |
Select-Object -Skip 1 |
Where-Object { $_ -like "$($WordToComplete)*" } |
ForEach-Object {
#show the color name using the color
$ansi = Get-RGB $_ | Convert-RGBtoAnsi
[String]$show = "$ansi$($_)$([char]27)[0m"
# completion text,listItem text,result type,Tooltip
[System.Management.Automation.CompletionResult]::new("'$($_)'", $show, 'ParameterValue', $_)
}
}
#register on module exit event to clean up
#this will only be run when using Remove-Module
$OnRemoveScript = {
#stop clocks if they are running
if ($global:consoleClockSettings) {
Stop-ConsoleClock
}
If ($global:titleClockSettings) {
Stop-TitleClock
}
If ($global:psClockSettings.Running) {
Stop-PSClock
}
#the following code is "just-in-case"
try {
Unregister-Event -SubscriptionId $script:titleClockEvent.SubscriptionId -Force -ErrorAction Stop
}
catch {
#ignore any errors
}
try {
Unregister-Event -SubscriptionId $script:clockEvent.SubscriptionId -Force -ErrorAction Stop
}
catch {
#ignore any errors
}
Remove-Variable -Name clockEvent -Scope Script -ErrorAction Ignore
Remove-Variable -Name titleClockEvent -Scope Script -ErrorAction Ignore
Remove-Variable -Name consoleClockSettings -Scope Global -ErrorAction Ignore
Remove-Variable -Name titleClockSettings -Scope Global -ErrorAction Ignore
Remove-Variable -Name PSClockSettings -Scope Global -ErrorAction Ignore
}
$ExecutionContext.SessionState.Module.OnRemove += $OnRemoveScript
#get module version for verbose messaging
$modVersion = (Test-ModuleManifest -Path $PSScriptRoot\PSClock.psd1).Version
#reset verbose preference
if ($VerbosePreference -eq 'Continue') {
$VerbosePreference = 'SilentlyContinue'
}