-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPSReminderLite.psm1
262 lines (226 loc) · 8.89 KB
/
PSReminderLite.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#region Main
# used for culture debugging
# write-host "Importing with culture $(Get-Culture)"
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 PSReminderLite.psd1 -BaseDirectory $PSScriptRoot\en-us
}
#dot-source module functions
Get-ChildItem -Path $PSScriptRoot\functions\*.ps1 |
ForEach-Object { . $_.FullName }
#endregion
#region Define module variables
$ExportPath = Join-Path -Path $HOME -ChildPath '.psreminder.json'
If (Test-Path -Path $ExportPath) {
#use the preference file
Get-Content -Path $ExportPath | ConvertFrom-Json |
ForEach-Object {
if ($_.Value.GetType().Name -ne 'PSCustomObject') {
Set-Variable -Name $_.Name -Value $_.Value -Force
}
elseif ($_.name -eq 'PSReminderTag') {
$Hash = @{}
$global:t = $_
$_.Value.PSObject.Properties | ForEach-Object {
$Hash[$_.Name] = $_.Value
}
Set-Variable -Name PSReminderTag -Value $Hash -Force
}
}
<#
Get-Content -Path $ExportPath | ConvertFrom-Json |
ForEach-Object {
Set-Variable -Name $_.Name -Value $_.Value -Force
}
#>
}
else {
#the default number of days to display for Show-TickleEvents
$PSReminderDefaultDays = 7
#database defaults
$PSReminderDB = Join-Path -Path $HOME -ChildPath PSReminder.db
$PSReminderTable = 'EventData'
$PSReminderArchiveTable = 'ArchivedEvent'
#define a default tag list and style settings
$PSReminderTag = @{
'Work' = "`e[38;5;192m"
'Personal' = "`e[96m"
'Priority' = "`e[1;3;38;5;199m"
}
}
If (-Not (Test-Path -Path $PSReminderDB)) {
Write-Warning "The database file $PSReminderDB does not exist or could not be found. Please run Initialize-PSReminderDatabase to create the database."
}
#endregion
#region Class definitions
Class PSReminder {
[String]$Event
[DateTime]$Date
[String]$Comment
[int32]$ID
[string[]]$Tags
[boolean]$Expired = $False
#add a hidden property that captures the database path
hidden [string]$Source
#add a hidden property to capture the computer name'
hidden [string]$ComputerName = [System.Environment]::MachineName
#constructor
PSReminder([int32]$ID, [String]$Event, [DateTime]$Date, [String]$Comment, [String[]]$Tags) {
$this.ID = $ID
$this.Event = $Event
$this.Date = $Date
$this.Comment = $Comment
$this.Tags = $Tags
if ($Date -lt (Get-Date)) {
$this.Expired = $True
}
}
} #close PSReminder class
Class ArchivePSReminder {
[String]$Event
[DateTime]$Date
[String]$Comment
[int32]$ID
[string[]]$Tags
[DateTime]$ArchivedDate
#add a hidden property that captures the database path
hidden [string]$Source
#add a hidden property to capture the computer name'
hidden [string]$ComputerName = [System.Environment]::MachineName
ArchivePSReminder([int32]$ID, [String]$Event, [DateTime]$Date, [String]$Comment, [String[]]$Tags, [DateTime]$ArchivedDate) {
$this.ID = $ID
$this.Event = $Event
$this.Date = $Date
$this.Comment = $Comment
$this.ArchivedDate = $ArchivedDate
$this.Tags = $Tags
}
} #close ArchivePSReminder class
Class PSReminderDBInfo {
[string]$Name
[string]$Path
[int32]$PageSize
[int32]$PageCount
[int32]$Reminders
[int32]$Expired
[int32]$Archived
[int32]$Size
[DateTime]$Created
[DateTime]$Modified
[string]$Author
[string]$Comment
[string]$Encoding
[version]$SQLiteVersion
[DateTime]$Date = (Get-Date)
[string]$Computername = [System.Environment]::MachineName
#methods
hidden [void]GetDBInfo () {
Try {
_verbose ($script:strings.GetDBInfo -f $this.Path)
$r = Get-MySQLiteDB -Path $this.Path -ErrorAction Stop
$this.Name = Split-Path -Path $r.path -Leaf
$this.PageSize = $r.PageSize
$this.PageCount = $r.PageCount
$this.Size = $r.Size
$this.Created = $r.Created
$this.Modified = $r.Modified
$this.Encoding = $r.Encoding
$this.SQLiteVersion = $r.SQLiteVersion
}
Catch {
Throw $_
}
}
[PSReminderDBInfo]RefreshInfo () {
_verbose $script:strings.OpenDB
$this.GetDBInfo()
$conn = Open-MySQLiteDB -Path $this.Path
$InvokeParams = @{
Connection = $conn
KeepAlive = $True
Query = ''
}
$InvokeParams.Query = 'select Author,comment from metadata'
_verbose ($script:strings.InvokeQuery -f $InvokeParams.Query)
$meta = Invoke-MySQLiteQuery @InvokeParams
$this.Author = $meta.Author
$this.Comment = $meta.Comment
$now = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
$InvokeParams.Query = "Select count(*) AS Count from $global:PSReminderTable Where EventDate>= '$now'"
_verbose ($script:strings.InvokeQuery -f $InvokeParams.Query)
$this.Reminders = $(Invoke-MySQLiteQuery @InvokeParams).count
_verbose ($script:strings.InvokeQuery -f $InvokeParams.Query)
$InvokeParams.Query = "Select count(*) AS Count from $global:PSReminderArchiveTable"
$this.Archived = (Invoke-MySQLiteQuery @InvokeParams).Count
_verbose ($script:strings.InvokeQuery -f $InvokeParams.Query)
$InvokeParams.Query = "Select count(*) AS Count from $global:PSReminderTable Where EventDate< '$((Get-Date).ToString('yyyy-MM-dd HH:mm:ss'))'"
$this.Expired = (Invoke-MySQLiteQuery @InvokeParams).count
If ($conn.state -eq 'Open') {
_verbose $script:strings.CloseDB
$conn.Close()
}
return $this
}
#Constructor
PSReminderDBInfo([string]$DatabasePath) {
$this.Path = $DatabasePath
$this.RefreshInfo()
}
} #close PSReminderDBInfo class
Class PSReminderPreference {
[string]$PSReminderDB = $global:PSReminderDB
[string]$PSReminderDefaultDays = $global:PSReminderDefaultDays
[string]$PSReminderTable = $global:PSReminderTable
[string]$PSReminderArchiveTable = $global:PSReminderArchiveTable
[hashtable]$PSReminderTag = $global:PSReminderTag
[object]ShowTags () {
$r = $this.PSReminderTag.GetEnumerator() | Foreach-Object {
$stringValue = $_.Value.Replace("$([char]27)", '`e')
[PSCustomObject]@{
PSTypeName = 'PSReminderTag'
Tag = $_.Key
Style = '{0}{1}{2}' -f $($_.Value), $stringValue, $("`e[0m")
}
}
return $r
}
} #close PSReminderPreference class
#custom type extensions. This might be exported to a ps1xml file in a future release
Update-TypeData -TypeName PSReminder -DefaultDisplayPropertySet ID, Date, Event, Comment -Force
Update-TypeData -TypeName PSReminder -MemberType AliasProperty -MemberName Name -Value Event -Force
Update-TypeData -TypeName PSReminder -MemberType ScriptProperty -MemberName Countdown -Value {
$ts = $this.Date - (Get-Date)
if ($ts.TotalMinutes -lt 0) {
$ts = New-TimeSpan -Minutes 0
}
$ts
} -Force
Update-TypeData -TypeName PSReminderDBInfo -MemberType ScriptProperty -MemberName Age -Value {
$ts = New-TimeSpan -Start $this.Modified -End (Get-Date)
$ts
} -Force
#endregion
#region auto completers
Register-ArgumentCompleter -CommandName Add-PSReminder, Set-PSReminder -ParameterName Tags -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
(Get-PSReminderTag).Where({ $_.Tag -like "$WordToComplete*" }).ForEach({ [System.Management.Automation.CompletionResult]::new($_.Tag.Trim(), $_.Tag.Trim(), 'ParameterValue', $_.Tag) })
}
Register-ArgumentCompleter -CommandName Get-PSReminder -ParameterName Tag -ScriptBlock {
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
(Get-PSReminderTag).Where({ $_.Tag -like "$WordToComplete*" }).ForEach({ [System.Management.Automation.CompletionResult]::new($_.Tag.Trim(), $_.Tag.Trim(), 'ParameterValue', $_.Tag) })
}
#endregion
$export = @{
Variable = @('PSReminderDefaultDays', 'PSReminderDB', 'PSReminderTable',
'PSReminderArchiveTable', 'PSReminderTag')
Function = @('Export-PSReminderPreference', 'Initialize-PSReminderDatabase',
'Add-PSReminder', 'Get-PSReminder', 'Get-PSReminderDBInformation', 'Set-PSReminder',
'Remove-PSReminder', 'Export-PSReminderDatabase', 'Import-PSReminderDatabase',
'Move-PSReminder', 'Get-AboutPSReminder', 'Get-PSReminderTag',
'Import-FromTickleDatabase','Get-PSReminderPreference')
Alias = @('apsr', 'gpsr', 'spsr', 'rpsr', 'Archive-PSReminder', 'gprt','New-PSReminder')
}
Export-ModuleMember @export