diff --git a/example/README.md b/example/README.md index e525b34..597c821 100644 --- a/example/README.md +++ b/example/README.md @@ -8,6 +8,7 @@ This package comes with a number of examples to demonstrate various features. | `bar.dart` | Simple demonstration of DrawingCanvas for drawing a vertical bar. | | `canvas.dart` | Shows use of ConsoleCanvas for character-based positioning. | | `choice.dart` | Select a multiple choice option. | +| `clipboard.dart` | Simple example of clipboard operation. | | `clock.dart` | Sophisticated example of using DrawingCanvas to display a clock. | | `colors.dart` | Setting colored output with the TextPen class. | | `cube.dart` | Draws a 3D cube with DrawingCanvas. | diff --git a/example/clipboard.dart b/example/clipboard.dart new file mode 100644 index 0000000..0556ff3 --- /dev/null +++ b/example/clipboard.dart @@ -0,0 +1,18 @@ +import 'package:console/console.dart'; + +import 'dart:io'; + +void main() { + var clip = getClipboard(); + if (clip == null) { + throw Exception('${Platform.operatingSystem} is not supported.'); + } + + var prev = clip.getContent(); + print('Previous clipboard: $prev'); + + clip.setContent('Hello!'); + + var cur = clip.getContent(); + print('Current clipboard: $cur'); +} diff --git a/lib/console.dart b/lib/console.dart index 523e1a6..ec24d03 100644 --- a/lib/console.dart +++ b/lib/console.dart @@ -3,6 +3,8 @@ library console; import 'dart:async'; import 'dart:convert'; import 'dart:io'; +import 'package:ffi/ffi.dart'; +import 'package:win32/win32.dart'; part 'src/base.dart'; part 'src/clipboard.dart'; diff --git a/lib/src/clipboard.dart b/lib/src/clipboard.dart index fe1f1aa..e5a453b 100755 --- a/lib/src/clipboard.dart +++ b/lib/src/clipboard.dart @@ -5,6 +5,7 @@ Clipboard? getClipboard() { if (Platform.isLinux && File('/usr/bin/xclip').existsSync()) { return XClipboard(); } + if (Platform.isWindows) return WinClipboard(); return null; } @@ -53,3 +54,31 @@ class XClipboard implements Clipboard { }); } } + +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().toDartString(); + } + + @override + void setContent(String content) { + var ptstr = content.toNativeUtf8(); + + OpenClipboard(NULL); + EmptyClipboard(); + SetClipboardData(CF_TEXT, ptstr.address); + CloseClipboard(); + } +} diff --git a/pubspec.yaml b/pubspec.yaml index ce53fee..dbe39ff 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -8,7 +8,9 @@ environment: sdk: '>=2.12.0 <3.0.0' dependencies: + ffi: ^1.1.2 vector_math: ^2.1.0 + win32: ^2.2.5 dev_dependencies: test: ^1.16.8