Add copy selection support

This commit is contained in:
Leroy Hopson 2021-07-08 20:33:46 +07:00 committed by Leroy Hopson
parent 57c1c3524d
commit 0e6334db96
5 changed files with 18 additions and 1 deletions

View file

@ -289,6 +289,7 @@ void Terminal::_register_methods() {
register_method("start_selection", &Terminal::start_selection); register_method("start_selection", &Terminal::start_selection);
register_method("select_to_pointer", &Terminal::select_to_pointer); register_method("select_to_pointer", &Terminal::select_to_pointer);
register_method("reset_selection", &Terminal::reset_selection); register_method("reset_selection", &Terminal::reset_selection);
register_method("copy_selection", &Terminal::copy_selection);
register_method("_update_theme", &Terminal::update_theme); register_method("_update_theme", &Terminal::update_theme);
register_method("_update_size", &Terminal::update_theme); register_method("_update_size", &Terminal::update_theme);
@ -598,3 +599,11 @@ void Terminal::reset_selection() {
tsm_screen_selection_reset(screen); tsm_screen_selection_reset(screen);
update(); 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;
}

View file

@ -61,6 +61,7 @@ public:
void start_selection(Vector2 position); void start_selection(Vector2 position);
void select_to_pointer(Vector2 position); void select_to_pointer(Vector2 position);
void reset_selection(); void reset_selection();
String copy_selection();
enum UpdateMode { enum UpdateMode {
DISABLED, DISABLED,

View file

@ -31,6 +31,9 @@ export (UpdateMode) var update_mode = UpdateMode.AUTO setget set_update_mode
var cols = 2 var cols = 2
var rows = 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 _viewport: Viewport = preload("./viewport.tscn").instance()
var _native_terminal: Control = _viewport.get_node("Terminal") var _native_terminal: Control = _viewport.get_node("Terminal")
var _screen := TextureRect.new() var _screen := TextureRect.new()
@ -167,6 +170,9 @@ func _handle_selection(event: InputEventMouse):
func _on_selection_held() -> void: func _on_selection_held() -> void:
if not Input.is_mouse_button_pressed(BUTTON_LEFT) or _selecting_mode == SelectionMode.NONE: 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() _selection_timer.stop()
return return

View file

@ -1,4 +1,4 @@
extends GDXterm.Terminal extends "res://addons/godot_xterm/nodes/terminal/terminal.gd"
onready var pty = $PTY onready var pty = $PTY

View file

@ -11,6 +11,7 @@ script = ExtResource( 3 )
__meta__ = { __meta__ = {
"_edit_use_anchors_": false "_edit_use_anchors_": false
} }
copy_on_selection = true
[node name="PTY" type="Node" parent="."] [node name="PTY" type="Node" parent="."]
script = ExtResource( 2 ) script = ExtResource( 2 )