Skip to content

Commit d0fe05c

Browse files
StartAutomatingStartAutomating
authored andcommitted
Updating YAML Formatter (Fixes #180)
1 parent 1787858 commit d0fe05c

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

PipeScript.format.ps1xml

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-16"?>
2-
<!-- Generated with EZOut 1.8.8: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
2+
<!-- Generated with EZOut 1.8.8.1: Install-Module EZOut or https://github.com/StartAutomating/EZOut -->
33
<Configuration>
44
<Controls>
55
<Control>
@@ -1153,12 +1153,15 @@ $BackgroundColor
11531153
# The InputObject.
11541154
[Parameter(ValueFromPipeline)]
11551155
[PSObject]
1156-
$inputObject,
1156+
$InputObject,
11571157

11581158
# If set, will make a YAML header by adding a YAML Document tag above and below output.
11591159
[Alias('YAMLDocument')]
11601160
[switch]
1161-
$YamlHeader
1161+
$YamlHeader,
1162+
1163+
[int]
1164+
$Indent = 0
11621165
)
11631166

11641167
begin {
@@ -1201,7 +1204,7 @@ $BackgroundColor
12011204
}
12021205
return # Once the string has been emitted, return.
12031206
}
1204-
if ( [int], [float], [bool] -contains $Object.GetType() ) { # If it is an [int] or [float] or [bool]
1207+
if ( $Object.GetType().IsPrimitive ) { # If it is a primitive type
12051208
"$Object".ToLower() # Emit it in lowercase.
12061209
if ($Parent -is [Collections.IList]) {
12071210
[Environment]::NewLine
@@ -1239,14 +1242,12 @@ $BackgroundColor
12391242
foreach ($Obj in $Object) {
12401243
$allPrimitive = $allPrimitive -band (
12411244
$Obj -is [string] -or
1242-
$obj -is [int] -or
1243-
$obj -is [float] -or
1244-
$obj -is [bool]
1245+
$obj.GetType().IsPrimitive
12451246
)
12461247
}
12471248
if ($parent -and -not $allPrimitive) {
12481249
$Indent += 2
1249-
}
1250+
}
12501251
}
12511252

12521253

@@ -1272,16 +1273,21 @@ $BackgroundColor
12721273
#endregion Nested
12731274
}
12741275
}
1276+
function IndentString([string]$String,[int]$Indent) {
1277+
@(foreach ($line in @($String -split '(?&gt;\r\n|\n)')) {
1278+
(' ' * $indent) + $line
1279+
}) -join [Environment]::NewLine
1280+
}
12751281
$inputWasNotPiped = $PSBoundParameters.InputObject -as [bool]
12761282
$allInputObjects = @()
12771283
}
12781284

12791285
process {
12801286
if ($inputWasNotPiped) {
1281-
'' + $(if ($YamlHeader) { '---' + [Environment]::NewLine }) + (
1287+
IndentString ('' + $(if ($YamlHeader) { '---' + [Environment]::NewLine }) + (
12821288
(&amp; $toYaml -object $inputObject) -join '' -replace
12831289
"$([Environment]::NewLine * 2)", [Environment]::NewLine
1284-
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})
1290+
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})) -Indent $Indent
12851291
} else {
12861292
$allInputObjects += $inputObject
12871293
}
@@ -1290,15 +1296,15 @@ $BackgroundColor
12901296
end {
12911297
if (-not $allInputObjects) { return }
12921298
if ($allInputObjects.Length -eq 1) {
1293-
'' + $(if ($YamlHeader) { '---' + [Environment]::NewLine}) + (
1299+
IndentString ('' + $(if ($YamlHeader) { '---' + [Environment]::NewLine}) + (
12941300
(&amp; $toYaml -object $inputObject) -join '' -replace
12951301
"$([Environment]::NewLine * 2)", [Environment]::NewLine
1296-
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})
1302+
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})) -Indent $Indent
12971303
} else {
1298-
'' + $(if ($YamlHeader) { '---' + [Environment]::NewLine}) + (
1304+
IndentString ('' + $(if ($YamlHeader) { '---' + [Environment]::NewLine}) + (
12991305
(&amp; $toYaml -object $allInputObjects) -join '' -replace
13001306
"$([Environment]::NewLine * 2)", [Environment]::NewLine
1301-
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})
1307+
) + $(if ($YamlHeader) { [Environment]::NewLine + '---'})) -Indent $Indent
13021308
}
13031309
}
13041310
</ScriptBlock>
@@ -1627,7 +1633,19 @@ $BackgroundColor
16271633

16281634

16291635

1630-
&amp; ${PipeScript_Format-YAML} -inputObject $_
1636+
$inputObject = $_
1637+
if ($inputObject.psobject.properties['Indent'].Value -as [int]) {
1638+
$indentLevel = $inputObject.Indent -as [int]
1639+
$inputCopy = [Ordered]@{}
1640+
foreach ($prop in $inputObject.psobject.properties) {
1641+
if ($prop.Name -eq 'indent') { continue }
1642+
$inputCopy[$prop.Name] = $prop.Value
1643+
}
1644+
&amp; ${PipeScript_Format-YAML} -inputObject $inputCopy -Indent $indentLevel
1645+
} else {
1646+
&amp; ${PipeScript_Format-YAML} -inputObject $inputObject
1647+
}
1648+
16311649
</ScriptBlock>
16321650
</ExpressionBinding>
16331651
</CustomItem>

0 commit comments

Comments
 (0)