-
Notifications
You must be signed in to change notification settings - Fork 1
Clear‑AnsiSequence
Clears ANSI CSI sequences from a string.
Clear-AnsiSequence [-InputString] <String> [-RemovePartial]
[<CommonParameters>]
The Clear-AnsiSequence command clears ANSI CSI (Control Sequence Introducer) sequences from a string, returning only the visible text content. This includes SGR (Select Graphic Rendition) sequences for colors and formatting, as well as cursor control sequences.
This is useful for calculating the actual visible length of strings that contain ANSI formatting codes, or for extracting plain text from formatted console output.
Note: This function specifically handles CSI sequences (ESC[ or just [). Other ANSI escape sequence types like OSC, DCS, PM, or APC are not processed.
Clear-AnsiSequence -InputString "`e[32mGreen text`e[0m"
This example clears ANSI CSI sequences from a string with properly escaped sequences, returning "Green text".
Clear-AnsiSequence -InputString "[31mRed text[0m"
This example clears ANSI CSI sequences from a string with unescaped sequences, returning "Red text".
Clear-AnsiSequence -InputString "Value is [32] units"
This example shows that plain bracketed numbers are preserved, returning "Value is [32] units".
Clear-AnsiSequence -InputString "[31incomplete" -RemovePartial
This example shows how the -RemovePartial switch removes unterminated sequences, returning "incomplete".
Clear-AnsiSequence -InputString "`e[1;37;44mBold white on blue`e[0m and plain text"
This example clears complex ANSI CSI sequences, returning "Bold white on blue and plain text".
$visibleLength = (Clear-AnsiSequence -InputString $formattedString).Length
This example shows how to get the visible length of a string that contains ANSI CSI sequences.
The string from which ANSI CSI sequences should be cleared. The string can contain any combination of ANSI CSI sequences with or without proper escape characters.
Type: String
Parameter Sets: (All)
Aliases:
Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: FalseWhen specified, also removes unterminated ANSI CSI sequences that don't end with 'm'. By default, unterminated sequences like "[31" are preserved to avoid accidentally removing plain bracketed numbers.
Type: SwitchParameter
Parameter Sets: (All)
Aliases:
Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: FalseThis cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.
This function handles various ANSI CSI (Control Sequence Introducer) sequence formats including:
- Properly escaped SGR sequences: `e[32m or [char]0x1b[32m (colors and formatting)
- Unescaped SGR sequences: [32m (only when they end with 'm')
- Non-SGR CSI sequences: ESC[2K, ESC[H (cursor control, only when properly escaped)
- Complex sequences with multiple codes: [1;37;44m
The function uses a three-pass approach: 1. Remove complete CSI sequences that start with an escape character 2. Remove SGR-like patterns that aren't escaped but end with 'm' 3. Optionally remove incomplete sequences with -RemovePartial
Plain bracketed numbers like "[32]" are preserved unless -RemovePartial is used.
Limitation: This function does not handle other ANSI escape sequence types such as OSC (Operating System Command), DCS (Device Control String), PM (Privacy Message), or APC (Application Program Command) sequences.
- Assert-GitLocalChange
- Assert-GitRemote
- Assert-IPv4Address
- Clear-AnsiSequence
- ConvertTo-AnsiSequence
- ConvertTo-AnsiString
- ConvertTo-DifferenceString
- ConvertTo-RelativePath
- Disable-CursorShortcutCode
- Get-ClassAst
- Get-ClassResourceAst
- Get-GitBranchCommit
- Get-GitLocalBranchName
- Get-GitRemote
- Get-GitRemoteBranch
- Get-GitTag
- Get-LinkLayerAddress
- Get-ModuleByVersion
- Get-ModuleFileSha
- Get-ModuleVersion
- Get-NumericalSequence
- Get-PSReadLineHistory
- Get-TextOffset
- Install-ModulePatch
- Invoke-Git
- Invoke-PesterJob
- New-GitTag
- New-SamplerGitHubReleaseTag
- Out-Difference
- Pop-VMLatestSnapshot
- Push-GitTag
- Receive-GitBranch
- Remove-GitTag
- Remove-History
- Remove-PSHistory
- Remove-PSReadLineHistory
- Rename-GitLocalBranch
- Rename-GitRemote
- Request-GitTag
- Resolve-DnsName
- Resume-GitRebase
- Send-WakeOnLan
- Split-StringAtIndex
- Start-GitRebase
- Stop-GitRebase
- Switch-GitLocalBranch
- Test-FileHash
- Test-GitLocalChanges
- Test-GitRemote
- Test-GitRemoteBranch
- Test-IPv4Address
- Update-GitLocalBranch
- Update-RemoteTrackingBranch