From 0e6334db962ffd7389fdb6e06f81dc2c8779882b Mon Sep 17 00:00:00 2001 From: Leroy Hopson Date: Thu, 8 Jul 2021 20:33:46 +0700 Subject: [PATCH] Add copy selection support --- addons/godot_xterm/native/src/terminal.cpp | 9 +++++++++ addons/godot_xterm/native/src/terminal.h | 1 + addons/godot_xterm/nodes/terminal/terminal.gd | 6 ++++++ examples/terminal/terminal.gd | 2 +- examples/terminal/terminal.tscn | 1 + 5 files changed, 18 insertions(+), 1 deletion(-) diff --git a/addons/godot_xterm/native/src/terminal.cpp b/addons/godot_xterm/native/src/terminal.cpp index ba90e85..7bc0f5a 100644 --- a/addons/godot_xterm/native/src/terminal.cpp +++ b/addons/godot_xterm/native/src/terminal.cpp @@ -289,6 +289,7 @@ void Terminal::_register_methods() { register_method("start_selection", &Terminal::start_selection); register_method("select_to_pointer", &Terminal::select_to_pointer); register_method("reset_selection", &Terminal::reset_selection); + register_method("copy_selection", &Terminal::copy_selection); register_method("_update_theme", &Terminal::update_theme); register_method("_update_size", &Terminal::update_theme); @@ -597,4 +598,12 @@ void Terminal::select_to_pointer(Vector2 position) { void Terminal::reset_selection() { tsm_screen_selection_reset(screen); update(); +} + +String Terminal::copy_selection() { + char *out = nullptr; + int len = tsm_screen_selection_copy(screen, &out); + String result = String(out); + std::free(out); + return result; } \ No newline at end of file diff --git a/addons/godot_xterm/native/src/terminal.h b/addons/godot_xterm/native/src/terminal.h index c5ea690..edaaa8d 100644 --- a/addons/godot_xterm/native/src/terminal.h +++ b/addons/godot_xterm/native/src/terminal.h @@ -61,6 +61,7 @@ public: void start_selection(Vector2 position); void select_to_pointer(Vector2 position); void reset_selection(); + String copy_selection(); enum UpdateMode { DISABLED, diff --git a/addons/godot_xterm/nodes/terminal/terminal.gd b/addons/godot_xterm/nodes/terminal/terminal.gd index 8c3affb..2506436 100644 --- a/addons/godot_xterm/nodes/terminal/terminal.gd +++ b/addons/godot_xterm/nodes/terminal/terminal.gd @@ -31,6 +31,9 @@ export (UpdateMode) var update_mode = UpdateMode.AUTO setget set_update_mode var cols = 2 var rows = 2 +# If true, text in the terminal will be copied to the clipboard when selected. +export (bool) var copy_on_selection + var _viewport: Viewport = preload("./viewport.tscn").instance() var _native_terminal: Control = _viewport.get_node("Terminal") var _screen := TextureRect.new() @@ -167,6 +170,9 @@ func _handle_selection(event: InputEventMouse): func _on_selection_held() -> void: if not Input.is_mouse_button_pressed(BUTTON_LEFT) or _selecting_mode == SelectionMode.NONE: + if copy_on_selection: + var selection = _native_terminal.copy_selection() + OS.set_clipboard(selection) _selection_timer.stop() return diff --git a/examples/terminal/terminal.gd b/examples/terminal/terminal.gd index 5e6209b..0910760 100644 --- a/examples/terminal/terminal.gd +++ b/examples/terminal/terminal.gd @@ -1,4 +1,4 @@ -extends GDXterm.Terminal +extends "res://addons/godot_xterm/nodes/terminal/terminal.gd" onready var pty = $PTY diff --git a/examples/terminal/terminal.tscn b/examples/terminal/terminal.tscn index 1bcaa11..c9da576 100644 --- a/examples/terminal/terminal.tscn +++ b/examples/terminal/terminal.tscn @@ -11,6 +11,7 @@ script = ExtResource( 3 ) __meta__ = { "_edit_use_anchors_": false } +copy_on_selection = true [node name="PTY" type="Node" parent="."] script = ExtResource( 2 )