fix(term): copy_selection() when nothing selected

Fixes the copy_selection() method so that it returns and empty string
when called and nothing is selected. Previously printed an error.
This commit is contained in:
Leroy Hopson 2024-04-28 15:25:26 +12:00 committed by Leroy Hopson
parent 3ca272c615
commit 43303a51bf
2 changed files with 8 additions and 3 deletions

View file

@ -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();
}

View file

@ -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