diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d60d5f76d..bd46bfca21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). + +## Unreleased + +### Added + +- Added App.copy_to_clipboard + ## [0.56.4] - 2024-04-09 ### Fixed diff --git a/src/textual/app.py b/src/textual/app.py index 48ed9ea66b..7ed7275d0a 100644 --- a/src/textual/app.py +++ b/src/textual/app.py @@ -1128,6 +1128,24 @@ def get_loading_widget(self) -> Widget: return LoadingIndicator() + def copy_to_clipboard(self, text: str) -> None: + """Copy text to the clipboard. + + !!! note + + This does not work on macOS Terminal, but will work on most other terminals. + + Args: + text: Text you wish to copy to the clipboard. + """ + if self._driver is None: + return + + import base64 + + base64_text = base64.b64encode(text.encode("utf-8")).decode("utf-8") + self._driver.write(f"\x1b]52;c;{base64_text}\a") + def call_from_thread( self, callback: Callable[..., CallThreadReturnType | Awaitable[CallThreadReturnType]],