-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathclipboard.dart
More file actions
executable file
·84 lines (70 loc) · 1.87 KB
/
clipboard.dart
File metadata and controls
executable file
·84 lines (70 loc) · 1.87 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
part of console;
Clipboard? getClipboard() {
if (Platform.isMacOS) return OSXClipboard();
if (Platform.isLinux && File('/usr/bin/xclip').existsSync()) {
return XClipboard();
}
if (Platform.isWindows) return WinClipboard();
return null;
}
abstract class Clipboard {
String getContent();
void setContent(String content);
}
class OSXClipboard implements Clipboard {
@override
String getContent() {
var result = Process.runSync('/usr/bin/pbpaste', []);
if (result.exitCode != 0) {
throw Exception('Failed to get clipboard content.');
}
return result.stdout.toString();
}
@override
void setContent(String content) {
Process.start('/usr/bin/pbpaste', []).then((process) {
process.stdin.write(content);
process.stdin.close();
});
}
}
class XClipboard implements Clipboard {
@override
String getContent() {
var result =
Process.runSync('/usr/bin/xclip', ['-selection', 'clipboard', '-o']);
if (result.exitCode != 0) {
throw Exception('Failed to get clipboard content.');
}
return result.stdout.toString();
}
@override
void setContent(String content) {
Process.start('/usr/bin/xclip', ['-selection', 'clipboard'])
.then((process) {
process.stdin.write(content);
process.stdin.close();
});
}
}
class WinClipboard implements Clipboard {
@override
String getContent() {
OpenClipboard(NULL);
if (IsClipboardFormatAvailable(CF_TEXT) == FALSE) return '';
var hg = GetClipboardData(CF_TEXT);
if (hg == NULL) return '';
var ptstr = GlobalLock(hg);
GlobalUnlock(hg);
CloseClipboard();
return ptstr.cast<Utf8>().toDartString();
}
@override
void setContent(String content) {
var ptstr = content.toNativeUtf8();
OpenClipboard(NULL);
EmptyClipboard();
SetClipboardData(CF_TEXT, ptstr.address);
CloseClipboard();
}
}