Skip to content

Commit 7062f2e

Browse files
author
James Brundage
committed
feat: Set-Turtle ( Fixes #65 )
1 parent 6271b60 commit 7062f2e

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

Commands/Set-Turtle.ps1

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function Set-Turtle {
2+
<#
3+
.SYNOPSIS
4+
Sets a turtle.
5+
.DESCRIPTION
6+
Changes properties of a turtle, and returns the turtle.
7+
.EXAMPLE
8+
New-Turtle |
9+
Move-Turtle SierpinskiTriangle 20 3 |
10+
Set-Turtle -Property Stroke -Value '#4488ff' |
11+
Save-Turtle "./SierpinskiTriangle.svg"
12+
#>
13+
param(
14+
# The property of the turtle to set.
15+
[Parameter(Mandatory)]
16+
[ArgumentCompleter({
17+
param ( $commandName,
18+
$parameterName,
19+
$wordToComplete,
20+
$commandAst,
21+
$fakeBoundParameters )
22+
$myInv = $myInvocation
23+
$turtleType = Get-TypeData -TypeName Turtle
24+
$propertyNames = @(foreach ($memberName in $turtleType.Members.Keys) {
25+
if ($turtleType.Members[$memberName] -is [System.Management.Automation.Runspaces.ScriptPropertyData] -and
26+
$turtleType.Members[$memberName].SetScriptBlock) {
27+
$memberName
28+
}
29+
})
30+
31+
if ($wordToComplete) {
32+
return $propertyNames -like "$wordToComplete*"
33+
} else {
34+
return $propertyNames
35+
}
36+
})]
37+
[string]
38+
$Property = 'Stroke',
39+
40+
# The value to set.
41+
[PSObject]
42+
$Value,
43+
44+
# The turtle input object.
45+
[Parameter(ValueFromPipeline)]
46+
[Alias('Turtle')]
47+
[PSObject]
48+
$InputObject
49+
)
50+
51+
process {
52+
if (-not $inputObject) { return }
53+
$propInfo = $inputObject.psobject.properties[$property]
54+
if (-not $propInfo.SetterScript) {
55+
Write-Error "Property '$property' can not be set."
56+
return
57+
}
58+
$inputObject.$property = $Value
59+
return $inputObject
60+
}
61+
}

0 commit comments

Comments
 (0)