Skip to content

Commit 2d213e2

Browse files
[release/v7.6] Add Delimiter parameter to Get-Clipboard (#26572)
Co-authored-by: MartinGC94 <42123497+MartinGC94@users.noreply.github.com>
1 parent 6827de5 commit 2d213e2

2 files changed

Lines changed: 47 additions & 2 deletions

File tree

src/Microsoft.PowerShell.Commands.Management/commands/management/GetClipboardCommand.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
// Licensed under the MIT License.
33

44
using System;
5+
using System.Collections;
56
using System.Collections.Generic;
67
using System.Management.Automation;
8+
using System.Management.Automation.Language;
79
using Microsoft.PowerShell.Commands.Internal;
810

911
namespace Microsoft.PowerShell.Commands
@@ -34,6 +36,13 @@ public SwitchParameter Raw
3436
}
3537
}
3638

39+
/// <summary>
40+
/// Gets or sets the delimiters to use when splitting the clipboard content.
41+
/// </summary>
42+
[Parameter]
43+
[ArgumentCompleter(typeof(DelimiterCompleter))]
44+
public string[] Delimiter { get; set; } = [Environment.NewLine];
45+
3746
private bool _raw;
3847

3948
/// <summary>
@@ -68,11 +77,40 @@ private List<string> GetClipboardContentAsText()
6877
}
6978
else
7079
{
71-
string[] splitSymbol = { Environment.NewLine };
72-
result.AddRange(textContent.Split(splitSymbol, StringSplitOptions.None));
80+
result.AddRange(textContent.Split(Delimiter, StringSplitOptions.None));
7381
}
7482

7583
return result;
7684
}
7785
}
86+
87+
/// <summary>
88+
/// Provides argument completion for the Delimiter parameter.
89+
/// </summary>
90+
public sealed class DelimiterCompleter : IArgumentCompleter
91+
{
92+
/// <summary>
93+
/// Provides argument completion for the Delimiter parameter.
94+
/// </summary>
95+
/// <param name="commandName">The name of the command that is being completed.</param>
96+
/// <param name="parameterName">The name of the parameter that is being completed.</param>
97+
/// <param name="wordToComplete">The input text to filter the results by.</param>
98+
/// <param name="commandAst">The ast of the command that triggered the completion.</param>
99+
/// <param name="fakeBoundParameters">The parameters bound to the command.</param>
100+
/// <returns>Completion results.</returns>
101+
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
102+
{
103+
wordToComplete ??= string.Empty;
104+
var pattern = new WildcardPattern(wordToComplete + '*', WildcardOptions.IgnoreCase);
105+
if (pattern.IsMatch("CRLF") || pattern.IsMatch("Windows"))
106+
{
107+
yield return new CompletionResult("\"`r`n\"", "CRLF", CompletionResultType.ParameterValue, "Windows (CRLF)");
108+
}
109+
110+
if (pattern.IsMatch("LF") || pattern.IsMatch("Unix") || pattern.IsMatch("Linux"))
111+
{
112+
yield return new CompletionResult("\"`n\"", "LF", CompletionResultType.ParameterValue, "UNIX (LF)");
113+
}
114+
}
115+
}
78116
}

test/powershell/Modules/Microsoft.PowerShell.Management/Clipboard.Tests.ps1

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ Describe 'Clipboard cmdlet tests' -Tag CI {
3636
Get-Clipboard -Raw | Should -BeExactly "1$([Environment]::NewLine)2"
3737
}
3838

39+
It 'Get-Clipboard -Delimiter should return items based on the delimiter' {
40+
Set-Clipboard -Value "Line1`r`nLine2`nLine3"
41+
$result = Get-Clipboard -Delimiter "`r`n", "`n"
42+
$result.Count | Should -Be 3
43+
$result | ForEach-Object -Process {$_.Length | Should -Be "LineX".Length}
44+
}
45+
3946
It 'Set-Clipboard -Append will add text' {
4047
'hello' | Set-Clipboard
4148
'world' | Set-Clipboard -Append

0 commit comments

Comments
 (0)