@@ -237,6 +237,7 @@ class Screen(Generic[ScreenResultType], Widget):
237
237
BINDINGS = [
238
238
Binding ("tab" , "app.focus_next" , "Focus Next" , show = False ),
239
239
Binding ("shift+tab" , "app.focus_previous" , "Focus Previous" , show = False ),
240
+ Binding ("ctrl+c" , "screen.copy_text" , "Copy selected text" , show = False ),
240
241
]
241
242
242
243
def __init__ (
@@ -833,6 +834,30 @@ def minimize(self) -> None:
833
834
self .scroll_to_widget , self .focused , animate = False , center = True
834
835
)
835
836
837
+ def get_selected_text (self ) -> str | None :
838
+ """Get text under selection.
839
+
840
+ Returns:
841
+ Selected text, or `None` if no text was selected.
842
+ """
843
+ if not self .selections :
844
+ return None
845
+
846
+ widget_text : list [str ] = []
847
+ for widget , selection in self .selections .items ():
848
+ selected_text_in_widget = widget .get_selection (selection )
849
+ if selected_text_in_widget is not None :
850
+ widget_text .append (selected_text_in_widget )
851
+
852
+ selected_text = "\n " .join (widget_text )
853
+ return selected_text
854
+
855
+ def action_copy_text (self ) -> None :
856
+ """Copy selected text to clipboard."""
857
+ selection = self .get_selected_text ()
858
+ if selection is not None :
859
+ self .app .copy_to_clipboard (selection )
860
+
836
861
def action_maximize (self ) -> None :
837
862
"""Action to maximize the currently focused widget."""
838
863
if self .focused is not None :
0 commit comments