Skip to main content

Screen Clearing and Cursor Control

JLine provides powerful capabilities for controlling the terminal display, including clearing the screen, moving the cursor, and updating specific portions of the display. This guide explains how to use these features to create more dynamic and interactive terminal applications.

Clearing the Entire Screen

The most basic screen operation is clearing the entire screen:

Loading snippet: ScreenClearingExample...

This example uses the clear_screen capability to erase all content from the terminal and position the cursor at the top-left corner.

Partial Screen Clearing

You can also clear only a portion of the screen:

Loading snippet: PartialScreenClearingExample...

In this example, we move the cursor to a specific position and then clear everything from that position to the end of the screen.

Clearing a Single Line

For more precise control, you can clear a single line:

Loading snippet: LineClearingExample...

This example moves the cursor to the beginning of a specific line and then clears that line before writing new content.

Cursor Movement

JLine provides several capabilities for moving the cursor:

Loading snippet: CursorMovementExample...

This example demonstrates moving the cursor to absolute positions, as well as saving and restoring the cursor position.

Raw Mode

For applications that need complete control over the terminal, JLine supports raw mode:

Loading snippet: RawModeExample...

In raw mode, input is not processed by the terminal driver, giving your application direct access to each keystroke.

Using the Display Class

For more complex screen management, JLine provides the Display class:

Loading snippet: DisplayExample...

The Display class manages a virtual screen buffer and efficiently updates only the parts of the screen that have changed, which can significantly improve performance for complex displays.

Terminal Capabilities

JLine uses the InfoCmp.Capability enum to access terminal capabilities. Here are some commonly used capabilities for screen and cursor control:

CapabilityDescription
clear_screenClear the entire screen
clr_eolClear from cursor to end of line
clr_eosClear from cursor to end of screen
cursor_addressMove cursor to absolute position
cursor_upMove cursor up one line
cursor_downMove cursor down one line
cursor_rightMove cursor right one column
cursor_leftMove cursor left one column
save_cursorSave current cursor position
restore_cursorRestore previously saved cursor position
enter_bold_modeStart bold text
exit_attribute_modeTurn off all attributes

Best Practices

When working with screen clearing and cursor control in JLine, keep these best practices in mind:

  1. Terminal Compatibility: Not all terminals support all capabilities. Check for capability support before using it.

  2. Efficient Updates: Update only the parts of the screen that have changed to improve performance.

  3. Cursor Position: Always be aware of the current cursor position, especially after clearing portions of the screen.

  4. Terminal Size: Be mindful of the terminal size when positioning content to avoid unexpected wrapping or scrolling.

  5. Restore State: When your application exits, restore the terminal to a usable state.

  6. User Experience: Use screen clearing and cursor control to create a more intuitive and responsive user interface.

  7. Accessibility: Consider users who may be using screen readers or other assistive technologies.

  8. Performance: Minimize the number of terminal operations to improve performance, especially over slow connections.

Screen clearing and cursor control are powerful tools for creating dynamic terminal applications. By using these features effectively, you can create more engaging and interactive command-line interfaces.