Skip to content

Commit 7e4253c

Browse files
committed
Add class for detecting information about console in extensible way
1 parent 39d4c7d commit 7e4253c

File tree

4 files changed

+96
-2
lines changed

4 files changed

+96
-2
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.Win32;
7+
8+
namespace Terminal.Gui;
9+
10+
/// <summary>
11+
/// Attempts to determine information about the terminal and what features it
12+
/// does/not support based on runtime operations e.g. registry etc
13+
/// </summary>
14+
internal class ConsoleFeatureFinder
15+
{
16+
public ConsoleFeatureFinderResults GetResults ()
17+
{
18+
var results = new ConsoleFeatureFinderResults ();
19+
20+
PlatformID p = Environment.OSVersion.Platform;
21+
results.IsWindows = p is PlatformID.Win32NT or PlatformID.Win32S or PlatformID.Win32Windows;
22+
23+
if (results.IsWindows)
24+
{
25+
DetectWindowsSpecificFeatures (results.Windows);
26+
}
27+
28+
return results;
29+
}
30+
31+
private void DetectWindowsSpecificFeatures (WindowsFeatureSet windowsFeatures)
32+
{
33+
windowsFeatures.ConHostLegacyMode = IsLegacyConsoleEnabled ();
34+
}
35+
36+
bool IsLegacyConsoleEnabled ()
37+
{
38+
try
39+
{
40+
using (RegistryKey key = Registry.CurrentUser.OpenSubKey (@"Console"))
41+
{
42+
if (key != null)
43+
{
44+
object value = key.GetValue ("ForceV2");
45+
if (value is int intValue)
46+
{
47+
return intValue == 0; // Legacy Mode enabled if ForceV2 is 0
48+
}
49+
}
50+
}
51+
}
52+
catch (Exception ex)
53+
{
54+
Logging.Warning ("Error reading registry: " + ex.Message);
55+
}
56+
57+
return false; // Assume new console mode if check fails
58+
}
59+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace Terminal.Gui;
2+
3+
/// <summary>
4+
/// Results of console feature detection
5+
/// </summary>
6+
internal class ConsoleFeatureFinderResults
7+
{
8+
public WindowsFeatureSet Windows { get; set; } = new WindowsFeatureSet();
9+
public bool IsWindows { get; set; }
10+
11+
/// <inheritdoc />
12+
public override string ToString ()
13+
{
14+
return $"{nameof(IsWindows)}:{IsWindows} {nameof(Windows)}:{Windows}";
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using static Unix.Terminal.Curses;
2+
3+
namespace Terminal.Gui;
4+
5+
/// <summary>
6+
/// Features specific to the windows operating system
7+
/// </summary>
8+
internal class WindowsFeatureSet
9+
{
10+
11+
public bool ConHostLegacyMode { get; set; }
12+
13+
14+
public override string ToString () { return $"{nameof(ConHostLegacyMode)}:{ConHostLegacyMode}"; }
15+
}

Terminal.Gui/ConsoleDrivers/V2/ApplicationV2.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,15 @@ public override void Init (IConsoleDriver? driver = null, string? driverName = n
8585

8686
private void CreateDriver (string? driverName)
8787
{
88-
PlatformID p = Environment.OSVersion.Platform;
8988

9089
bool definetlyWin = driverName?.Contains ("win") ?? false;
9190
bool definetlyNet = driverName?.Contains ("net") ?? false;
9291

92+
var finder = new ConsoleFeatureFinder ();
93+
var result = finder.GetResults ();
94+
95+
Logging.Logger.LogInformation ($"Feature detection results:{ result}");
96+
9397
if (definetlyWin)
9498
{
9599
_coordinator = CreateWindowsSubcomponents ();
@@ -98,7 +102,7 @@ private void CreateDriver (string? driverName)
98102
{
99103
_coordinator = CreateNetSubcomponents ();
100104
}
101-
else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
105+
else if (result.IsWindows)
102106
{
103107
_coordinator = CreateWindowsSubcomponents ();
104108
}

0 commit comments

Comments
 (0)