forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSetClipboardCommand.cs
More file actions
124 lines (109 loc) · 4.32 KB
/
Copy pathSetClipboardCommand.cs
File metadata and controls
124 lines (109 loc) · 4.32 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Management.Automation;
using System.Text;
using Microsoft.PowerShell.Commands.Internal;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Defines the implementation of the 'Set-Clipboard' cmdlet.
/// This cmdlet gets the content from system clipboard.
/// </summary>
[Cmdlet(VerbsCommon.Set, "Clipboard", SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=2109826")]
[Alias("scb")]
public class SetClipboardCommand : PSCmdlet
{
private List<string> _contentList = new List<string>();
/// <summary>
/// Property that sets clipboard content.
/// </summary>
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[System.Management.Automation.AllowNull]
[AllowEmptyCollection]
[AllowEmptyString]
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
public string[] Value { get; set; }
/// <summary>
/// Property that sets append parameter. This will allow to append clipboard without clear it.
/// </summary>
[Parameter]
public SwitchParameter Append { get; set; }
/// <summary>
/// This method implements the BeginProcessing method for Set-Clipboard command.
/// </summary>
protected override void BeginProcessing()
{
_contentList.Clear();
}
/// <summary>
/// This method implements the ProcessRecord method for Set-Clipboard command.
/// </summary>
protected override void ProcessRecord()
{
if (Value != null)
{
_contentList.AddRange(Value);
}
}
/// <summary>
/// This method implements the EndProcessing method for Set-Clipboard command.
/// </summary>
protected override void EndProcessing()
{
SetClipboardContent(_contentList, Append);
}
/// <summary>
/// Set the clipboard content.
/// </summary>
/// <param name="contentList">The content to store into the clipboard.</param>
/// <param name="append">If true, appends to clipboard instead of overwriting.</param>
private void SetClipboardContent(List<string> contentList, bool append)
{
string setClipboardShouldProcessTarget;
if ((contentList == null || contentList.Count == 0) && !append)
{
setClipboardShouldProcessTarget = string.Format(CultureInfo.InvariantCulture, ClipboardResources.ClipboardCleared);
if (ShouldProcess(setClipboardShouldProcessTarget, "Set-Clipboard"))
{
Clipboard.SetText(string.Empty);
}
return;
}
StringBuilder content = new StringBuilder();
if (append)
{
content.AppendLine(Clipboard.GetText());
}
if (contentList != null)
{
content.Append(string.Join(Environment.NewLine, contentList.ToArray(), 0, contentList.Count));
}
string verboseString = null;
if (contentList != null)
{
verboseString = contentList[0];
if (verboseString.Length >= 20)
{
verboseString = verboseString.Substring(0, 20);
verboseString = verboseString + " ...";
}
}
if (append)
{
setClipboardShouldProcessTarget = string.Format(CultureInfo.InvariantCulture, ClipboardResources.AppendClipboardContent, verboseString);
}
else
{
setClipboardShouldProcessTarget = string.Format(CultureInfo.InvariantCulture, ClipboardResources.SetClipboardContent, verboseString);
}
if (ShouldProcess(setClipboardShouldProcessTarget, "Set-Clipboard"))
{
Clipboard.SetText(content.ToString());
}
}
}
}