Skip to content

Commit 75d2d53

Browse files
author
James Brundage
committed
feat: Get-Turtle ( Fixes #77 )
1 parent e83c558 commit 75d2d53

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

Commands/Get-Turtle.ps1

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
function Get-Turtle {
2+
<#
3+
.EXAMPLE
4+
turtle square 50
5+
.EXAMPLE
6+
turtle circle 10
7+
.EXAMPLE
8+
turtle polygon 10 6
9+
.EXAMPLE
10+
turtle ('forward', 10, 'rotate', 120 * 3)
11+
#>
12+
[CmdletBinding(PositionalBinding=$false)]
13+
[Alias('turtle')]
14+
param(
15+
# The arguments to pass to turtle.
16+
[ArgumentCompleter({
17+
param ( $commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters )
18+
if (-not $script:TurtleTypeData) {
19+
$script:TurtleTypeData = Get-TypeData -TypeName Turtle
20+
}
21+
$methodNames = @(foreach ($memberName in $script:TurtleTypeData.Members.Keys) {
22+
if ($script:TurtleTypeData.Members[$memberName] -is
23+
[Management.Automation.Runspaces.ScriptMethodData]) {
24+
$memberName
25+
}
26+
})
27+
28+
if ($wordToComplete) {
29+
return $methodNames -like "$wordToComplete*"
30+
} else {
31+
return $methodNames
32+
}
33+
})]
34+
[Parameter(ValueFromRemainingArguments)]
35+
[PSObject[]]
36+
$ArgumentList,
37+
38+
# Any input object to process.
39+
# If this is already a turtle object, the arguments will be applied to this object.
40+
# If the input object is not a turtle object, it will be ignored and a new turtle object will be created.
41+
[Parameter(ValueFromPipeline)]
42+
[PSObject]
43+
$InputObject
44+
)
45+
46+
begin {
47+
$turtleType = Get-TypeData -TypeName Turtle
48+
$memberNames = @(foreach ($memberName in $turtleType.Members.Keys) {
49+
if (
50+
($turtleType.Members[$memberName] -is [Management.Automation.Runspaces.ScriptMethodData]) -or
51+
(
52+
$turtleType.Members[$memberName] -is
53+
[Management.Automation.Runspaces.AliasPropertyData] -and
54+
$turtleType.Members[
55+
$turtleType.Members[$memberName].ReferencedMemberName
56+
] -is [Management.Automation.Runspaces.ScriptMethodData]
57+
)
58+
) {
59+
$memberName
60+
}
61+
})
62+
63+
$memberNames = $memberNames | Sort-Object @{Expression={ $_.Length };Descending=$true}, Name
64+
$currentTurtle = [PSCustomObject]@{PSTypeName='Turtle'}
65+
}
66+
67+
process {
68+
69+
if ($PSBoundParameters.InputObject -and
70+
$PSBoundParameters.InputObject.pstypenames -eq 'Turtle') {
71+
$currentTurtle = $PSBoundParameters.InputObject
72+
}
73+
74+
$currentMethod = $null
75+
76+
$wordsAndArguments = foreach ($arg in $ArgumentList) {
77+
if ($arg -is [string]) {
78+
$arg -split '\s{1,}'
79+
} else {
80+
$arg
81+
}
82+
}
83+
84+
:findCommand for ($argIndex =0; $argIndex -lt $wordsAndArguments.Length; $argIndex++) {
85+
$arg = $wordsAndArguments[$argIndex]
86+
if ($arg -in $memberNames) {
87+
$currentMethod = $arg
88+
for (
89+
$methodArgIndex = $argIndex + 1;
90+
$methodArgIndex -lt $wordsAndArguments.Length -and
91+
$wordsAndArguments[$methodArgIndex] -notin $memberNames;
92+
$methodArgIndex++) {
93+
}
94+
# Command without parameters
95+
if ($methodArgIndex -eq $argIndex) {
96+
$argList = @()
97+
$currentTurtle = $currentTurtle.$currentMethod.Invoke()
98+
}
99+
else {
100+
$argList = $wordsAndArguments[($argIndex + 1)..($methodArgIndex - 1)]
101+
$currentTurtle = $currentTurtle.$currentMethod.Invoke($argList)
102+
# "$($currentMethod) $($argList -join ' ')"
103+
$argIndex = $methodArgIndex - 1
104+
}
105+
}
106+
}
107+
108+
return $currentTurtle
109+
}
110+
}

0 commit comments

Comments
 (0)