@@ -3,11 +3,13 @@ function Save-Turtle {
3
3
. SYNOPSIS
4
4
Saves a turtle.
5
5
. DESCRIPTION
6
- Saves a turtle graphics pattern to a file.
6
+ Saves Turtle graphics to a file.
7
7
. 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
11
13
. EXAMPLE
12
14
Move-Turtle BoxFractal 15 5 |
13
15
Set-Turtle Stroke '#4488ff' |
@@ -37,7 +39,7 @@ function Save-Turtle {
37
39
}
38
40
})]
39
41
[string ]
40
- $Property = ' Symbol ' ,
42
+ $Property = ' SVG ' ,
41
43
42
44
# The turtle input object.
43
45
[Parameter (ValueFromPipeline )]
@@ -47,27 +49,55 @@ function Save-Turtle {
47
49
)
48
50
49
51
process {
52
+ # If there is no input, return
50
53
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
57
72
if (-not $toExport ) { return }
73
+
74
+ # Find the file path
58
75
$unresolvedPath = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath ($FilePath )
76
+ # and create a file
59
77
$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
61
82
$toExport.Save (" $unresolvedPath " )
62
83
}
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
66
94
$toExport > $unresolvedPath
67
95
}
68
96
97
+ # If the last command worked
69
98
if ($? ) {
70
- Get-Item - Path $unresolvedPath
99
+ # return the file
100
+ return (Get-Item - Path $unresolvedPath )
71
101
}
72
102
}
73
103
}
0 commit comments