Skip to content

Make IConsoleDriver public and add ConsoleDriverFactory for extensibility#330

Merged
shibayan merged 5 commits intomasterfrom
copilot/review-architectural-design
Mar 5, 2026
Merged

Make IConsoleDriver public and add ConsoleDriverFactory for extensibility#330
shibayan merged 5 commits intomasterfrom
copilot/review-architectural-design

Conversation

Copy link
Contributor

Copilot AI commented Mar 4, 2026

IConsoleDriver and DefaultConsoleDriver were internal, blocking custom console driver implementations and making forms untestable without a real interactive console.

Changes

  • IConsoleDriver — promoted from internal to public
  • DefaultConsoleDriver — promoted from internal sealed to public (unsealed to allow subclassing)
  • Prompt.ConsoleDriverFactory — new Func<IConsoleDriver> property on Prompt, defaults to () => new DefaultConsoleDriver(); setter guards against null
  • FormBase — constructor now calls Prompt.ConsoleDriverFactory() instead of hardcoding new DefaultConsoleDriver()
  • ConsoleDriverFactoryTests — new test class covering: default factory is non-null, custom factory assignment, null factory throws ArgumentNullException, factory reset, and custom IConsoleDriver implementation
  • IConsoleDriverTests — new test class covering all 17 interface members via a RecordingConsoleDriver stub: all methods (Beep, Reset, ClearLine, ReadKey, Write, WriteLine, SetCursorPosition, Dispose), all properties (KeyAvailable, CursorVisible, CursorLeft, CursorTop, BufferWidth, BufferHeight, WindowWidth, WindowHeight), and CancellationCallback

Usage

// Inject a custom or mock driver globally before running prompts
Prompt.ConsoleDriverFactory = () => new MyCustomConsoleDriver();

Fully backward-compatible — no changes required for existing consumers.

Original prompt

全体的なアーキテクチャ設計を見直してみてください


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…lity

Co-authored-by: shibayan <1356444+shibayan@users.noreply.github.com>
Copilot AI changed the title [WIP] Review overall architectural design Make IConsoleDriver public and add ConsoleDriverFactory for extensibility Mar 4, 2026
@shibayan
Copy link
Owner

shibayan commented Mar 4, 2026

@copilot Please add test cases

Co-authored-by: shibayan <1356444+shibayan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 4, 2026

@copilot Please add test cases

Added ConsoleDriverFactoryTests in commit 975d9f1 covering:

  • Default factory is non-null
  • Setting a custom factory stores and invokes it correctly
  • Setting null throws ArgumentNullException
  • Factory can be reset to a previous value
  • A custom IConsoleDriver implementation can be assigned and retrieved

@shibayan
Copy link
Owner

shibayan commented Mar 4, 2026

@copilot Please add IConsoleDriver test cases

Co-authored-by: shibayan <1356444+shibayan@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Mar 4, 2026

@copilot Please add IConsoleDriver test cases

Added IConsoleDriverTests in commit 93afb8c covering all 17 interface members via a RecordingConsoleDriver stub:

  • Methods: Beep, Reset, ClearLine (with argument verification), ReadKey (return value), Write (value + color), WriteLine, SetCursorPosition (coordinates), Dispose
  • Properties: KeyAvailable, CursorVisible (set), CursorLeft, CursorTop, BufferWidth, BufferHeight, WindowWidth, WindowHeight
  • CancellationCallback (set + invoke)

@shibayan shibayan requested a review from Copilot March 5, 2026 02:29
@shibayan shibayan added the enhancement New feature or request label Mar 5, 2026
@shibayan shibayan marked this pull request as ready for review March 5, 2026 02:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR makes the console driver abstraction extensible by exposing IConsoleDriver publicly and introducing Prompt.ConsoleDriverFactory so consumers (and tests) can inject custom console implementations instead of hardwiring DefaultConsoleDriver.

Changes:

  • Promote IConsoleDriver and DefaultConsoleDriver to public and add Prompt.ConsoleDriverFactory (with null-guard on setter).
  • Update FormBase to construct its driver via Prompt.ConsoleDriverFactory() and set CancellationCallback.
  • Add new unit tests covering the factory behavior and a stub implementation exercising the interface members.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
Sharprompt/Prompt.Configuration.cs Adds global ConsoleDriverFactory configuration for driver injection.
Sharprompt/Forms/FormBase.cs Uses Prompt.ConsoleDriverFactory() instead of directly instantiating DefaultConsoleDriver.
Sharprompt/Drivers/IConsoleDriver.cs Makes the console driver interface public for external implementations/mocks.
Sharprompt/Drivers/DefaultConsoleDriver.cs Makes the default driver public (and no longer sealed).
Sharprompt.Tests/ConsoleDriverFactoryTests.cs Verifies default/custom factory assignment and null-guard behavior.
Sharprompt.Tests/IConsoleDriverTests.cs Adds a recording stub to exercise the public interface contract.
Comments suppressed due to low confidence (1)

Sharprompt/Drivers/DefaultConsoleDriver.cs:17

  • DefaultConsoleDriver is now public and the type initializer throws when IO is redirected. Exceptions from static constructors are surfaced as TypeInitializationException, which is a confusing failure mode for public consumers. Consider moving the environment checks into the instance constructor (or a factory method) so callers get the intended InvalidOperationException directly, and avoid making type-loading fail for merely referencing the type.
public class DefaultConsoleDriver : IConsoleDriver
{
    static DefaultConsoleDriver()
    {
        if (Console.IsInputRedirected || Console.IsOutputRedirected)
        {
            throw new InvalidOperationException(Resource.Message_NotSupportedEnvironment);
        }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@shibayan shibayan merged commit f3620a1 into master Mar 5, 2026
5 checks passed
@shibayan shibayan deleted the copilot/review-architectural-design branch March 5, 2026 02:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants