functions/Add-SBLine.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
50
51
52
53
54
55
56
57
58
59
60
function Add-SBLine
{
<#
    .SYNOPSIS
        Add a line of text to a string builder.
     
    .DESCRIPTION
        Add a line of text to a string builder.
     
    .PARAMETER Text
        The text to add.
     
    .PARAMETER Values
        Any values to format into the text specified.
        Uses the -f operator.
     
    .PARAMETER Name
        Name of the string builder to work with.
        Defaults to the caller's module name (if present) or '<none>' (if not so)
     
    .EXAMPLE
        PS C:\> Add-SBLine -Text 'Example line of text'
     
        Adds the specified string into the open string builder.
     
    .EXAMPLE
        PS C:\> Add-SBLine -Text '{0} is larger than {1}' -Values 42,23
     
        Adds the specified string into the open string builder, after formatting in the values offered.
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, Position = 0)]
        [string]
        $Text,
        
        [Alias('f')]
        [object[]]
        $Values,
        
        [PsfArgumentCompleter('StringBuilder.Name')]
        [string]
        $Name
    )
    
    begin {
        $sbName = [PSFramework.Utility.UtilityHost]::FriendlyCallstack.Entries[1].InvocationInfo.MyCommand.Module.Name
        if (-not $sbName) { $sbName = '<none>' }
        if ($Name) { $sbName = $Name }
        
        Assert-StringBuilder -Name $sbName -Cmdlet $PSCmdlet
    }
    process
    {
        $newLine = $Text
        if ($Values) { $newLine = $Text -f $Values }
        
        $null = $script:builders[$sbName].AppendLine($newLine)
    }
}