mirror of
https://github.com/fleschutz/PowerShell.git
synced 2024-11-08 09:04:18 +01:00
Update write-fractal.ps1 and write-matrix.ps1
This commit is contained in:
parent
2c2567fe5c
commit
fa694a5278
@ -27,7 +27,7 @@ function CalculateFractal([float]$left, [float]$top, [float]$xside, [float]$ysid
|
||||
$zy = $zoom * $zx * $zy + $cy
|
||||
$zx = $tempx
|
||||
}
|
||||
$global:buffer[$y * $maxx + $x] = $([char](65 + $count))
|
||||
$global:buf[$y * $maxx + $x] = $([char](65 + $count))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -41,20 +41,20 @@ $rui = $ui.rawui
|
||||
[float]$yside = 0.45
|
||||
$buffer0 = ""
|
||||
1..($rui.MaxWindowSize.Width * $rui.MaxWindowSize.Height) | ForEach-Object { $buffer0 += " " }
|
||||
$global:buffer = $buffer0.ToCharArray()
|
||||
$global:buf = $buffer0.ToCharArray()
|
||||
|
||||
while ($true) {
|
||||
for ([float]$zoom = 4.0; $zoom -gt 1.1; $zoom -= 0.02) {
|
||||
CalculateFractal $left $top $xside $yside $zoom
|
||||
[console]::SetCursorPosition(0,0)
|
||||
[string]$drawscreen = New-Object system.string($global:buffer, 0, $global:buffer.Length)
|
||||
Write-Host -foreground green $drawscreen -noNewline
|
||||
[string]$Screen = New-Object system.string($global:buf, 0, $global:buf.Length)
|
||||
Write-Host -foreground green $Screen -noNewline
|
||||
}
|
||||
for ([float]$zoom = 1.1; $zoom -lt 4.0; $zoom += 0.02) {
|
||||
CalculateFractal $left $top $xside $yside $zoom
|
||||
[console]::SetCursorPosition(0,0)
|
||||
[string]$drawscreen = New-Object system.string($global:buffer, 0, $global:buffer.Length)
|
||||
Write-Host -foreground green $drawscreen -noNewline
|
||||
[string]$Screen = New-Object system.string($global:buf, 0, $global:buf.Length)
|
||||
Write-Host -foreground green $Screen -noNewline
|
||||
}
|
||||
}
|
||||
exit 0 # success
|
@ -1,396 +1,60 @@
|
||||
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++
|
||||
}
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Writes the matrix
|
||||
.DESCRIPTION
|
||||
This PowerShell script writes the animated Matrix.
|
||||
.EXAMPLE
|
||||
PS> ./write-fractal
|
||||
.LINK
|
||||
https://github.com/fleschutz/PowerShell
|
||||
.NOTES
|
||||
Author: Markus Fleschutz | License: CC0
|
||||
#>
|
||||
|
||||
# time to start rendering the darker green "tail?"
|
||||
if ($head -gt $fadelen) {
|
||||
function CalculateMatrix { param([int]$Pos, [char]$Letter)
|
||||
[int]$maxx = $rui.MaxWindowSize.Width
|
||||
[int]$maxy = $rui.MaxWindowSize.Height
|
||||
[int]$y = 0
|
||||
for ([int]$x = 0; $x -lt $maxx; $x++) {
|
||||
if ($x -eq $Pos) {
|
||||
$global:buf[$y * $maxx + $x] = $Letter
|
||||
} else {
|
||||
$global:buf[$y * $maxx + $x] = [char]32
|
||||
}
|
||||
}
|
||||
for ([int]$y = ($maxy - 1); $y -gt 0; $y--) {
|
||||
for ([int]$x = 0; $x -lt $maxx; $x++) {
|
||||
$global:buf[$y * $maxx + $x] = $global:buf[($y - 1) * $maxx + $x]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
& $parentModule Set-BufferCell $xpos $fade (
|
||||
& $parentModule New-BufferCell -Character `
|
||||
([char](get-random -min 65 -max 122)) -Fore darkgreen) > $null
|
||||
function NextLetter {
|
||||
if ($global:index -eq 6) { $global:index = 0; $global:pos = [int]$global:generator.next(0, $rui.MaxWindowSize.Width) }
|
||||
switch($global:index++) {
|
||||
0 { return 'X' }
|
||||
1 { return 'I' }
|
||||
2 { return 'R' }
|
||||
3 { return 'T' }
|
||||
4 { return 'A' }
|
||||
5 { return 'M' }
|
||||
}
|
||||
}
|
||||
|
||||
# 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
|
||||
}
|
||||
$ui = (Get-Host).ui
|
||||
$rui = $ui.rawui
|
||||
$buffer0 = ""
|
||||
1..($rui.MaxWindowSize.Width * $rui.MaxWindowSize.Height) | ForEach-Object { $buffer0 += " " }
|
||||
$global:buf = $buffer0.ToCharArray()
|
||||
$global:generator = New-Object System.Random
|
||||
$global:pos = [int]$global:generator.next(0, $rui.MaxWindowSize.Width)
|
||||
$global:index = 0
|
||||
|
||||
$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
|
||||
while ($true) {
|
||||
$Letter = NextLetter
|
||||
CalculateMatrix $global:pos $Letter
|
||||
[console]::SetCursorPosition(0,0)
|
||||
[string]$Screen = New-Object system.string($global:buf, 0, $global:buf.Length)
|
||||
Write-Host -foreground green $Screen -noNewline
|
||||
}
|
||||
|
||||
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
|
||||
#
|
||||
|
||||
Start-ScreenSaver
|
||||
# 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
|
||||
exit 0 # success
|
Loading…
Reference in New Issue
Block a user