Skip to content

Commit

Permalink
add - doc - Implemented check whitelists
Browse files Browse the repository at this point in the history
---

We've added the check whitelists. It's an alternative to the "no check" property.

---

Type: add
Breaking: False
Doc Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed Apr 27, 2024
1 parent 36362b9 commit 565c99c
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions Terminaux/Base/Checks/ConsoleChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
using Terminaux.Colors.Data;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Collections.Generic;
using Textify.General;

namespace Terminaux.Base.Checks
{
Expand All @@ -41,6 +43,8 @@ public static class ConsoleChecker
private static bool acknowledged = false;
private static bool _dumbSet = false;
private static bool _dumb = true;
private static readonly string[] whitelist = ["testhost", "Terminaux.Tests"];
private static readonly List<string> customWhitelist = [];

/// <summary>
/// Is the console a dumb console?
Expand Down Expand Up @@ -90,6 +94,17 @@ public static void CheckConsole()
if (acknowledged)
return;
busy = true;

// First, get the assembly for whitelist
var asm = Assembly.GetEntryAssembly();
if (asm is null || asm.FullName.ContainsAnyOf(whitelist) || asm.FullName.ContainsAnyOf([.. customWhitelist]))
{
busy = false;
acknowledged = true;
return;
}

// Now, check the type
string TerminalType = PlatformHelper.GetTerminalType();
string TerminalEmulator = PlatformHelper.GetTerminalEmulator();

Expand Down Expand Up @@ -143,15 +158,11 @@ public static void CheckConsole()
{
if (PlatformHelper.IsOnWindows())
{
var asm = Assembly.GetEntryAssembly();
if (asm is not null && !asm.FullName.Contains("testhost") && !asm.FullName.Contains("Terminaux.Tests"))
{
FastFail(
"User tried to run a Terminaux application on Git Bash's MinTTY without winpty.",
"You'll need to use winpty to be able to use this program. If you are sure that you're not running Git Bash, ensure that you're using a proper Windows terminal.",
ex
);
}
FastFail(
"User tried to run a Terminaux application on Git Bash's MinTTY without winpty.",
"You'll need to use winpty to be able to use this program. If you are sure that you're not running Git Bash, ensure that you're using a proper Windows terminal.",
ex
);
}
else
TextWriterColor.WriteColor("Console positioning is not working due to an I/O error, so this application might behave erratically.", ConsoleColors.Yellow);
Expand Down Expand Up @@ -298,6 +309,34 @@ public static bool IsConHost()
return conHost;
}

/// <summary>
/// Adds the assembly to the check whitelist
/// </summary>
/// <param name="asm">Assembly to add</param>
public static void AddToCheckWhitelist(Assembly asm)
{
if (asm is null)
throw new TerminauxException("Assembly not provided.");

// Add the partial name for the assembly to the whitelist
if (!customWhitelist.Contains(asm.FullName))
customWhitelist.Add(asm.FullName);
}

/// <summary>
/// Removes the assembly from the check whitelist
/// </summary>
/// <param name="asm">Assembly to remove</param>
public static void RemoveFromCheckWhitelist(Assembly asm)
{
if (asm is null)
throw new TerminauxException("Assembly not provided.");

// Add the partial name for the assembly to the whitelist
if (customWhitelist.Contains(asm.FullName))
customWhitelist.Remove(asm.FullName);
}

internal static void FastFail(string eventMessage, string description, Exception? ex = null)
{
TextWriterColor.WriteColor($"{description} Can't continue.", ConsoleColors.Red);
Expand Down

0 comments on commit 565c99c

Please sign in to comment.