Class TUI

java.lang.Object
it.polimi.ingsw.gc14.View.TUI.TUI
All Implemented Interfaces:
IView

public class TUI extends Object implements IView
Text-based User Interface (TUI) implementation of IView.

Single render view: board + menu on top, all players' hands in a responsive grid below. Hands wrap to a new row when their combined width would exceed the terminal width.

All output goes through display(String), which uses LineReader.printAbove(String) when a JLine reader is set so that the readline prompt is correctly redrawn after background-thread renders.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private org.jline.reader.LineReader
    JLine reader — when non-null, all output uses printAbove so the readline prompt is preserved after background-thread renders.
    private MiniModel
     
    private org.jline.terminal.Terminal
    JLine terminal — used to query terminal width for centering and grid layout.
    private String
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    TUI(MiniModel model)
    Constructs a TUI bound to the given model.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the board state as a multi-line string: turn order, upper cards, offer track, lower cards.
    private String
    Arranges every player's hand side-by-side, wrapping to a new grid row whenever the next panel would exceed the terminal width.
    private String
    Builds the main view: board + menu side by side, followed by all players' hands arranged in a responsive grid.
    private String
     
    private String
     
    private String
    center(String content)
    Horizontally centers each line of content within the terminal width.
    private void
    display(String content)
    Clears the terminal and displays content, horizontally centered.
    private void
    Prints text above the prompt without clearing the screen.
    void
    Default render: dispatches to the right view based on game stage.
    void
    Renders the board + menu (top) and all players' hands in a grid (bottom).
    void
    setLineReader(org.jline.reader.LineReader lineReader)
    Registers the active JLine LineReader.
    void
    Updates the model stored in this view.
    void
    setUsername(String username)
    Sets the username of the local player.
    void
    showError(ErrorType error, String message)
    Shows an error message combined with the current board in one display call, so only one printAbove is issued and the prompt is redrawn correctly.
    private int
    Returns the display width of a string in terminal columns, stripping ANSI escape codes.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • model

      private MiniModel model
    • username

      private String username
    • lineReader

      private org.jline.reader.LineReader lineReader
      JLine reader — when non-null, all output uses printAbove so the readline prompt is preserved after background-thread renders.
    • terminal

      private org.jline.terminal.Terminal terminal
      JLine terminal — used to query terminal width for centering and grid layout.
  • Constructor Details

    • TUI

      public TUI(MiniModel model)
      Constructs a TUI bound to the given model.
      Parameters:
      model - the model to display; may be null initially.
  • Method Details

    • setUsername

      public void setUsername(String username)
      Sets the username of the local player.
      Parameters:
      username - the username to assign.
    • setModel

      public void setModel(MiniModel model)
      Updates the model stored in this view.
      Specified by:
      setModel in interface IView
      Parameters:
      model - the latest mini model to display.
    • setLineReader

      public void setLineReader(org.jline.reader.LineReader lineReader)
      Registers the active JLine LineReader. Once set, all output routes through LineReader.printAbove(String) so the prompt survives background-thread renders.
      Parameters:
      lineReader - the JLine reader to use for output; also provides the terminal reference.
    • render

      public void render()
      Default render: dispatches to the right view based on game stage.
      Specified by:
      render in interface IView
    • renderBoard

      public void renderBoard()
      Renders the board + menu (top) and all players' hands in a grid (bottom).
    • showError

      public void showError(ErrorType error, String message)
      Shows an error message combined with the current board in one display call, so only one printAbove is issued and the prompt is redrawn correctly.
      Specified by:
      showError in interface IView
      Parameters:
      error - the error type.
      message - the human-readable message to append.
    • buildBoardContent

      private String buildBoardContent()
      Builds the main view: board + menu side by side, followed by all players' hands arranged in a responsive grid.
    • buildAllHandsContent

      private String buildAllHandsContent()
      Arranges every player's hand side-by-side, wrapping to a new grid row whenever the next panel would exceed the terminal width.
    • buildTotemsContent

      private String buildTotemsContent()
    • buildStandingContent

      private String buildStandingContent()
    • display

      private void display(String content)
      Clears the terminal and displays content, horizontally centered.

      When a JLine LineReader is registered, output is routed through LineReader.printAbove(String) which pauses readline, prints the content, and redraws the prompt — safe to call from any thread. Otherwise, ANSI codes are written directly to stdout.

    • printLine

      private void printLine(String text)
      Prints text above the prompt without clearing the screen.
    • center

      private String center(String content)
      Horizontally centers each line of content within the terminal width. Empty lines are left unpadded. Falls back to the original string when the terminal width is unknown.
    • visibleLength

      private int visibleLength(String line)
      Returns the display width of a string in terminal columns, stripping ANSI escape codes.
    • boardStamp

      public String boardStamp()
      Returns the board state as a multi-line string: turn order, upper cards, offer track, lower cards.
      Returns:
      multi-line board string.