Add Delimiter parameter to Get-Clipboard (#26134)

This commit is contained in:
MartinGC94
2025-10-15 21:50:37 +05:00
committed by GitHub
parent b77c80ec2f
commit 9c5ac7d622
2 changed files with 47 additions and 2 deletions
@@ -2,8 +2,10 @@
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Management.Automation;
using System.Management.Automation.Language;
using Microsoft.PowerShell.Commands.Internal;
namespace Microsoft.PowerShell.Commands
@@ -34,6 +36,13 @@ namespace Microsoft.PowerShell.Commands
}
}
/// <summary>
/// Gets or sets the delimiters to use when splitting the clipboard content.
/// </summary>
[Parameter]
[ArgumentCompleter(typeof(DelimiterCompleter))]
public string[] Delimiter { get; set; } = [Environment.NewLine];
private bool _raw;
/// <summary>
@@ -68,11 +77,40 @@ namespace Microsoft.PowerShell.Commands
}
else
{
string[] splitSymbol = { Environment.NewLine };
result.AddRange(textContent.Split(splitSymbol, StringSplitOptions.None));
result.AddRange(textContent.Split(Delimiter, StringSplitOptions.None));
}
return result;
}
}
/// <summary>
/// Provides argument completion for the Delimiter parameter.
/// </summary>
public sealed class DelimiterCompleter : IArgumentCompleter
{
/// <summary>
/// Provides argument completion for the Delimiter parameter.
/// </summary>
/// <param name="commandName">The name of the command that is being completed.</param>
/// <param name="parameterName">The name of the parameter that is being completed.</param>
/// <param name="wordToComplete">The input text to filter the results by.</param>
/// <param name="commandAst">The ast of the command that triggered the completion.</param>
/// <param name="fakeBoundParameters">The parameters bound to the command.</param>
/// <returns>Completion results.</returns>
public IEnumerable<CompletionResult> CompleteArgument(string commandName, string parameterName, string wordToComplete, CommandAst commandAst, IDictionary fakeBoundParameters)
{
wordToComplete ??= string.Empty;
var pattern = new WildcardPattern(wordToComplete + '*', WildcardOptions.IgnoreCase);
if (pattern.IsMatch("CRLF") || pattern.IsMatch("Windows"))
{
yield return new CompletionResult("\"`r`n\"", "CRLF", CompletionResultType.ParameterValue, "Windows (CRLF)");
}
if (pattern.IsMatch("LF") || pattern.IsMatch("Unix") || pattern.IsMatch("Linux"))
{
yield return new CompletionResult("\"`n\"", "LF", CompletionResultType.ParameterValue, "UNIX (LF)");
}
}
}
}
@@ -36,6 +36,13 @@ Describe 'Clipboard cmdlet tests' -Tag CI {
Get-Clipboard -Raw | Should -BeExactly "1$([Environment]::NewLine)2"
}
It 'Get-Clipboard -Delimiter should return items based on the delimiter' {
Set-Clipboard -Value "Line1`r`nLine2`nLine3"
$result = Get-Clipboard -Delimiter "`r`n", "`n"
$result.Count | Should -Be 3
$result | ForEach-Object -Process {$_.Length | Should -Be "LineX".Length}
}
It 'Set-Clipboard -Append will add text' {
'hello' | Set-Clipboard
'world' | Set-Clipboard -Append