Skip to content

Commit 02011c8

Browse files
committed
Screen writer support for AttributedString
- New Screen.Writer has new text method taking AttributedString instead of just String - Fixes #1110
1 parent 417789b commit 02011c8

File tree

4 files changed

+46
-1
lines changed

4 files changed

+46
-1
lines changed

spring-shell-core/src/main/java/org/springframework/shell/component/view/screen/DefaultScreen.java

+25
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
import org.springframework.shell.geom.Position;
3131
import org.springframework.shell.geom.Rectangle;
3232
import org.springframework.shell.geom.VerticalAlign;
33+
import org.springframework.shell.style.ThemeResolver;
34+
import org.springframework.shell.style.ThemeResolver.ResolvedValues;
3335
import org.springframework.util.Assert;
3436

3537
/**
@@ -312,6 +314,29 @@ public void text(String text, int x, int y) {
312314
}
313315
}
314316

317+
@Override
318+
public void text(AttributedString text, int x, int y) {
319+
Layer layer = getLayer(index);
320+
for (int i = 0; i < text.length() && i < columns; i++) {
321+
DefaultScreenItem item = layer.getScreenItem(x + i, y);
322+
if (item != null) {
323+
char c = text.charAt(i);
324+
AttributedStyle as = text.styleAt(i);
325+
ResolvedValues rv = ThemeResolver.resolveValues(as);
326+
item.content = Character.toString(c);
327+
if (rv.foreground() > -1) {
328+
item.foreground = rv.foreground();
329+
}
330+
if (rv.style() > -1) {
331+
item.style = rv.style();
332+
}
333+
if (rv.background() > -1) {
334+
item.background = rv.background();
335+
}
336+
}
337+
}
338+
}
339+
315340
@Override
316341
public void border(int x, int y, int width, int height) {
317342
log.trace("PrintBorder rows={}, columns={}, x={}, y={}, width={}, height={}", rows, columns, x, y, width,

spring-shell-core/src/main/java/org/springframework/shell/component/view/screen/Screen.java

+12
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
*/
1616
package org.springframework.shell.component.view.screen;
1717

18+
import org.jline.utils.AttributedString;
19+
1820
import org.springframework.lang.Nullable;
1921
import org.springframework.shell.geom.HorizontalAlign;
2022
import org.springframework.shell.geom.Position;
@@ -107,6 +109,16 @@ interface Writer {
107109
*/
108110
void text(String text, int x, int y);
109111

112+
/**
113+
* Write an attributed text horizontally starting from a position defined by
114+
* {@code x} and {@code y} within a bounds of a {@link Screen}.
115+
*
116+
* @param text the text to write
117+
* @param x the x position
118+
* @param y the y position
119+
*/
120+
void text(AttributedString text, int x, int y);
121+
110122
/**
111123
* Write a border with a given rectangle coordinates.
112124
*

spring-shell-core/src/main/java/org/springframework/shell/style/ThemeResolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public record ResolvedValues(int style, int foreground, int background){}
7070
* @param attributedStyle the attibuted style
7171
* @return resolved values
7272
*/
73-
public ResolvedValues resolveValues(AttributedStyle attributedStyle) {
73+
public static ResolvedValues resolveValues(AttributedStyle attributedStyle) {
7474
long style = attributedStyle.getStyle();
7575
long s = style & ~(F_FOREGROUND | F_BACKGROUND);
7676
s = (s & 0x00007FFF);

spring-shell-core/src/test/java/org/springframework/shell/component/view/screen/ScreenTests.java

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.springframework.shell.component.view.screen;
1717

18+
import org.jline.utils.AttributedString;
1819
import org.junit.jupiter.api.Test;
1920

2021
import org.springframework.shell.component.view.control.AbstractViewTests;
@@ -56,6 +57,13 @@ void printsText() {
5657
assertThat(forScreen(screen24x80)).hasHorizontalText("text", 0, 0, 4);
5758
}
5859

60+
@Test
61+
void printsAttributedText() {
62+
AttributedString text = new AttributedString("text");
63+
screen24x80.writerBuilder().build().text(text, 0, 0);
64+
assertThat(forScreen(screen24x80)).hasHorizontalText("text", 0, 0, 4);
65+
}
66+
5967
@Test
6068
void printsTextWithForegroundColor() {
6169
Writer writer = screen24x80.writerBuilder().color(Color.RED).build();

0 commit comments

Comments
 (0)