feat(io): make print(StyledText) the GameIO primitive; keep suite green

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-31 20:16:55 +02:00
parent 02e448d61a
commit 44fde78c64
4 changed files with 36 additions and 21 deletions

View File

@@ -6,6 +6,8 @@ import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import thb.jeanluc.adventure.io.text.StyledText;
/**
* {@link GameIO} backed by {@code System.in} and {@code System.out}.
* Each {@code write} appends a newline so output blocks are visually
@@ -51,7 +53,7 @@ public class ConsoleIO implements GameIO {
}
@Override
public void write(String s) {
out.println(s);
public void print(StyledText text) {
out.println(text.plainText());
}
}

View File

@@ -1,24 +1,35 @@
package thb.jeanluc.adventure.io;
import thb.jeanluc.adventure.io.text.Hud;
import thb.jeanluc.adventure.io.text.Renderings;
import thb.jeanluc.adventure.io.text.RoomView;
import thb.jeanluc.adventure.io.text.StyledText;
/**
* Bidirectional text channel between the engine and the player. Implemented
* by both {@code ConsoleIO} and {@code SwingIO} so the game loop is agnostic
* to the actual interface.
* Bidirectional channel between the engine and the player. Commands emit
* semantic, role-styled output via {@link #print(StyledText)} (the primitive)
* and the {@link #showRoom}/{@link #setHud} views; each frontend renders them.
*/
public interface GameIO {
/**
* Blocks until a line of input is available.
*
* @return the next user input line, or an empty string at end of input
*/
/** Blocks until a line of input is available; empty string at end of input. */
String readLine();
/**
* Writes a message to the player. Each call corresponds to one logical
* output block; the implementation handles line breaks at the boundary.
*
* @param s message text; must not be null
*/
void write(String s);
/** Renders one styled output block. The only method implementors must provide. */
void print(StyledText text);
/** Convenience: a plain, unstyled line. */
default void write(String s) {
print(StyledText.of(s));
}
/** Renders a room. Default flattens via {@link Renderings}; renderers may override. */
default void showRoom(RoomView room) {
print(Renderings.roomToStyledText(room));
}
/** Updates the status/HUD region. No-op by default; renderers may override. */
default void setHud(Hud hud) {
// no-op
}
}

View File

@@ -11,6 +11,8 @@ import java.awt.Color;
import java.awt.Font;
import java.util.concurrent.LinkedBlockingQueue;
import thb.jeanluc.adventure.io.text.StyledText;
/**
* {@link GameIO} backed by a Swing window. A read-only output area shows
* game text; a bottom text field collects player input. The input from
@@ -79,7 +81,7 @@ public class SwingIO implements GameIO {
}
@Override
public void write(String s) {
SwingUtilities.invokeLater(() -> output.append(s + "\n"));
public void print(StyledText text) {
SwingUtilities.invokeLater(() -> output.append(text.plainText() + "\n"));
}
}

View File

@@ -36,7 +36,7 @@ public class TestIO implements GameIO {
}
@Override
public void write(String s) {
outputs.add(s);
public void print(thb.jeanluc.adventure.io.text.StyledText text) {
outputs.add(text.plainText());
}
}