Package org.aesh.terminal
Class TerminalFeatures
java.lang.Object
org.aesh.terminal.TerminalFeatures
Advanced terminal features: queries, capability detection, semantic output,
and mode management.
This class wraps a Connection and provides access to all
terminal-specific functionality that goes beyond basic I/O. Access it
via Connection.terminal().
Features are organized into four areas:
- Queries — send escape sequences and parse responses (OSC, DA1/DA2, DECRQM, theme DSR)
- Capability Detection — heuristic checks based on terminal type and environment
- Semantic Output — shell integration markers, hyperlinks, clipboard
- Mode Toggles — synchronized output, grapheme cluster mode, theme notifications
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final longDefault timeout in milliseconds for terminal queries (OSC, DA1, DA2, etc.).static final longFast timeout in milliseconds for lightweight terminal queries. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionGet the color capabilities of this terminal connection.Get the color depth of this terminal connection.Access the underlying connection.Disable Mode 2027 (grapheme cluster segmentation).Disable Mode 2026 (synchronized output) — End Synchronized Update (ESU).voidDisable unsolicited theme change notifications.Enable Mode 2027 (grapheme cluster segmentation).Enable Mode 2026 (synchronized output) — Begin Synchronized Update (BSU).voidEnable unsolicited theme change notifications.voidEnable unsolicited theme change notifications with a handler.Get the current cursor position in the terminal.Get the detected terminal type based on environment variables.queryAnsi16Colors(long timeoutMs) Query the ANSI 16-color palette (colors 0-15) in a single batch operation.int[]queryBackgroundColor(long timeoutMs) Query the terminal for its background color using OSC 11.queryBatchOsc(long timeoutMs, int... oscCodes) Query multiple OSC color codes in a single batch operation.queryColors(long timeoutMs) Query foreground, background, and cursor colors in a single batch operation.int[]queryCursorColor(long timeoutMs) Query the terminal for its cursor color using OSC 12.queryDeviceAttributes(long timeoutMs) Query the terminal for both primary and secondary device attributes.int[]queryForegroundColor(long timeoutMs) Query the terminal for its foreground color using OSC 10.queryGraphemeClusterMode(long timeoutMs) Query the terminal to check if Mode 2027 is supported via DECRQM.queryImageProtocol(long timeoutMs) Query the terminal for its image protocol support.<T> TSend an OSC query with an index parameter to the terminal.<T> TSend an OSC (Operating System Command) query to the terminal.int[]queryPaletteColor(int index, long timeoutMs) Query the terminal for a palette color using OSC 4.int[]queryPaletteColorIfSupported(int index, long timeoutMs) Query palette color only if the terminal supports it.queryPaletteColors(long timeoutMs, int... indices) Query multiple palette colors in a single batch operation.queryPrimaryDeviceAttributes(long timeoutMs) Query the terminal for its primary device attributes (DA1).querySecondaryDeviceAttributes(long timeoutMs) Query the terminal for its secondary device attributes (DA2).booleanquerySupportsOscQueries(long timeoutMs) Query the terminal to check if OSC queries are supported.querySynchronizedOutput(long timeoutMs) Query the terminal to check if Mode 2026 is supported via DECRQM.<T> TqueryTerminal(String query, long timeoutMs, Function<int[], T> responseParser) Send a query to the terminal and wait for a response with timeout.queryThemeMode(long timeoutMs) Query the terminal for its current theme mode using the CSI ?queryWithResponseBuffering(long timeoutMs, int expectedCount, String batchQuery, Function<int[], Map<Integer, int[]>> parser) Shared orchestration for batch OSC queries with response buffering.booleanCheck if the terminal likely supports OSC 52 clipboard access.booleanCheck if the terminal likely supports OSC 10/11 color queries.booleanCheck if Mode 2027 (grapheme cluster segmentation) is likely supported.booleanCheck if OSC 8 hyperlinks are likely supported.booleansupportsOscCode(Device.OscCode oscCode) Check if the terminal likely supports a specific OSC code.booleanCheck if OSC (Operating System Command) queries are supported.booleanCheck if OSC queries are supported, using DA1 device attributes for improved detection.booleanCheck if the terminal likely supports OSC 4 palette color queries.booleanCheck if OSC 133 (shell integration) is likely supported.booleanCheck if Mode 2026 (synchronized output) is likely supported.booleanCheck if the terminal supports the CSI ?writeClipboard(String text) Write text to the system clipboard via OSC 52.Write OSC 133;D (Command Finished) to the terminal without an exit code.writeCommandFinished(int exitCode) Write OSC 133;D (Command Finished) to the terminal with an exit code.Write OSC 133;C (Command Start) to the terminal.writeHyperlink(String url, String text) Write a clickable hyperlink to the terminal.writeHyperlink(String url, String text, String id) Write a clickable hyperlink with a grouping id.Write OSC 133;B (Prompt End) to the terminal.Write OSC 133;A (Prompt Start) to the terminal.
-
Field Details
-
DEFAULT_QUERY_TIMEOUT_MS
public static final long DEFAULT_QUERY_TIMEOUT_MSDefault timeout in milliseconds for terminal queries (OSC, DA1, DA2, etc.). Suitable for most query operations where the terminal is expected to respond.- See Also:
-
FAST_QUERY_TIMEOUT_MS
public static final long FAST_QUERY_TIMEOUT_MSFast timeout in milliseconds for lightweight terminal queries. Use for simple queries like theme DSR or when low latency is important.- See Also:
-
-
Constructor Details
-
TerminalFeatures
-
-
Method Details
-
connection
Access the underlying connection.- Returns:
- the connection this features object wraps
-
queryTerminal
Send a query to the terminal and wait for a response with timeout.This method enters raw mode, sends the query, collects the response, and restores the original terminal attributes. It's useful for both OSC queries and CSI queries (DA1/DA2, DECRQM, theme DSR, etc.).
This method uses
Connection.setStdinHandler(Consumer)to receive responses, which requires the connection to be actively reading input (i.e.,Connection.reading()returns true). If not reading, this method returns null.- Type Parameters:
T- the type of the parsed response- Parameters:
query- the query sequence to sendtimeoutMs- timeout in milliseconds to wait for responseresponseParser- function to parse the response; should return non-null when a complete response is received, null to continue waiting- Returns:
- the parsed response, or null if not reading, timeout, or parsing failed
-
getCursorPosition
Get the current cursor position in the terminal.- Returns:
- the current cursor position as a Point (row, column)
-
queryOsc
Send an OSC (Operating System Command) query to the terminal.- Type Parameters:
T- the type of the parsed response- Parameters:
oscCode- the OSC code (e.g., 10 for foreground, 11 for background)param- the query parameter (typically "?" for queries)timeoutMs- timeout in milliseconds to wait for responseresponseParser- function to parse the response- Returns:
- the parsed response, or null if timeout or not supported
-
queryForegroundColor
public int[] queryForegroundColor(long timeoutMs) Query the terminal for its foreground color using OSC 10.- Parameters:
timeoutMs- timeout in milliseconds to wait for response- Returns:
- RGB array [r, g, b] (0-255 each), or null if not supported or timeout
-
queryBackgroundColor
public int[] queryBackgroundColor(long timeoutMs) Query the terminal for its background color using OSC 11.- Parameters:
timeoutMs- timeout in milliseconds to wait for response- Returns:
- RGB array [r, g, b] (0-255 each), or null if not supported or timeout
-
queryCursorColor
public int[] queryCursorColor(long timeoutMs) Query the terminal for its cursor color using OSC 12.- Parameters:
timeoutMs- timeout in milliseconds to wait for response- Returns:
- RGB array [r, g, b] (0-255 each), or null if not supported or timeout
-
queryThemeMode
Query the terminal for its current theme mode using the CSI ? 996 n protocol.- Parameters:
timeoutMs- timeout in milliseconds to wait for response- Returns:
TerminalTheme.DARKorTerminalTheme.LIGHT, ornullif not supported or timeout
-
queryOsc
public <T> T queryOsc(int oscCode, int index, String param, long timeoutMs, Function<int[], T> responseParser) Send an OSC query with an index parameter to the terminal.- Type Parameters:
T- the type of the parsed response- Parameters:
oscCode- the OSC code (e.g., 4 for palette color)index- the index parameter (e.g., palette color index 0-255)param- the query parameter (typically "?" for queries)timeoutMs- timeout in milliseconds to wait for responseresponseParser- function to parse the response- Returns:
- the parsed response, or null if timeout or not supported
-
queryPaletteColor
public int[] queryPaletteColor(int index, long timeoutMs) Query the terminal for a palette color using OSC 4.- Parameters:
index- the palette color index (0-255)timeoutMs- timeout in milliseconds to wait for response- Returns:
- RGB array [r, g, b] (0-255 each), or null if not supported or timeout
-
queryWithResponseBuffering
public Map<Integer,int[]> queryWithResponseBuffering(long timeoutMs, int expectedCount, String batchQuery, Function<int[], Map<Integer, int[]>> parser) Shared orchestration for batch OSC queries with response buffering.- Parameters:
timeoutMs- timeout in milliseconds to wait for all responsesexpectedCount- the number of expected responsesbatchQuery- the concatenated query string to sendparser- function to parse the buffered response into a result map- Returns:
- map from key to RGB array [r, g, b] (0-255 each)
-
queryBatchOsc
Query multiple OSC color codes in a single batch operation.- Parameters:
timeoutMs- timeout in milliseconds to wait for all responsesoscCodes- the OSC codes to query (e.g., 10 for foreground, 11 for background)- Returns:
- map from OSC code to RGB array [r, g, b] (0-255 each)
-
queryColors
Query foreground, background, and cursor colors in a single batch operation.- Parameters:
timeoutMs- timeout in milliseconds to wait for all responses- Returns:
- map from OSC code to RGB array [r, g, b] (0-255 each)
-
queryPaletteColors
Query multiple palette colors in a single batch operation.- Parameters:
timeoutMs- timeout in milliseconds to wait for all responsesindices- the palette color indices to query (0-255)- Returns:
- map from palette index to RGB array [r, g, b] (0-255 each)
-
queryAnsi16Colors
Query the ANSI 16-color palette (colors 0-15) in a single batch operation.- Parameters:
timeoutMs- timeout in milliseconds to wait for all responses- Returns:
- map from palette index (0-15) to RGB array [r, g, b] (0-255 each)
-
queryPrimaryDeviceAttributes
Query the terminal for its primary device attributes (DA1).- Parameters:
timeoutMs- timeout in milliseconds to wait for response- Returns:
- DeviceAttributes with DA1 data, or null if not supported or timeout
-
querySecondaryDeviceAttributes
Query the terminal for its secondary device attributes (DA2).- Parameters:
timeoutMs- timeout in milliseconds to wait for response- Returns:
- DeviceAttributes with DA2 data, or null if not supported or timeout
-
queryDeviceAttributes
Query the terminal for both primary and secondary device attributes.- Parameters:
timeoutMs- timeout in milliseconds to wait for each response- Returns:
- DeviceAttributes with merged DA1 and DA2 data, or null if neither succeeded
-
queryImageProtocol
Query the terminal for its image protocol support.- Parameters:
timeoutMs- timeout in milliseconds to wait for DA1 response- Returns:
- the detected image protocol
-
querySynchronizedOutput
Query the terminal to check if Mode 2026 is supported via DECRQM.- Parameters:
timeoutMs- timeout in milliseconds to wait for response- Returns:
- true if supported, false if not supported, null if no response/timeout
-
queryGraphemeClusterMode
Query the terminal to check if Mode 2027 is supported via DECRQM.- Parameters:
timeoutMs- timeout in milliseconds to wait for response- Returns:
- true if supported, false if not supported, null if no response/timeout
-
querySupportsOscQueries
public boolean querySupportsOscQueries(long timeoutMs) Query the terminal to check if OSC queries are supported.- Parameters:
timeoutMs- timeout in milliseconds for the DA1 query- Returns:
- true if OSC queries are likely supported
-
queryPaletteColorIfSupported
public int[] queryPaletteColorIfSupported(int index, long timeoutMs) Query palette color only if the terminal supports it.- Parameters:
index- the palette color index (0-255)timeoutMs- timeout in milliseconds to wait for response- Returns:
- RGB array [r, g, b] (0-255 each), or null if not supported or timeout
-
supportsOscQueries
public boolean supportsOscQueries()Check if OSC (Operating System Command) queries are supported.- Returns:
- true if OSC queries are likely supported
-
supportsOscQueries
Check if OSC queries are supported, using DA1 device attributes for improved detection.- Parameters:
attrs- the device attributes from DA1 query (may be null)- Returns:
- true if OSC queries are likely supported
-
colorDepth
Get the color depth of this terminal connection.- Returns:
- the detected color depth
-
colorCapability
Get the color capabilities of this terminal connection.- Returns:
- the detected color capabilities
-
supportsThemeQuery
public boolean supportsThemeQuery()Check if the terminal supports the CSI ? 996 n theme mode query.- Returns:
- true if the terminal supports theme mode queries
-
supportsSynchronizedOutput
public boolean supportsSynchronizedOutput()Check if Mode 2026 (synchronized output) is likely supported.- Returns:
- true if Mode 2026 is likely supported
-
supportsShellIntegration
public boolean supportsShellIntegration()Check if OSC 133 (shell integration) is likely supported.- Returns:
- true if OSC 133 shell integration is likely supported
-
supportsGraphemeClusterMode
public boolean supportsGraphemeClusterMode()Check if Mode 2027 (grapheme cluster segmentation) is likely supported.- Returns:
- true if Mode 2027 is likely supported
-
supportsHyperlinks
public boolean supportsHyperlinks()Check if OSC 8 hyperlinks are likely supported.- Returns:
- true if OSC 8 hyperlinks are likely supported
-
getTerminalType
Get the detected terminal type based on environment variables.- Returns:
- the detected terminal type
-
supportsOscCode
Check if the terminal likely supports a specific OSC code.- Parameters:
oscCode- the OSC code to check- Returns:
- true if the terminal likely supports this OSC code
-
supportsPaletteQuery
public boolean supportsPaletteQuery()Check if the terminal likely supports OSC 4 palette color queries.- Returns:
- true if OSC 4 is likely supported
-
supportsColorQuery
public boolean supportsColorQuery()Check if the terminal likely supports OSC 10/11 color queries.- Returns:
- true if OSC 10/11 are likely supported
-
supportsClipboard
public boolean supportsClipboard()Check if the terminal likely supports OSC 52 clipboard access.- Returns:
- true if OSC 52 clipboard access is likely supported
-
enableSynchronizedOutput
Enable Mode 2026 (synchronized output) — Begin Synchronized Update (BSU).- Returns:
- the underlying connection
-
disableSynchronizedOutput
Disable Mode 2026 (synchronized output) — End Synchronized Update (ESU).- Returns:
- the underlying connection
-
writePromptStart
Write OSC 133;A (Prompt Start) to the terminal.- Returns:
- the underlying connection
-
writePromptEnd
Write OSC 133;B (Prompt End) to the terminal.- Returns:
- the underlying connection
-
writeCommandStart
Write OSC 133;C (Command Start) to the terminal.- Returns:
- the underlying connection
-
writeCommandFinished
Write OSC 133;D (Command Finished) to the terminal without an exit code.- Returns:
- the underlying connection
-
writeCommandFinished
Write OSC 133;D (Command Finished) to the terminal with an exit code.- Parameters:
exitCode- the command exit code- Returns:
- the underlying connection
-
enableGraphemeClusterMode
Enable Mode 2027 (grapheme cluster segmentation).- Returns:
- the underlying connection
-
disableGraphemeClusterMode
Disable Mode 2027 (grapheme cluster segmentation).- Returns:
- the underlying connection
-
writeHyperlink
Write a clickable hyperlink to the terminal.- Parameters:
url- the hyperlink URLtext- the visible text- Returns:
- the underlying connection
-
writeHyperlink
Write a clickable hyperlink with a grouping id.- Parameters:
url- the hyperlink URLtext- the visible textid- the grouping id- Returns:
- the underlying connection
-
writeClipboard
Write text to the system clipboard via OSC 52.- Parameters:
text- the text to copy to the clipboard- Returns:
- the underlying connection
-
enableThemeChangeNotification
public void enableThemeChangeNotification()Enable unsolicited theme change notifications. -
enableThemeChangeNotification
Enable unsolicited theme change notifications with a handler.- Parameters:
handler- the handler to invoke with the new theme
-
disableThemeChangeNotification
public void disableThemeChangeNotification()Disable unsolicited theme change notifications.
-