Class Cursor

java.lang.Object
org.jline.terminal.Cursor

public class Cursor extends Object
Represents the position of the cursor within a terminal.

The Cursor class encapsulates the coordinates of the cursor in a terminal, providing access to its X (column) and Y (row) position. Cursor positions are used for various terminal operations such as text insertion, deletion, and formatting.

In terminal coordinates:

  • X coordinate - Represents the column position (horizontal), typically 0-based
  • Y coordinate - Represents the row position (vertical), typically 0-based

Cursor objects are typically obtained from a Terminal using the Terminal.getCursorPosition(java.util.function.IntConsumer) method, which queries the terminal for its current cursor position. This information can be used to determine where text will be inserted or to calculate relative positions for cursor movement.

Example usage:

 Terminal terminal = TerminalBuilder.terminal();

 // Get current cursor position
 Cursor cursor = terminal.getCursorPosition(c -> {});
 if (cursor != null) {
     System.out.println("Cursor position: column=" + cursor.getX() + ", row=" + cursor.getY());
 }
 

Note that not all terminals support cursor position reporting. The Terminal.getCursorPosition(java.util.function.IntConsumer) method may return null if cursor position reporting is not supported.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Cursor(int x, int y)
    Creates a new Cursor instance at the specified coordinates.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    Compares this Cursor object with another object for equality.
    int
    Returns the column position (horizontal coordinate) of this cursor.
    int
    Returns the row position (vertical coordinate) of this cursor.
    int
    Returns a hash code for this Cursor object.
    Returns a string representation of this Cursor object.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • Cursor

      public Cursor(int x, int y)
      Creates a new Cursor instance at the specified coordinates.

      This constructor creates a Cursor object representing a position in the terminal at the given column (x) and row (y) coordinates. In terminal coordinates, the origin (0,0) is typically at the top-left corner of the screen.

      Parameters:
      x - the column position (horizontal coordinate)
      y - the row position (vertical coordinate)
  • Method Details

    • getX

      public int getX()
      Returns the column position (horizontal coordinate) of this cursor.

      The X coordinate represents the horizontal position of the cursor in the terminal, measured in character cells from the left edge of the terminal. The leftmost column is typically position 0.

      Returns:
      the column position (X coordinate)
    • getY

      public int getY()
      Returns the row position (vertical coordinate) of this cursor.

      The Y coordinate represents the vertical position of the cursor in the terminal, measured in character cells from the top edge of the terminal. The topmost row is typically position 0.

      Returns:
      the row position (Y coordinate)
    • equals

      public boolean equals(Object o)
      Compares this Cursor object with another object for equality.

      Two Cursor objects are considered equal if they have the same X and Y coordinates.

      Overrides:
      equals in class Object
      Parameters:
      o - the object to compare with
      Returns:
      true if the objects are equal, false otherwise
    • hashCode

      public int hashCode()
      Returns a hash code for this Cursor object.

      The hash code is computed based on the X and Y coordinates.

      Overrides:
      hashCode in class Object
      Returns:
      a hash code value for this object
    • toString

      public String toString()
      Returns a string representation of this Cursor object.

      The string representation includes the X and Y coordinates.

      Overrides:
      toString in class Object
      Returns:
      a string representation of this object