Skip to content

Commit

Permalink
fix(term): copy_selection() when nothing selected
Browse files Browse the repository at this point in the history
Fixes the copy_selection() method so that it returns and empty string
when called and nothing is selected. Previously printed an error.
  • Loading branch information
lihop committed Apr 28, 2024
1 parent 3ca272c commit 43303a5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
8 changes: 5 additions & 3 deletions addons/godot_xterm/native/src/terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,11 @@ String Terminal::_copy_screen(ScreenCopyFunction func) {
char *out;
PackedByteArray data;

data.resize(func(screen, &out));
memcpy(data.ptrw(), out, data.size());
std::free(out);
data.resize(std::max(func(screen, &out), 0));
if (data.size() > 0) {
memcpy(data.ptrw(), out, data.size());
std::free(out);
}

return data.get_string_from_utf8();
}
Expand Down
3 changes: 3 additions & 0 deletions test/test_terminal.gd
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ class TestCopy:
subject.write(text)
assert_string_contains(subject.copy_all(), text)

func test_copy_selection_when_nothing_selected():
assert_eq(subject.copy_selection(), "")


class TestClear:
extends TerminalTest
Expand Down

0 comments on commit 43303a5

Please sign in to comment.