Class NetConsoleDriver
- Namespace
- SharpConsoleUI.Drivers
- Assembly
- SharpConsoleUI.dll
Provides a cross-platform console driver implementation.
public class NetConsoleDriver : IConsoleDriver
- Inheritance
-
NetConsoleDriver
- Implements
- Inherited Members
- Extension Methods
Remarks
Windows: Uses .NET Console APIs with Win32 console mode configuration for virtual terminal input/output and mouse reporting.
Unix: Bypasses .NET's Console infrastructure entirely, using raw libc I/O for both input (read from stdin fd 0) and output (write to stdout fd 1). Terminal is put into raw mode via tcgetattr/tcsetattr (cfmakeraw equivalent), and an alternate screen buffer is used for clean display. Console.Out is redirected to /dev/null to prevent .NET runtime code from touching stdout. Window size is queried via ioctl TIOCGWINSZ instead of Console.WindowWidth/Height. SIGINT/SIGTSTP are suppressed via signal() instead of Console.TreatControlCAsInput.
Why: .NET's ConsolePal.Unix calls tcsetattr on every Console.ReadKey, Console.SetCursorPosition, Console.CursorVisible, and even Console.OutputEncoding access. Each tcsetattr briefly toggles the ECHO flag, creating windows where raw mouse/keyboard ANSI sequences leak to the screen. The only reliable fix is to never touch any Console.* property on the hot path.
Approach inspired by Terminal.Gui v2 (https://github.com/gui-cs/Terminal.Gui), which solved the same problem in their Unix NetDriver by using raw file descriptor I/O and avoiding all .NET Console APIs on Unix. Key techniques adopted: reading from stdin fd 0 directly (not /dev/tty, to avoid byte competition with .NET's internal fd 0 reader), tcflush to clear pending input, and alternate screen buffer usage.
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()
FillCells(int, int, int, char, Color, Color)
Fills a horizontal run of cells at the specified position with the given character and colors.
public void FillCells(int x, int y, int width, char character, Color fg, Color bg)
Parameters
xintThe starting horizontal position (column).
yintThe vertical position (row).
widthintThe number of cells to fill.
charactercharThe character to fill with.
fgColorThe foreground color.
bgColorThe background color.
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.
SetNarrowCell(int, int, char, Color, Color)
Sets a single cell at the specified position with the given character and colors.
public void SetNarrowCell(int x, int y, char character, Color fg, Color bg)
Parameters
xintThe horizontal position (column).
yintThe vertical position (row).
charactercharThe character to write.
fgColorThe foreground color.
bgColorThe background color.
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()
WriteBufferRegion(int, int, CharacterBuffer, int, int, int, Color)
Copies a horizontal strip of cells from a CharacterBuffer directly to the console output buffer, bypassing ANSI string serialization and parsing.
public void WriteBufferRegion(int destX, int destY, CharacterBuffer source, int srcX, int srcY, int width, Color fallbackBg)
Parameters
destXintDestination screen X position.
destYintDestination screen Y position.
sourceCharacterBufferThe source CharacterBuffer.
srcXintSource X offset within the buffer.
srcYintSource Y (row) within the buffer.
widthintNumber of cells to write.
fallbackBgColorBackground color for padding when source is out of bounds.
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