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:
@@ -6,6 +6,8 @@ import java.io.InputStreamReader;
|
|||||||
import java.io.PrintStream;
|
import java.io.PrintStream;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import thb.jeanluc.adventure.io.text.StyledText;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link GameIO} backed by {@code System.in} and {@code System.out}.
|
* {@link GameIO} backed by {@code System.in} and {@code System.out}.
|
||||||
* Each {@code write} appends a newline so output blocks are visually
|
* Each {@code write} appends a newline so output blocks are visually
|
||||||
@@ -51,7 +53,7 @@ public class ConsoleIO implements GameIO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(String s) {
|
public void print(StyledText text) {
|
||||||
out.println(s);
|
out.println(text.plainText());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,24 +1,35 @@
|
|||||||
package thb.jeanluc.adventure.io;
|
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
|
* Bidirectional channel between the engine and the player. Commands emit
|
||||||
* by both {@code ConsoleIO} and {@code SwingIO} so the game loop is agnostic
|
* semantic, role-styled output via {@link #print(StyledText)} (the primitive)
|
||||||
* to the actual interface.
|
* and the {@link #showRoom}/{@link #setHud} views; each frontend renders them.
|
||||||
*/
|
*/
|
||||||
public interface GameIO {
|
public interface GameIO {
|
||||||
|
|
||||||
/**
|
/** Blocks until a line of input is available; empty string at end of input. */
|
||||||
* Blocks until a line of input is available.
|
|
||||||
*
|
|
||||||
* @return the next user input line, or an empty string at end of input
|
|
||||||
*/
|
|
||||||
String readLine();
|
String readLine();
|
||||||
|
|
||||||
/**
|
/** Renders one styled output block. The only method implementors must provide. */
|
||||||
* Writes a message to the player. Each call corresponds to one logical
|
void print(StyledText text);
|
||||||
* output block; the implementation handles line breaks at the boundary.
|
|
||||||
*
|
/** Convenience: a plain, unstyled line. */
|
||||||
* @param s message text; must not be null
|
default void write(String s) {
|
||||||
*/
|
print(StyledText.of(s));
|
||||||
void write(String 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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ import java.awt.Color;
|
|||||||
import java.awt.Font;
|
import java.awt.Font;
|
||||||
import java.util.concurrent.LinkedBlockingQueue;
|
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
|
* {@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
|
* game text; a bottom text field collects player input. The input from
|
||||||
@@ -79,7 +81,7 @@ public class SwingIO implements GameIO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(String s) {
|
public void print(StyledText text) {
|
||||||
SwingUtilities.invokeLater(() -> output.append(s + "\n"));
|
SwingUtilities.invokeLater(() -> output.append(text.plainText() + "\n"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public class TestIO implements GameIO {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void write(String s) {
|
public void print(thb.jeanluc.adventure.io.text.StyledText text) {
|
||||||
outputs.add(s);
|
outputs.add(text.plainText());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user