Don't fail writing text outside screen

- Adding np check not to try using item outside of a screen
- This fix essentially discards text write beyond screen bounds
- Fixes #993
This commit is contained in:
Janne Valkealahti
2024-01-29 09:42:57 +00:00
parent 9830fafe1a
commit 3d52f174af
2 changed files with 29 additions and 8 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2023 the original author or authors.
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -300,12 +300,14 @@ public class DefaultScreen implements Screen, DisplayLines {
for (int i = 0; i < text.length() && i < columns; i++) {
char c = text.charAt(i);
DefaultScreenItem item = layer.getScreenItem(x + i, y);
item.content = Character.toString(c);
if (color > -1) {
item.foreground = color;
}
if (style > -1) {
item.style = style;
if (item != null) {
item.content = Character.toString(c);
if (color > -1) {
item.foreground = color;
}
if (style > -1) {
item.style = style;
}
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2023 the original author or authors.
* Copyright 2023-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@ import org.springframework.shell.geom.Rectangle;
import org.springframework.shell.geom.VerticalAlign;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
class ScreenTests extends AbstractViewTests {
@@ -105,4 +106,22 @@ class ScreenTests extends AbstractViewTests {
assertThat(items[0][3].getContent()).isEqualTo("t");
}
@Test
void discardAndDoNotErrorTextWriteOutsideScreen() {
assertThatCode(() -> {
screen24x80.writerBuilder().build().text("text", 79, 0);
screen24x80.writerBuilder().build().text("text", 80, 0);
screen24x80.writerBuilder().build().text("text", 0, 24);
}).doesNotThrowAnyException();
}
@Test
void discardAndDoNotErrorBorderWriteOutsideScreen() {
assertThatCode(() -> {
screen24x80.writerBuilder().build().border(0, 0, 81, 24);
screen24x80.writerBuilder().build().border(0, 0, 80, 25);
screen24x80.writerBuilder().build().border(0, 0, 81, 25);
}).doesNotThrowAnyException();
}
}