From 295188fdaa6b9473d0354735d4e6594c0e6cb4cb Mon Sep 17 00:00:00 2001 From: Taylor Southwick Date: Fri, 12 Feb 2021 17:19:51 -0800 Subject: [PATCH] Expose ITerminal.Width property --- src/System.CommandLine.Rendering/ITerminal.cs | 2 ++ src/System.CommandLine.Rendering/SystemConsoleTerminal.cs | 4 ++++ src/System.CommandLine.Rendering/TerminalBase.cs | 2 ++ src/System.CommandLine.Rendering/TestTerminal.cs | 2 +- src/System.CommandLine.Rendering/VirtualTerminal.cs | 2 ++ 5 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/System.CommandLine.Rendering/ITerminal.cs b/src/System.CommandLine.Rendering/ITerminal.cs index 111e2fc0e9..bd6082c0c8 100644 --- a/src/System.CommandLine.Rendering/ITerminal.cs +++ b/src/System.CommandLine.Rendering/ITerminal.cs @@ -22,5 +22,7 @@ public interface ITerminal : IConsole void HideCursor(); void ShowCursor(); + + int Width { get; } } } diff --git a/src/System.CommandLine.Rendering/SystemConsoleTerminal.cs b/src/System.CommandLine.Rendering/SystemConsoleTerminal.cs index ceb6d1ad62..46e3bf0ba0 100644 --- a/src/System.CommandLine.Rendering/SystemConsoleTerminal.cs +++ b/src/System.CommandLine.Rendering/SystemConsoleTerminal.cs @@ -1,6 +1,8 @@ // Copyright (c) .NET Foundation and contributors. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. +using System.CommandLine.IO; + namespace System.CommandLine.Rendering { public class SystemConsoleTerminal : TerminalBase @@ -22,6 +24,8 @@ public override ConsoleColor BackgroundColor set => System.Console.BackgroundColor = value; } + public override int Width => Console is SystemConsole system ? system.GetConsoleWindowWidth() : int.MaxValue; + public override ConsoleColor ForegroundColor { get => System.Console.ForegroundColor; diff --git a/src/System.CommandLine.Rendering/TerminalBase.cs b/src/System.CommandLine.Rendering/TerminalBase.cs index bc03d51acc..450e381b70 100644 --- a/src/System.CommandLine.Rendering/TerminalBase.cs +++ b/src/System.CommandLine.Rendering/TerminalBase.cs @@ -45,6 +45,8 @@ protected TerminalBase(IConsole console) public bool IsInputRedirected => Console.IsInputRedirected; + public abstract int Width { get; } + protected virtual void Dispose(bool disposing) { } diff --git a/src/System.CommandLine.Rendering/TestTerminal.cs b/src/System.CommandLine.Rendering/TestTerminal.cs index 84495f4aa8..4016a563dd 100644 --- a/src/System.CommandLine.Rendering/TestTerminal.cs +++ b/src/System.CommandLine.Rendering/TestTerminal.cs @@ -70,7 +70,7 @@ private void OnCharWrittenToOut(char c) public int Height { get; set; } = 100; - public int Width { get; set; } = 100; + public int Width { get; } = 100; public virtual void ResetColor() { diff --git a/src/System.CommandLine.Rendering/VirtualTerminal.cs b/src/System.CommandLine.Rendering/VirtualTerminal.cs index ef5e492e8b..064821fcfb 100644 --- a/src/System.CommandLine.Rendering/VirtualTerminal.cs +++ b/src/System.CommandLine.Rendering/VirtualTerminal.cs @@ -26,6 +26,8 @@ public override void ResetColor() Ansi.Color.Foreground.Default.EscapeSequence); } + public override int Width => int.MaxValue; + public override void Clear() { Console.Out.Write(