forked from setiawanjemy88/jemy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmatrix.psm1
395 lines (289 loc) · 10.4 KB
/
cmatrix.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
Set-StrictMode -off
#
# Module: PowerShell Console ScreenSaver Version 0.1
# Author: Oisin Grehan ( http://www.nivot.org )
#
# A PowerShell CMatrix-style screen saver for true-console hosts.
#
# This will not work in Micrisoft's ISE, Quest's PowerGUI or other graphical hosts.
# It should work fine in PowerShell+ from Idera which is a true console.
#
if ($host.ui.rawui.windowsize -eq $null) {
write-warning "Sorry, I only work in a true console host like powershell.exe."
throw
}
#
# Console Utility Functions
#
function New-Size {
param([int]$width, [int]$height)
new-object System.Management.Automation.Host.Size $width,$height
}
function New-Rectangle {
param(
[int]$left,
[int]$top,
[int]$right,
[int]$bottom
)
$rect = new-object System.Management.Automation.Host.Rectangle
$rect.left= $left
$rect.top = $top
$rect.right =$right
$rect.bottom = $bottom
$rect
}
function New-Coordinate {
param([int]$x, [int]$y)
new-object System.Management.Automation.Host.Coordinates $x, $y
}
function Get-BufferCell {
param([int]$x, [int]$y)
$rect = new-rectangle $x $y $x $y
[System.Management.Automation.Host.buffercell[,]]$cells = $host.ui.RawUI.GetBufferContents($rect)
$cells[0,0]
}
function Set-BufferCell {
[outputtype([System.Management.Automation.Host.buffercell])]
param(
[int]$x,
[int]$y,
[System.Management.Automation.Host.buffercell]$cell
)
$rect = new-rectangle $x $y $x $y
# return previous
get-buffercell $x $y
# use "fill" overload with single cell rect
$host.ui.rawui.SetBufferContents($rect, $cell)
}
function New-BufferCell {
param(
[string]$Character,
[consolecolor]$ForeGroundColor = $(get-buffercell 0 0).foregroundcolor,
[consolecolor]$BackGroundColor = $(get-buffercell 0 0).backgroundcolor,
[System.Management.Automation.Host.BufferCellType]$BufferCellType = "Complete"
)
$cell = new-object System.Management.Automation.Host.BufferCell
$cell.Character = $Character
$cell.ForegroundColor = $foregroundcolor
$cell.BackgroundColor = $backgroundcolor
$cell.BufferCellType = $buffercelltype
$cell
}
function log {
param($message)
[diagnostics.debug]::WriteLine($message, "PS ScreenSaver")
}
#
# Main entry point for starting the animation
#
function Start-CMatrix {
param(
[int]$maxcolumns = 8,
[int]$frameWait = 100
)
$script:winsize = $host.ui.rawui.WindowSize
$script:columns = @{} # key: xpos; value; column
$script:framenum = 0
$prevbg = $host.ui.rawui.BackgroundColor
$host.ui.rawui.BackgroundColor = "black"
cls
$done = $false
while (-not $done) {
Write-FrameBuffer -maxcolumns $maxcolumns
Show-FrameBuffer
sleep -milli $frameWait
$done = $host.ui.rawui.KeyAvailable
}
$host.ui.rawui.BackgroundColor = $prevbg
cls
}
# TODO: actually write into buffercell[,] framebuffer
function Write-FrameBuffer {
param($maxColumns)
# do we need a new column?
if ($columns.count -lt $maxcolumns) {
# incur staggering of columns with get-random
# by only adding a new one 50% of the time
if ((get-random -min 0 -max 10) -lt 5) {
# search for a column not current animating
do {
$x = get-random -min 0 -max ($winsize.width - 1)
} while ($columns.containskey($x))
$columns.add($x, (new-column $x))
}
}
$script:framenum++
}
# TODO: setbuffercontent with buffercell[,] framebuffer
function Show-FrameBuffer {
param($frame)
$completed=@()
# loop through each active column and animate a single step/frame
foreach ($entry in $columns.getenumerator()) {
$column = $entry.value
# if column has finished animating, add to the "remove" pile
if (-not $column.step()) {
$completed += $entry.key
}
}
# cannot remove from collection while enumerating, so do it here
foreach ($key in $completed) {
$columns.remove($key)
}
}
function New-Column {
param($x)
# return a new module that represents the column of letters and its state
# we also pass in a reference to the main screensaver module to be able to
# access our console framebuffer functions.
new-module -ascustomobject -name "col_$x" -script {
param(
[int]$startx,
[PSModuleInfo]$parentModule
)
$script:xpos = $startx
$script:ylimit = $host.ui.rawui.WindowSize.Height
[int]$script:head = 1
[int]$script:fade = 0
$randomLengthVariation = (1 + (get-random -min -30 -max 50)/100)
[int]$script:fadelen = [math]::Abs($ylimit / 3 * $randomLengthVariation)
$script:fadelen += (get-random -min 0 -max $fadelen)
function Step {
# reached the bottom yet?
if ($head -lt $ylimit) {
& $parentModule Set-BufferCell $xpos $head (
& $parentModule New-BufferCell -Character `
([char](get-random -min 65 -max 122)) -Fore white) > $null
& $parentModule Set-BufferCell $xpos ($head - 1) (
& $parentModule New-BufferCell -Character `
([char](get-random -min 65 -max 122)) -Fore green) > $null
$script:head++
}
# time to start rendering the darker green "tail?"
if ($head -gt $fadelen) {
& $parentModule Set-BufferCell $xpos $fade (
& $parentModule New-BufferCell -Character `
([char](get-random -min 65 -max 122)) -Fore darkgreen) > $null
# tail end
$tail = $fade-1
if ($tail -lt $ylimit) {
& $parentModule Set-BufferCell $xpos ($fade-1) (
& $parentModule New-BufferCell -Character `
([char](get-random -min 65 -max 122)) -Fore black) > $null
}
$script:fade++
}
# are we done animating?
if ($fade -lt $ylimit) {
return $true
}
# remove last row from tail end
if (($fade - 1) -lt $ylimit) {
& $parentModule Set-BufferCell $xpos ($fade - 1) (
& $parentModule New-BufferCell -Character `
([char]' ') -Fore black) > $null
}
$false
}
Export-ModuleMember -function Step
} -args $x, $executioncontext.sessionstate.module
}
function Start-ScreenSaver {
# feel free to tweak maxcolumns and frame delay
# currently 20 columns with 30ms wait
Start-CMatrix -max 20 -frame 30
}
function Register-Timer {
# prevent prompt from reregistering if explicit disable
if ($_ssdisabled) {
return
}
if (-not (Test-Path variable:global:_ssjob)) {
# register our counter job
$global:_ssjob = Register-ObjectEvent $_sstimer elapsed -action {
$global:_sscount++;
$global:_sssrcid = $event.sourceidentifier
# hit timeout yet?
if ($_sscount -eq $_sstimeout) {
# disable this event (prevent choppiness)
Unregister-Event -sourceidentifier $_sssrcid
Remove-Variable _ssjob -scope Global
sleep -seconds 1
# start ss
Start-ScreenSaver
}
}
}
}
function Enable-ScreenSaver {
if (-not $_ssdisabled) {
write-warning "Screensaver is not disabled."
return
}
$global:_ssdisabled = $false
}
function Disable-ScreenSaver {
if ((Test-Path variable:global:_ssjob)) {
$global:_ssdisabled = $true
Unregister-Event -SourceIdentifier $_sssrcid
Remove-Variable _ssjob -Scope global
} else {
write-warning "Screen saver is not enabled."
}
}
function Get-ScreenSaverTimeout {
new-timespan -seconds $global:_sstimeout
}
function Set-ScreenSaverTimeout {
[cmdletbinding(defaultparametersetname="int")]
param(
[parameter(position=0, mandatory=$true, parametersetname="int")]
[int]$Seconds,
[parameter(position=0, mandatory=$true, parametersetname="timespan")]
[Timespan]$Timespan
)
if ($pscmdlet.parametersetname -eq "int") {
$timespan = new-timespan -seconds $Seconds
}
if ($timespan.totalseconds -lt 1) {
throw "Timeout must be greater than 0 seconds."
}
$global:_sstimeout = $timespan.totalseconds
}
#
# Eventing / Timer Hooks, clean up and Prompt injection
#
# timeout
[int]$global:_sstimeout = 180 # default 3 minutes
# tick count
[int]$global:_sscount = 0
# modify current prompt function to reset ticks counter to 0 and
# to reregister timer, while saving for later on module onload
$self = $ExecutionContext.SessionState.Module
$function:global:prompt = $self.NewBoundScriptBlock(
[scriptblock]::create(
("{0}`n`$global:_sscount = 0`nRegister-Timer" `
-f ($global:_ssprompt = gc function:prompt))))
# configure our timer
$global:_sstimer = new-object system.timers.timer
$_sstimer.Interval = 1000 # tick once a second
$_sstimer.AutoReset = $true
$_sstimer.start()
# we start out disabled - use enable-screensaver
$global:_ssdisabled = $true
# arrange clean up on module remove
$ExecutionContext.SessionState.Module.OnRemove = {
# restore prompt
$function:global:prompt = [scriptblock]::Create($_ssprompt)
# kill off eventing subscriber, if one exists
if ($_sssrcid) {
Unregister-Event -SourceIdentifier $_sssrcid
}
# clean up timer
$_sstimer.Dispose()
# clear out globals
remove-variable _ss* -scope global
}
Export-ModuleMember -function Start-ScreenSaver, Get-ScreenSaverTimeout, `
Set-ScreenSaverTimeout, Enable-ScreenSaver, Disable-ScreenSaver