forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathMeasure-Indents.ps1
49 lines (43 loc) · 1.41 KB
/
Measure-Indents.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<#
.SYNOPSIS
Measures the indentation characters used in a text file.
.INPUTS
System.String file path to examine.
.OUTPUTS
System.Management.Automation.PSCustomObject with properties indictating indentation counts.
* Tab: Lines starting with tabs.
* Space: Lines starting with spaces.
* Mix: Lines starting with both tabs and spaces.
* Other: Lines starting with any other whitespace characters than tab or space.
.EXAMPLE
Measure-Indents.ps1 Program.cs
Tab Space Mix Other
--- ----- --- -----
1 17 0 0
#>
#Requires -Version 3
[CmdletBinding()][OutputType([Management.Automation.PSCustomObject])] Param(
# A file to measure.
[Parameter(Position=0,Mandatory=$true,ValueFromPipeline=$true)][string]$Path
)
Begin { $Count = [pscustomobject]@{Tab=0;Space=0;Mix=0;Other=0} }
Process
{
foreach($line in (Get-Content $Path))
{
if($line -notmatch '^(?<Indent>\s+)') {continue}
$IndentChars = $Matches.Indent.ToCharArray() |Select-Object -Unique
if($IndentChars -is [object[]]) {$Count.Mix++}
elseif($IndentChars -is [char])
{
switch($IndentChars)
{
"`t" {$Count.Tab++}
' ' {$Count.Space++}
default {$Count.Other++}
}
}
else { Write-Warning "Unexpected indent type: $($IndentChars.GetType().FullName)" }
}
}
End { $Count }