diff --git a/spring-shell-core/src/main/java/org/springframework/shell/component/view/screen/DefaultScreen.java b/spring-shell-core/src/main/java/org/springframework/shell/component/view/screen/DefaultScreen.java index 4e3c9a9e..483dd141 100644 --- a/spring-shell-core/src/main/java/org/springframework/shell/component/view/screen/DefaultScreen.java +++ b/spring-shell-core/src/main/java/org/springframework/shell/component/view/screen/DefaultScreen.java @@ -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; + } } } } diff --git a/spring-shell-core/src/test/java/org/springframework/shell/component/view/screen/ScreenTests.java b/spring-shell-core/src/test/java/org/springframework/shell/component/view/screen/ScreenTests.java index 62b56e43..cc0a4e6c 100644 --- a/spring-shell-core/src/test/java/org/springframework/shell/component/view/screen/ScreenTests.java +++ b/spring-shell-core/src/test/java/org/springframework/shell/component/view/screen/ScreenTests.java @@ -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(); + } + }