Skip to content

Commit eab0fe0

Browse files
feat: Save-Turtle defaults to SVG ( Fixes #259 )
1 parent 24fef7e commit eab0fe0

File tree

1 file changed

+46
-16
lines changed

1 file changed

+46
-16
lines changed

Commands/Save-Turtle.ps1

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ function Save-Turtle {
33
.SYNOPSIS
44
Saves a turtle.
55
.DESCRIPTION
6-
Saves a turtle graphics pattern to a file.
6+
Saves Turtle graphics to a file.
77
.EXAMPLE
8-
New-Turtle |
9-
Move-Turtle SierpinskiTriangle 20 3 |
10-
Save-Turtle "./SierpinskiTriangle.svg"
8+
turtle SierpinskiTriangle 42 4 |
9+
Save-Turtle ./SierpinskiTriangle-42-4.svg
10+
.EXAMPLE
11+
turtle Flower 42 |
12+
Save-Turtle ./Flower-42.svg Pattern
1113
.EXAMPLE
1214
Move-Turtle BoxFractal 15 5 |
1315
Set-Turtle Stroke '#4488ff' |
@@ -37,7 +39,7 @@ function Save-Turtle {
3739
}
3840
})]
3941
[string]
40-
$Property = 'Symbol',
42+
$Property = 'SVG',
4143

4244
# The turtle input object.
4345
[Parameter(ValueFromPipeline)]
@@ -47,27 +49,55 @@ function Save-Turtle {
4749
)
4850

4951
process {
52+
# If there is no input, return
5053
if (-not $inputObject) { return }
51-
switch -regex ($FilePath) {
52-
'\.png$' { if ($Property -eq 'Symbol') { $Property = 'PNG' } }
53-
'\.jpe?g$' { if ($Property -eq 'Symbol') { $Property = 'JPEG' } }
54-
'\.webp$' { if ($Property -eq 'Symbol') { $Property = 'WEBP' } }
55-
}
56-
$toExport = $inputObject.$Property
54+
# Auto detect property names from file names
55+
$defaultToProperty =
56+
switch -regex ($FilePath) {
57+
'\.png$' { 'PNG' }
58+
'\.jpe?g$' { 'JPEG' }
59+
'\.webp$' { 'WEBP' }
60+
default { 'SVG' }
61+
}
62+
63+
# If we have not provided a property and we know of a viable default, use that
64+
if ($defaultToProperty -and -not $PSBoundParameters['Property']) {
65+
$property = $PSBoundParameters['Property'] = $defaultToProperty
66+
}
67+
68+
# Get the value of our property
69+
$toExport = $inputObject.$Property
70+
71+
# If there is nothing there, return
5772
if (-not $toExport) { return }
73+
74+
# Find the file path
5875
$unresolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($FilePath)
76+
# and create a file
5977
$null = New-Item -ItemType File -Force -Path $unresolvedPath
60-
if ($toExport -is [xml]) {
78+
# If we are exporting XML
79+
if ($toExport -is [xml])
80+
{
81+
# save it to that path
6182
$toExport.Save("$unresolvedPath")
6283
}
63-
elseif ($toExport -is [byte[]]) {
64-
Set-Content -Path $unresolvedPath -Value $toExport -AsByteStream
65-
} else {
84+
# If we are outputting bytes
85+
elseif ($toExport -is [byte[]])
86+
{
87+
# write them to the file
88+
[IO.File]::WriteAllBytes("$unresolvedPath", $toExport)
89+
}
90+
# If we are outputting anything else
91+
else
92+
{
93+
# simply redirect to the file
6694
$toExport > $unresolvedPath
6795
}
6896

97+
# If the last command worked
6998
if ($?) {
70-
Get-Item -Path $unresolvedPath
99+
# return the file
100+
return (Get-Item -Path $unresolvedPath)
71101
}
72102
}
73103
}

0 commit comments

Comments
 (0)