Skip to content

ConsolePresentationAdapter

Namespace: Hex1b

Assembly: Hex1b.dll

Console presentation adapter using platform-specific raw mode for proper input handling.

csharp
public sealed class ConsolePresentationAdapter : IHex1bTerminalPresentationAdapter, IAsyncDisposable, ITerminalReflowProvider

Inheritance

ObjectConsolePresentationAdapter

Implements

Constructors

ConsolePresentationAdapter(bool, bool)

Creates a new console presentation adapter with raw mode support.

Parameters:

  • enableMouse (Boolean): Whether to enable mouse tracking.
  • preserveOPost (Boolean): If true, preserve output post-processing (LF→CRLF conversion) in raw mode. This is useful for WithProcess scenarios where child programs expect normal output handling. Defaults to false for full raw mode (required for terminal emulators and Hex1bApp).
csharp
public ConsolePresentationAdapter(bool enableMouse = false, bool preserveOPost = false)

Properties

Capabilities

Capability hints that inform optimization strategies.

Returns: TerminalCapabilities

csharp
public TerminalCapabilities Capabilities { get; }

Height

Current terminal height in rows.

Returns: Int32

csharp
public int Height { get; }

ReflowEnabled

Gets whether reflow is enabled. When false, the terminal uses standard crop-and-extend resize behavior even though the adapter implements this interface. Defaults to true.

Returns: Boolean

csharp
public bool ReflowEnabled { get; }

ShouldClearSoftWrapOnAbsolutePosition

Gets whether absolute cursor positioning (CUP, HVP) should clear the flag on the current row's last cell.

Returns: Boolean

csharp
public bool ShouldClearSoftWrapOnAbsolutePosition { get; }

Width

Current terminal width in columns.

Returns: Int32

csharp
public int Width { get; }

Methods

DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

Returns: ValueTask

A task that represents the asynchronous dispose operation.

csharp
public ValueTask DisposeAsync()

EnterRawModeAsync(CancellationToken)

Enter raw mode for proper input capture.

Parameters:

Returns: ValueTask

csharp
public ValueTask EnterRawModeAsync(CancellationToken ct = default)

ExitRawModeAsync(CancellationToken)

Exit raw mode and restore normal terminal input handling.

Parameters:

Returns: ValueTask

csharp
public ValueTask ExitRawModeAsync(CancellationToken ct = default)

FlushAsync(CancellationToken)

Flush any buffered output immediately.

Parameters:

Returns: ValueTask

csharp
public ValueTask FlushAsync(CancellationToken ct = default)

GetCursorPosition()

Gets the current cursor position in the terminal. Returns (0, 0) if the position cannot be determined.

Returns: ValueTuple<Int32, Int32>

A tuple of (Row, Column), both 0-based.

csharp
public (int Row, int Column) GetCursorPosition()

ReadInputAsync(CancellationToken)

Receive input (keystrokes, mouse events as ANSI sequences) FROM the user.

Parameters:

Returns: ValueTask<Byte>>

Raw input bytes from the user, or empty when disconnected.

csharp
public ValueTask<ReadOnlyMemory<byte>> ReadInputAsync(CancellationToken ct = default)

Reflow(ReflowContext)

Performs reflow of terminal content during a resize operation.

Parameters:

  • context (ReflowContext): The current terminal state including screen buffer, scrollback, and cursor position.

Returns: ReflowResult

The reflowed terminal state with new buffer, scrollback, and cursor position.

csharp
public ReflowResult Reflow(ReflowContext context)

WithReflow()

Enables reflow using the auto-detected strategy for the current terminal emulator. By default, reflow is disabled and resize uses standard crop behavior.

Returns: ConsolePresentationAdapter

This adapter for fluent chaining.

csharp
public ConsolePresentationAdapter WithReflow()

WithReflow(ITerminalReflowProvider)

Enables reflow with a specific strategy, overriding auto-detection. By default, reflow is disabled and resize uses standard crop behavior.

Parameters:

Returns: ConsolePresentationAdapter

This adapter for fluent chaining.

csharp
public ConsolePresentationAdapter WithReflow(ITerminalReflowProvider strategy)

WriteOutputAsync(ReadOnlyMemory<byte>, CancellationToken)

Write rendered output TO the presentation layer (display).

Parameters:

Returns: ValueTask

csharp
public ValueTask WriteOutputAsync(ReadOnlyMemory<byte> data, CancellationToken ct = default)

Events

Disconnected

Raised when the presentation layer disconnects (e.g., terminal closed, WebSocket dropped).

Returns: Action

csharp
public event Action? Disconnected

Resized

Raised when the presentation layer is resized by the user.

Returns: Action<Int32, Int32>

csharp
public event Action<int, int>? Resized

Remarks

This adapter uses raw terminal mode (termios on Unix, SetConsoleMode on Windows) to properly capture mouse events, escape sequences, and control characters.

Released under the MIT License.