Skip to content

Commit

Permalink
Don't fail writing text outside screen
Browse files Browse the repository at this point in the history
- Adding np check not to try using item outside of a screen
- This fix essentially discards text write beyond screen bounds
- Fixes #993
  • Loading branch information
jvalkeal committed Jan 29, 2024
1 parent 9830faf commit 3d52f17
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -300,12 +300,14 @@ public void text(String text, int x, int y) {
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;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -24,6 +24,7 @@
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 {
Expand Down Expand Up @@ -105,4 +106,22 @@ void writeTextFromWriterLayerOverrides() {
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();
}

}

0 comments on commit 3d52f17

Please sign in to comment.