Skip to content

Commit a57010b

Browse files
Copilotjkotas
andauthored
Address review: merge TerminalFormatStrings.Unix.cs, make s_instance internal, drop TerminalFormatStringsInstance
Co-authored-by: jkotas <[email protected]>
1 parent 1c664b1 commit a57010b

6 files changed

Lines changed: 24 additions & 32 deletions

File tree

‎src/libraries/System.Console/src/System.Console.csproj‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,6 @@
197197
<Compile Include="System\ConsolePal.Unix.cs" />
198198
<Compile Include="System\ConsolePal.Unix.ConsoleStream.cs" />
199199
<Compile Include="System\TerminalFormatStrings.cs" />
200-
<Compile Include="System\TerminalFormatStrings.Unix.cs" />
201200
<Compile Include="$(CommonPath)System\Console\ConsoleUtils.cs"
202201
Link="Common\System\Console\ConsoleUtils.cs" />
203202
<Compile Include="$(CommonPath)System\NullableBool.cs"

‎src/libraries/System.Console/src/System/ConsolePal.Unix.cs‎

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ internal static partial class ConsolePal
3939
private static int s_invalidateCachedSettings = 1; // Tracks whether we should invalidate the cached settings.
4040
private static SafeFileHandle? s_terminalHandle; // Tracks the handle used for writing to the terminal.
4141

42-
/// <summary>Gets the terminal information for the terminal.</summary>
43-
public static TerminalFormatStrings TerminalFormatStringsInstance => TerminalFormatStrings.Instance;
44-
4542
public static Stream OpenStandardInput()
4643
{
4744
return new UnixConsoleStream(OpenStandardInputHandle(), FileAccess.Read,
@@ -203,7 +200,7 @@ public static string Title
203200
if (Console.IsOutputRedirected)
204201
return;
205202

206-
string? titleFormat = TerminalFormatStringsInstance.Title;
203+
string? titleFormat = TerminalFormatStrings.s_instance.Title;
207204
if (!string.IsNullOrEmpty(titleFormat))
208205
{
209206
string ansiStr = TermInfo.ParameterizedStrings.Evaluate(titleFormat, value);
@@ -216,15 +213,15 @@ public static void Beep()
216213
{
217214
if (!Console.IsOutputRedirected)
218215
{
219-
WriteTerminalAnsiString(TerminalFormatStringsInstance.Bell, mayChangeCursorPosition: false);
216+
WriteTerminalAnsiString(TerminalFormatStrings.s_instance.Bell, mayChangeCursorPosition: false);
220217
}
221218
}
222219

223220
public static void Clear()
224221
{
225222
if (!Console.IsOutputRedirected)
226223
{
227-
WriteTerminalAnsiString(TerminalFormatStringsInstance.Clear);
224+
WriteTerminalAnsiString(TerminalFormatStrings.s_instance.Clear);
228225
}
229226
}
230227

@@ -247,7 +244,7 @@ public static void SetTerminalCursorPosition(int left, int top)
247244
return;
248245
}
249246

250-
string? cursorAddressFormat = TerminalFormatStringsInstance.CursorAddress;
247+
string? cursorAddressFormat = TerminalFormatStrings.s_instance.CursorAddress;
251248
if (!string.IsNullOrEmpty(cursorAddressFormat))
252249
{
253250
string ansiStr = TermInfo.ParameterizedStrings.Evaluate(cursorAddressFormat, top, left);
@@ -373,8 +370,8 @@ private static void GetWindowSize(out int width, out int height)
373370
}
374371
else
375372
{
376-
s_windowWidth = TerminalFormatStringsInstance.Columns;
377-
s_windowHeight = TerminalFormatStringsInstance.Lines;
373+
s_windowWidth = TerminalFormatStrings.s_instance.Columns;
374+
s_windowHeight = TerminalFormatStrings.s_instance.Lines;
378375
}
379376
}
380377

@@ -400,8 +397,8 @@ public static bool CursorVisible
400397
if (!Console.IsOutputRedirected)
401398
{
402399
WriteTerminalAnsiString(value ?
403-
TerminalFormatStringsInstance.CursorVisible :
404-
TerminalFormatStringsInstance.CursorInvisible);
400+
TerminalFormatStrings.s_instance.CursorVisible :
401+
TerminalFormatStrings.s_instance.CursorInvisible);
405402
}
406403
}
407404
}
@@ -803,10 +800,10 @@ private static void WriteSetColorString(bool foreground, ConsoleColor color)
803800
}
804801

805802
// We haven't yet computed a format string. Compute it, use it, then cache it.
806-
string? formatString = foreground ? TerminalFormatStringsInstance.Foreground : TerminalFormatStringsInstance.Background;
803+
string? formatString = foreground ? TerminalFormatStrings.s_instance.Foreground : TerminalFormatStrings.s_instance.Background;
807804
if (!string.IsNullOrEmpty(formatString))
808805
{
809-
int maxColors = TerminalFormatStringsInstance.MaxColors; // often 8 or 16; 0 is invalid
806+
int maxColors = TerminalFormatStrings.s_instance.MaxColors; // often 8 or 16; 0 is invalid
810807
if (maxColors > 0)
811808
{
812809
// The values of the ConsoleColor enums unfortunately don't map to the
@@ -850,7 +847,7 @@ private static void WriteResetColorString()
850847
{
851848
if (ConsoleUtils.EmitAnsiColorCodes)
852849
{
853-
WriteTerminalAnsiColorString(TerminalFormatStringsInstance.Reset);
850+
WriteTerminalAnsiColorString(TerminalFormatStrings.s_instance.Reset);
854851
}
855852
}
856853

@@ -904,7 +901,7 @@ private static unsafe void EnsureInitializedCore()
904901
// "application mode". This will both transition it immediately, as well as allow
905902
// the native lib later to handle signals that require re-entering the mode.
906903
if (s_terminalHandle != null &&
907-
TerminalFormatStringsInstance.KeypadXmit is string keypadXmit)
904+
TerminalFormatStrings.s_instance.KeypadXmit is string keypadXmit)
908905
{
909906
Interop.Sys.SetKeypadXmit(s_terminalHandle, keypadXmit);
910907
}

‎src/libraries/System.Console/src/System/IO/StdInReader.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ private bool ReadLineCore(bool consumeKeys)
212212
if (ConsolePal.TryGetCursorPosition(out int left, out int top, reinitializeForRead: true) &&
213213
left == 0 && top > 0)
214214
{
215-
s_clearToEol ??= ConsolePal.TerminalFormatStringsInstance.ClrEol ?? string.Empty;
215+
s_clearToEol ??= TerminalFormatStrings.s_instance.ClrEol ?? string.Empty;
216216

217217
// Move to end of previous line
218218
ConsolePal.SetTerminalCursorPosition(ConsolePal.WindowWidth - 1, top - 1);
@@ -223,7 +223,7 @@ private bool ReadLineCore(bool consumeKeys)
223223
{
224224
if (s_moveLeftString == null)
225225
{
226-
string? moveLeft = ConsolePal.TerminalFormatStringsInstance.CursorLeft;
226+
string? moveLeft = TerminalFormatStrings.s_instance.CursorLeft;
227227
s_moveLeftString = !string.IsNullOrEmpty(moveLeft) ? moveLeft + " " + moveLeft : string.Empty;
228228
}
229229

@@ -247,7 +247,7 @@ private bool ReadLineCore(bool consumeKeys)
247247
_readLineSB.Clear();
248248
if (freshKeys)
249249
{
250-
ConsolePal.WriteTerminalAnsiString(ConsolePal.TerminalFormatStringsInstance.Clear);
250+
ConsolePal.WriteTerminalAnsiString(TerminalFormatStrings.s_instance.Clear);
251251
}
252252
}
253253
else if (keyInfo.KeyChar != '\0')
@@ -365,7 +365,7 @@ private unsafe ConsoleKeyInfo ReadKey()
365365
}
366366
}
367367

368-
return KeyParser.Parse(_unprocessedBufferToBeRead, ConsolePal.TerminalFormatStringsInstance, ConsolePal.s_posixDisableValue, ConsolePal.s_veraseCharacter, ref _startIndex, _endIndex);
368+
return KeyParser.Parse(_unprocessedBufferToBeRead, TerminalFormatStrings.s_instance, ConsolePal.s_posixDisableValue, ConsolePal.s_veraseCharacter, ref _startIndex, _endIndex);
369369
}
370370
finally
371371
{

‎src/libraries/System.Console/src/System/TerminalFormatStrings.Unix.cs‎

Lines changed: 0 additions & 11 deletions
This file was deleted.

‎src/libraries/System.Console/src/System/TerminalFormatStrings.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
namespace System;
88

99
/// <summary>Provides format strings and related information for use with the current terminal.</summary>
10-
internal sealed partial class TerminalFormatStrings
10+
internal sealed class TerminalFormatStrings
1111
{
12+
#if !TARGET_WINDOWS
13+
/// <summary>The cached instance for the current terminal.</summary>
14+
internal static readonly TerminalFormatStrings s_instance = new(TermInfo.DatabaseFactory.ReadActiveDatabase());
15+
#endif
16+
1217
/// <summary>The format string to use to change the foreground color.</summary>
1318
public readonly string? Foreground;
1419
/// <summary>The format string to use to change the background color.</summary>

‎src/libraries/System.Console/tests/System.Console.Tests.csproj‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<IncludeRemoteExecutor>true</IncludeRemoteExecutor>
55
<TargetFrameworks>$(NetCoreAppCurrent);$(NetCoreAppCurrent)-windows</TargetFrameworks>
66
<StringResourcesPath>..\src\Resources\Strings.resx</StringResourcesPath>
7+
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
8+
<DefineConstants Condition="'$(TargetPlatformIdentifier)' == 'windows'">$(DefineConstants);TARGET_WINDOWS</DefineConstants>
79
</PropertyGroup>
810
<ItemGroup>
911
<Compile Include="BeepByBellChar.cs" />

0 commit comments

Comments
 (0)