diff --git a/src/Terminal.Gui.Cli/CliHost.cs b/src/Terminal.Gui.Cli/CliHost.cs index 040ba1d..2ed053d 100644 --- a/src/Terminal.Gui.Cli/CliHost.cs +++ b/src/Terminal.Gui.Cli/CliHost.cs @@ -184,7 +184,16 @@ private static CommandResult CreateCancelledResult () private async Task RunWithTerminalGuiAsync (ICliCommand command, CommandRunOptions runOptions, CancellationToken cancellationToken) { - using IApplication app = Application.Create ().Init (); + var useInline = command.Kind == CommandKind.Input && !runOptions.Fullscreen; + + using IApplication app = Application.Create (); + app.AppModel = useInline ? AppModel.Inline : AppModel.FullScreen; + + if (!useInline) + { + app.Init (); + } + return await command.RunAsync (app, runOptions.Initial, runOptions, cancellationToken); } diff --git a/src/Terminal.Gui.Cli/InputCommandRunner.cs b/src/Terminal.Gui.Cli/InputCommandRunner.cs index ccdd233..9172fb1 100644 --- a/src/Terminal.Gui.Cli/InputCommandRunner.cs +++ b/src/Terminal.Gui.Cli/InputCommandRunner.cs @@ -23,6 +23,11 @@ public static async Task> RunAsync +/// Verifies that CliHost.RunWithTerminalGuiAsync sets IApplication.AppModel +/// correctly based on CommandKind and the --fullscreen option. +/// +public sealed class AppModelDispatchTests +{ + [Fact] + public async Task InputCommand_WithoutFullscreen_SetsInlineAppModel () + { + AppModel? captured = null; + SpyInputCommand spy = new (app => captured = app.AppModel); + + CliHost host = new (); + host.Registry.Register (spy); + using StringWriter stdout = new (); + using StringWriter stderr = new (); + + await host.RunAsync (["spy-input"], TestContext.Current.CancellationToken, stdout, stderr); + + Assert.NotNull (captured); + Assert.Equal (AppModel.Inline, captured); + } + + [Fact] + public async Task InputCommand_WithFullscreen_SetsFullScreenAppModel () + { + AppModel? captured = null; + SpyInputCommand spy = new (app => captured = app.AppModel); + + CliHost host = new (); + host.Registry.Register (spy); + using StringWriter stdout = new (); + using StringWriter stderr = new (); + + await host.RunAsync (["spy-input", "--fullscreen"], TestContext.Current.CancellationToken, stdout, stderr); + + Assert.NotNull (captured); + Assert.Equal (AppModel.FullScreen, captured); + } + + [Fact] + public async Task ViewerCommand_SetsFullScreenAppModel () + { + AppModel? captured = null; + SpyViewerCommand spy = new (app => captured = app.AppModel); + + CliHost host = new (); + host.Registry.Register (spy); + using StringWriter stdout = new (); + using StringWriter stderr = new (); + + await host.RunAsync (["spy-viewer"], TestContext.Current.CancellationToken, stdout, stderr); + + Assert.NotNull (captured); + Assert.Equal (AppModel.FullScreen, captured); + } + + /// Spy input command that captures IApplication.AppModel then stops immediately. + private sealed class SpyInputCommand : ICliCommand + { + private readonly Action _onRun; + + public SpyInputCommand (Action onRun) + { + _onRun = onRun; + } + + public string PrimaryAlias => "spy-input"; + public IReadOnlyList Aliases { get; } = ["spy-input"]; + public string Description => "Spy input command for testing."; + public CommandKind Kind => CommandKind.Input; + public Type ResultType => typeof (string); + public IReadOnlyList Options { get; } = []; + + public Task RunAsync (IApplication app, string? initial, CommandRunOptions options, + CancellationToken cancellationToken) + { + _onRun (app); + app.RequestStop (); + + return Task.FromResult (new CommandResult (CommandStatus.Ok, "test", null, null)); + } + } + + /// Spy viewer command that captures IApplication.AppModel then stops immediately. + private sealed class SpyViewerCommand : IViewerCommand + { + private readonly Action _onRun; + + public SpyViewerCommand (Action onRun) + { + _onRun = onRun; + } + + public string PrimaryAlias => "spy-viewer"; + public IReadOnlyList Aliases { get; } = ["spy-viewer"]; + public string Description => "Spy viewer command for testing."; + public CommandKind Kind => CommandKind.Viewer; + public Type ResultType => typeof (void); + public IReadOnlyList Options { get; } = []; + + public Task RunAsync (IApplication app, string? initial, CommandRunOptions options, + CancellationToken cancellationToken) + { + _onRun (app); + app.RequestStop (); + + return Task.FromResult (new CommandResult (CommandStatus.Ok, null, null, null)); + } + + public Task RenderCatAsync (CommandRunOptions options, TextWriter stdout, + CancellationToken cancellationToken) + { + return Task.FromResult (null); + } + } +}