Skip to content

Commit a85522b

Browse files
feat: Get-Turtle argument improvement and tracking ( Fixes #157, Fixes #169, Fixes #170 )
1 parent 7a4bf35 commit a85522b

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

Commands/Get-Turtle.ps1

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ function Get-Turtle {
3030
Each argument can be the name of a move of the turtle object.
3131
3232
After a member name is encountered, subsequent arguments will be passed to the member as parameters.
33+
34+
Any parameter that begins with whitespace will be split into multiple words.
3335
.EXAMPLE
3436
# We can write shapes as a series of steps
3537
turtle "
@@ -370,6 +372,10 @@ function Get-Turtle {
370372
$memberNames = $memberNames | Sort-Object @{Expression={ $_.Length };Descending=$true}, name
371373
# Create a new turtle object in case we have no turtle input.
372374
$currentTurtle = [PSCustomObject]@{PSTypeName='Turtle'}
375+
376+
$invocationInfo = $MyInvocation
377+
$invocationInfo |
378+
Add-Member ScriptProperty History {Get-History -Id $this.HistoryId} -Force
373379
}
374380

375381
process {
@@ -381,21 +387,31 @@ function Get-Turtle {
381387
return $PSBoundParameters.InputObject
382388
}
383389

390+
if (-not $currentTurtle.Invocations) {
391+
$currentTurtle | Add-Member NoteProperty Invocations -Force @(,$invocationInfo)
392+
} elseif ($currentTurtle.Invocations -is [object[]]) {
393+
$currentTurtle.Invocations += $invocationInfo
394+
}
395+
384396

385397
# First we want to split each argument into words.
386398
# This way, it is roughly the same if you say:
387399
# * `turtle 'forward 10'`
388400
# * `turtle forward 10`
389401
# * `turtle 'forward', 10`
390402
$wordsAndArguments = @(foreach ($arg in $ArgumentList) {
391-
# If the argument is a string, split it by whitespace.
403+
# If the argument is a string, and it starts with whitespace
392404
if ($arg -is [string]) {
393-
$arg -split '\s{1,}'
405+
if ($arg -match '^[\r\n\s]+') {
406+
$arg -split '\s{1,}'
407+
} else {
408+
$arg
409+
}
394410
} else {
395411
# otherwise, leave the argument alone.
396412
$arg
397413
}
398-
})
414+
})
399415

400416
# Now that we have a series of words, we can process them.
401417
# We want to keep track of the current member,
@@ -406,7 +422,7 @@ function Get-Turtle {
406422

407423
# To do this in one pass, we will iterate through the words and arguments.
408424
# We use an indexed loop so we can skip past claimed arguments.
409-
for ($argIndex =0; $argIndex -lt $wordsAndArguments.Length; $argIndex++) {
425+
for ($argIndex =0; $argIndex -lt $wordsAndArguments.Length; $argIndex++) {
410426
$arg = $wordsAndArguments[$argIndex]
411427
# If the argument is not in the member names list, we can complain about it.
412428
if ($arg -notin $memberNames) {
@@ -415,7 +431,6 @@ function Get-Turtle {
415431
}
416432
continue
417433
}
418-
419434

420435
# If we have a current member, we can invoke it or get it.
421436
$currentMember = $arg
@@ -478,7 +493,16 @@ function Get-Turtle {
478493

479494
# If we have any arguments,
480495
if ($argList) {
481-
# lets try to set it.
496+
# Check to see if they are strongly typed
497+
if ($memberInfo -is [Management.Automation.Runspaces.ScriptPropertyData]) {
498+
$desiredType = $memberInfo.SetScriptBlock.Ast.ParamBlock.Parameters.StaticType
499+
if ($desiredType -is [Type] -and
500+
$argList.Length -eq 1 -and
501+
$argList[0] -as $desiredType) {
502+
$argList = $argList[0] -as $desiredType
503+
}
504+
}
505+
# lets try to set it.
482506
$currentTurtle.$currentMember = $argList
483507
} else {
484508
# otherwise, lets get the property
@@ -492,9 +516,9 @@ function Get-Turtle {
492516
# Properties being returned will largely be strings or numbers, and these will always output directly.
493517
if ($null -ne $stepOutput -and -not ($stepOutput.pstypenames -eq 'Turtle')) {
494518
# Output the step
495-
$stepOutput
519+
$stepOutput
496520
# and set the output turtle to false.
497-
$outputTurtle = $false
521+
$outputTurtle = $false
498522
} elseif ($null -ne $stepOutput) {
499523
# Set the current turtle to the step output.
500524
$currentTurtle = $stepOutput

0 commit comments

Comments
 (0)