Skip to content

Commit

Permalink
feat: Template.WhileLoop.py ( Fixes #936 )
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Feb 17, 2024
1 parent a043926 commit 459a06f
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions Languages/Python/Templates/Python-Template-WhileLoop.ps.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Template function WhileLoop.py {
<#
.SYNOPSIS
Template for a Python `while` Loop
.DESCRIPTION
Template for a `while` loop in Python.
.EXAMPLE
Template.WhileLoop.py -Condition "false" -Body 'print("This never happens")'
#>
param(
# The Loop's Condition.
# This determines if the loop should continue running.
[vbn()]
[string]
$Condition,

# The body of the loop
[vbn()]
[string]
$Body,

# The number of spaces to indent the body.
# By default, two.
[vbn()]
[int]
$BodyIndent = 2,

# The number of spaces to indent all code.
# By default, zero
[vbn()]
[int]
$Indent = 0
)

process {
if ($body -match '^\{') {
$body = $body -replace '^\s{0,}\{' -replace '\}\s{0,}$'
}
if ($Condition -match '^\$') {
$Condition = $Condition -replace '^\$'
}
if ($Condition -match '^(?>true|false)$') {
$Condition = $Condition.Substring(0,1).ToUpper() + $Condition.Substring(1).ToLower()
}
'' +
$(if ($Indent) {(' ' * $Indent)}) +
(@"
while ${Condition}:
$(' ' * $BodyIndent)$(
$Body -split "(?>\r\n|\n)" -join ([Environment]::NewLine + $(' ' * $BodyIndent))
)
"@ -split '(?>\r\n|\n)' -join ([Environment]::NewLine + $(' ' * $Indent)))
}
}


0 comments on commit 459a06f

Please sign in to comment.