Class NetConsoleDriver
- Namespace
- SharpConsoleUI.Drivers
- Assembly
- SharpConsoleUI.dll
Provides a cross-platform console driver implementation using .NET Console APIs.
public class NetConsoleDriver : IConsoleDriver
- Inheritance
-
NetConsoleDriver
- Implements
- Inherited Members
Remarks
This driver supports both Windows and Unix-like platforms, handling platform-specific console mode configuration for mouse input, virtual terminal processing, and other features.
On Windows, the driver configures console modes using Win32 API calls to enable virtual terminal input/output and mouse reporting.
Mouse events are parsed from ANSI escape sequences in both X10 and SGR formats, supporting button presses, releases, clicks, double-clicks, triple-clicks, and wheel events.
Constructors
NetConsoleDriver(NetConsoleDriverOptions)
Initializes a new instance of the NetConsoleDriver class with configuration options.
public NetConsoleDriver(NetConsoleDriverOptions options)
Parameters
optionsNetConsoleDriverOptionsConfiguration options for the driver.
Exceptions
- ArgumentNullException
Thrown when
optionsis null.- ApplicationException
Thrown when console mode configuration fails on Windows platforms.
NetConsoleDriver(RenderMode)
Initializes a new instance of the NetConsoleDriver class with a specific render mode.
public NetConsoleDriver(RenderMode renderMode = RenderMode.Buffer)
Parameters
renderModeRenderModeThe rendering mode to use. Defaults to Buffer.
Exceptions
- ApplicationException
Thrown when console mode configuration fails on Windows platforms.
Properties
Options
Gets the driver configuration options.
public NetConsoleDriverOptions Options { get; }
Property Value
RenderMode
Gets the rendering mode for console output.
public RenderMode RenderMode { get; }
Property Value
- RenderMode
The current render mode.
Remarks
The render mode is set during driver construction and cannot be changed afterward.
ScreenSize
Gets the current size of the console screen.
public Size ScreenSize { get; }
Property Value
Methods
Cleanup()
Restores the console to its original configuration.
public void Cleanup()
Remarks
On Windows, this method restores the original console modes for input, output, and error handles. This method is automatically called by Stop().
Exceptions
- ApplicationException
Thrown when restoring console modes fails on Windows platforms.
Clear()
Clears the console screen or buffer.
public void Clear()
Flush()
Flushes any buffered output to the console.
public void Flush()
Remarks
For buffered render modes, this triggers the actual rendering to the console. For direct render modes, this may be a no-op.
GetDirtyCharacterCount()
Gets the count of dirty characters in the rendering buffer.
public int GetDirtyCharacterCount()
Returns
- int
The number of dirty characters, or 0 if not using buffered rendering.
Initialize(ConsoleWindowSystem)
Initializes the driver with a reference to the window system. Called by ConsoleWindowSystem after state services are created.
public void Initialize(ConsoleWindowSystem windowSystem)
Parameters
windowSystemConsoleWindowSystemThe window system instance
ResetCursorShape()
Resets the cursor to the default shape.
public void ResetCursorShape()
SetCursorPosition(int, int)
Sets the cursor position on the console screen.
public void SetCursorPosition(int x, int y)
Parameters
SetCursorShape(CursorShape)
Sets the cursor shape/style.
public void SetCursorShape(CursorShape shape)
Parameters
shapeCursorShapeThe desired cursor shape.
SetCursorVisible(bool)
Sets the visibility of the cursor.
public void SetCursorVisible(bool visible)
Parameters
visibleboolTrue to show the cursor, false to hide it.
Start()
Starts the console driver, initializing input handling and enabling mouse/keyboard events.
public void Start()
Stop()
Stops the console driver, disabling input handling and restoring the console to its original state.
public void Stop()
WriteToConsole(int, int, string)
Writes a string value to the console at the specified position.
public void WriteToConsole(int x, int y, string value)
Parameters
xintThe horizontal position (column) to write at.
yintThe vertical position (row) to write at.
valuestringThe string value to write, which may include ANSI escape sequences.
Events
KeyPressed
Occurs when a key is pressed on the keyboard.
public event EventHandler<ConsoleKeyInfo>? KeyPressed
Event Type
MouseEvent
Occurs when a mouse event is detected.
public event IConsoleDriver.MouseEventHandler? MouseEvent
Event Type
ScreenResized
Occurs when the console screen is resized.
public event EventHandler<Size>? ScreenResized