// Copyright (c) Microsoft Corporation. // 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 { /// /// Defines the implementation of the 'Get-Clipboard' cmdlet. /// This cmdlet get the content from system clipboard. /// [Cmdlet(VerbsCommon.Get, "Clipboard", HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2109905")] [Alias("gcb")] [OutputType(typeof(string))] public class GetClipboardCommand : PSCmdlet { /// /// Property that sets raw parameter. This will allow clipboard return text or file list as one string. /// [Parameter] public SwitchParameter Raw { get { return _raw; } set { _raw = value; } } /// /// Gets or sets the delimiters to use when splitting the clipboard content. /// [Parameter] [ArgumentCompleter(typeof(DelimiterCompleter))] public string[] Delimiter { get; set; } = [Environment.NewLine]; private bool _raw; /// /// This method implements the ProcessRecord method for Get-Clipboard command. /// protected override void BeginProcessing() { this.WriteObject(GetClipboardContentAsText(), true); } /// /// Returns the clipboard content as text format. /// /// Array of strings representing content from clipboard. private List GetClipboardContentAsText() { var result = new List(); string textContent = null; try { textContent = Clipboard.GetText(); } catch (PlatformNotSupportedException) { ThrowTerminatingError(new ErrorRecord(new InvalidOperationException(ClipboardResources.UnsupportedPlatform), "FailedToGetClipboardUnsupportedPlatform", ErrorCategory.InvalidOperation, "Clipboard")); } if (_raw) { result.Add(textContent); } else { result.AddRange(textContent.Split(Delimiter, StringSplitOptions.None)); } return result; } } /// /// Provides argument completion for the Delimiter parameter. /// public sealed class DelimiterCompleter : IArgumentCompleter { /// /// Provides argument completion for the Delimiter parameter. /// /// The name of the command that is being completed. /// The name of the parameter that is being completed. /// The input text to filter the results by. /// The ast of the command that triggered the completion. /// The parameters bound to the command. /// Completion results. public IEnumerable 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)"); } } } }