Skip to content

Commit a9ccde5

Browse files
committed
copy empty text: clear the clipboard instead of error (v0.2)
Apparently Clipboard.SetText("") throws an exception, which affected -c/-i/-I. Now it instead clears the clipboard, which is semantically consistent with setting the clipboard text to an empty string.
1 parent d182661 commit a9ccde5

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

uclip.cs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,23 @@ static void Main(string[] args) {
5757
" uclip -O Write clipboard text to standard output as UTF-16LE\n"+
5858
" uclip -oe | -Oe Like -o/-O but error if text is empty or unavailable\n"+
5959
" uclip -h Print this help and exit\n"+
60-
"Version 0.1, https://github.com/avih/uclip\n");
60+
"Version 0.2, https://github.com/avih/uclip\n");
6161

6262
} else if (o == "-c" && alen == 2) {
63-
Clipboard.SetText(args[1]);
63+
if (args[1] == "")
64+
Clipboard.Clear();
65+
else
66+
Clipboard.SetText(args[1]);
6467

6568
} else if ((o == "-i" || o == "-I") && alen == 1) {
6669
byte[] bytes = read_stream(Console.OpenStandardInput());
6770
Encoding e = o == "-i" ? Encoding.UTF8 : Encoding.Unicode;
68-
Clipboard.SetText(new string(e.GetChars(bytes, 0, bytes.Length)));
71+
string s = new string(e.GetChars(bytes));
72+
73+
if (s == "")
74+
Clipboard.Clear();
75+
else
76+
Clipboard.SetText(s);
6977

7078
} else if ((o == "-o" || o == "-O" || o == "-oe" || o == "-Oe") && alen == 1) {
7179
bool do_err = o.Length > 2, do_utf8 = o[1] == 'o';
@@ -98,7 +106,10 @@ class Program {
98106
[STAThreadAttribute]
99107
static int Main(string[] args) {
100108
if (args.Length == 2 && args[0] == "-c") {
101-
Clipboard.SetText(args[1]);
109+
if (args[1] == "")
110+
Clipboard.Clear();
111+
else
112+
Clipboard.SetText(args[1]);
102113
return 0;
103114
}
104115
if (args.Length == 1 && args[0] == "-o") {

0 commit comments

Comments
 (0)