Skip to content

Commit

Permalink
Screen writer support for AttributedString
Browse files Browse the repository at this point in the history
- New Screen.Writer has new text method taking
  AttributedString instead of just String
- Fixes #1110
  • Loading branch information
jvalkeal committed Jul 28, 2024
1 parent 417789b commit 02011c8
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
import org.springframework.shell.geom.Position;
import org.springframework.shell.geom.Rectangle;
import org.springframework.shell.geom.VerticalAlign;
import org.springframework.shell.style.ThemeResolver;
import org.springframework.shell.style.ThemeResolver.ResolvedValues;
import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -312,6 +314,29 @@ public void text(String text, int x, int y) {
}
}

@Override
public void text(AttributedString text, int x, int y) {
Layer layer = getLayer(index);
for (int i = 0; i < text.length() && i < columns; i++) {
DefaultScreenItem item = layer.getScreenItem(x + i, y);
if (item != null) {
char c = text.charAt(i);
AttributedStyle as = text.styleAt(i);
ResolvedValues rv = ThemeResolver.resolveValues(as);
item.content = Character.toString(c);
if (rv.foreground() > -1) {
item.foreground = rv.foreground();
}
if (rv.style() > -1) {
item.style = rv.style();
}
if (rv.background() > -1) {
item.background = rv.background();
}
}
}
}

@Override
public void border(int x, int y, int width, int height) {
log.trace("PrintBorder rows={}, columns={}, x={}, y={}, width={}, height={}", rows, columns, x, y, width,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package org.springframework.shell.component.view.screen;

import org.jline.utils.AttributedString;

import org.springframework.lang.Nullable;
import org.springframework.shell.geom.HorizontalAlign;
import org.springframework.shell.geom.Position;
Expand Down Expand Up @@ -107,6 +109,16 @@ interface Writer {
*/
void text(String text, int x, int y);

/**
* Write an attributed text horizontally starting from a position defined by
* {@code x} and {@code y} within a bounds of a {@link Screen}.
*
* @param text the text to write
* @param x the x position
* @param y the y position
*/
void text(AttributedString text, int x, int y);

/**
* Write a border with a given rectangle coordinates.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public record ResolvedValues(int style, int foreground, int background){}
* @param attributedStyle the attibuted style
* @return resolved values
*/
public ResolvedValues resolveValues(AttributedStyle attributedStyle) {
public static ResolvedValues resolveValues(AttributedStyle attributedStyle) {
long style = attributedStyle.getStyle();
long s = style & ~(F_FOREGROUND | F_BACKGROUND);
s = (s & 0x00007FFF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.springframework.shell.component.view.screen;

import org.jline.utils.AttributedString;
import org.junit.jupiter.api.Test;

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

@Test
void printsAttributedText() {
AttributedString text = new AttributedString("text");
screen24x80.writerBuilder().build().text(text, 0, 0);
assertThat(forScreen(screen24x80)).hasHorizontalText("text", 0, 0, 4);
}

@Test
void printsTextWithForegroundColor() {
Writer writer = screen24x80.writerBuilder().color(Color.RED).build();
Expand Down

0 comments on commit 02011c8

Please sign in to comment.