Skip to content

Add class for detecting information about console in extensible way #4010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: v2_develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;

namespace Terminal.Gui;

/// <summary>
/// Attempts to determine information about the terminal and what features it
/// does/not support based on runtime operations e.g. registry etc
/// </summary>
internal class ConsoleFeatureFinder
{
public ConsoleFeatureFinderResults GetResults ()
{
var results = new ConsoleFeatureFinderResults ();

PlatformID p = Environment.OSVersion.Platform;
results.IsWindows = p is PlatformID.Win32NT or PlatformID.Win32S or PlatformID.Win32Windows;

if (results.IsWindows)
{
DetectWindowsSpecificFeatures (results.Windows);
}

return results;
}

private void DetectWindowsSpecificFeatures (WindowsFeatureSet windowsFeatures)
{
windowsFeatures.ConHostLegacyMode = IsLegacyConsoleEnabled ();
}

bool IsLegacyConsoleEnabled ()
{
try
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey (@"Console"))
{
if (key != null)
{
object value = key.GetValue ("ForceV2");
if (value is int intValue)
{
return intValue == 0; // Legacy Mode enabled if ForceV2 is 0
}
}
}
}
catch (Exception ex)
{
Logging.Warning ("Error reading registry: " + ex.Message);
}

return false; // Assume new console mode if check fails
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Terminal.Gui;

/// <summary>
/// Results of console feature detection
/// </summary>
internal class ConsoleFeatureFinderResults
{
public WindowsFeatureSet Windows { get; set; } = new WindowsFeatureSet();
public bool IsWindows { get; set; }

/// <inheritdoc />
public override string ToString ()
{
return $"{nameof(IsWindows)}:{IsWindows} {nameof(Windows)}:{Windows}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using static Unix.Terminal.Curses;

namespace Terminal.Gui;

/// <summary>
/// Features specific to the windows operating system
/// </summary>
internal class WindowsFeatureSet
{

public bool ConHostLegacyMode { get; set; }


public override string ToString () { return $"{nameof(ConHostLegacyMode)}:{ConHostLegacyMode}"; }
}
8 changes: 6 additions & 2 deletions Terminal.Gui/ConsoleDrivers/V2/ApplicationV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ public override void Init (IConsoleDriver? driver = null, string? driverName = n

private void CreateDriver (string? driverName)
{
PlatformID p = Environment.OSVersion.Platform;

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

var finder = new ConsoleFeatureFinder ();
var result = finder.GetResults ();

Logging.Logger.LogInformation ($"Feature detection results:{ result}");

if (definetlyWin)
{
_coordinator = CreateWindowsSubcomponents ();
Expand All @@ -98,7 +102,7 @@ private void CreateDriver (string? driverName)
{
_coordinator = CreateNetSubcomponents ();
}
else if (p == PlatformID.Win32NT || p == PlatformID.Win32S || p == PlatformID.Win32Windows)
else if (result.IsWindows)
{
_coordinator = CreateWindowsSubcomponents ();
}
Expand Down
Loading