Skip to content

Commit

Permalink
Added equal checks. 3.1.3
Browse files Browse the repository at this point in the history
  • Loading branch information
dedouwe26 committed May 22, 2024
1 parent 5bd2248 commit 76334e5
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 63 deletions.
12 changes: 0 additions & 12 deletions Terminal/ANSI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,13 @@ public static class ANSI {
/// </summary>
public const string CSI = ESC+"[";

/// <summary>
///
/// </summary>
public const string EraseScreen = CSI+"2J";
/// <summary>
///
/// </summary>
public const string EraseScreenFromCursor = CSI+"0J";
/// <summary>
///
/// </summary>
public const string EraseLine = CSI+"2K";
/// <summary>
///
/// </summary>
public const string EraseLineFromCursor = CSI+"0K";
/// <summary>
/// Will return as CSI{row};{column}R <para/>
Expand All @@ -36,13 +28,9 @@ public static class ANSI {
/// {column} is the column.
/// </summary>
public const string RequestCursorPosition = CSI+"6n";
/// <summary>
///
/// </summary>
public const string CursorInvisible = CSI+"?25l";
/// <summary>
///
/// </summary>
public const string CursorVisible = CSI+"?25h";

/// <summary>
Expand Down
69 changes: 46 additions & 23 deletions Terminal/Color.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,27 @@
using System.Numerics;

namespace OxDED.Terminal;


/// <summary>
/// These are TERMINAL-DEFINED colors.
/// </summary>
public enum Colors : byte {
/// <summary>
///
/// </summary>
Black = 30,
/// <summary>
///
/// </summary>
Red = 31,
/// <summary>
///
/// </summary>
Green = 32,
/// <summary>
///
/// </summary>
Yellow = 33,
/// <summary>
///
/// </summary>
Blue = 34,
/// <summary>
///
/// </summary>
Magenta = 35,
/// <summary>
///
/// </summary>
Cyan = 36,
/// <summary>
///
/// </summary>
White = 37,

/// <summary>
Expand Down Expand Up @@ -80,7 +66,7 @@ public enum Colors : byte {
/// <summary>
/// Represents a color for a terminal.
/// </summary>
public class Color {
public class Color : IEquatable<Color> {
/// <summary>
/// The 8-bit (generated) table color.
/// </summary>
Expand Down Expand Up @@ -229,19 +215,56 @@ public string ToForegroundANSI() {
/// 255, 255, 255
/// </summary>
public static readonly Color White = new(255, 255, 255);
/// <summary>
///
/// </summary>
public static implicit operator Color(Colors color) { return new(color); }
/// <summary>
///
/// </summary>
public static explicit operator Colors?(Color color) { return color.paletteColor; }
/// <summary>
///
/// </summary>
public static explicit operator Colors(Color color) {
if (color.HasPaletteColor&&color.paletteColor.HasValue) { return color.paletteColor.Value; }
throw new Exception("No palette color.");
}
///
///
public static bool operator ==(Color? left, Color? right) {
if (left is null && right is null) {
return true;
} else if (left is null) {
return false;
}
return left.Equals(right);
}
///
public static bool operator !=(Color? left, Color? right) {
return !(left == right);
}
/// <inheritdoc/>
/// <remarks>
/// Checks if the that color is identical to this one.
/// </remarks>
public bool Equals(Color? other) {
if (other is null) {
return false;
}
if (ReferenceEquals(this, other)) {
return true;
}
if (GetType() != other.GetType()) {
return false;
}
return tableColor == other.tableColor &&
paletteColor == other.paletteColor &&
trueColor == other.trueColor;
}
/// <inheritdoc/>
/// <remarks>
/// Checks if the that color is identical to this one.
/// </remarks>
public override bool Equals(object? obj) {
return Equals(obj as Color);
}
/// <inheritdoc/>
public override int GetHashCode() {
return (tableColor ?? 0) ^ (paletteColor?.GetHashCode() ?? 0) ^ (trueColor?.GetHashCode() ?? 0);
}
}
43 changes: 42 additions & 1 deletion Terminal/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace OxDED.Terminal.Logging;
/// <summary>
/// Represents a logger for the terminal and log files.
/// </summary>
public class Logger : IDisposable {
public class Logger : IDisposable, IEquatable<Logger> {
/// <summary>
/// The ID of this logger.
/// </summary>
Expand Down Expand Up @@ -304,4 +304,45 @@ public void Dispose() {
~Logger() {
Dispose();
}

///
public static bool operator ==(Logger? left, Logger? right) {
if (left is null && right is null) {
return true;
} else if (left is null) {
return false;
}
return left.Equals(right);
}
///
public static bool operator !=(Logger? left, Logger? right) {
return !(left == right);
}
/// <inheritdoc/>
/// <remarks>
/// Checks if the that color is identical to this one.
/// </remarks>
public bool Equals(Logger? other) {
if (other is null) {
return false;
}
if (ReferenceEquals(this, other)) {
return true;
}
if (GetType() != other.GetType()) {
return false;
}
return ID == other.ID;
}
/// <inheritdoc/>
/// <remarks>
/// Checks if the that color is identical to this one.
/// </remarks>
public override bool Equals(object? obj) {
return Equals(obj as Color);
}
/// <inheritdoc/>
public override int GetHashCode() {
return ID.GetHashCode();
}
}
14 changes: 0 additions & 14 deletions Terminal/Logging/Severity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,18 @@ namespace OxDED.Terminal.Logging;
/// Logger severity.
/// </summary>
public enum Severity : byte {
/// <summary>
///
/// </summary>
Fatal,
/// <summary>
///
/// </summary>
Error,
/// <summary>
///
/// </summary>
Warning,
/// <summary>
///
/// </summary>
Message,
/// <summary>
///
/// </summary>
Info,
/// <summary>
///
/// </summary>
Debug,
/// <summary>
///
/// </summary>
Trace
}
74 changes: 62 additions & 12 deletions Terminal/Style.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace OxDED.Terminal;
/// <summary>
/// Contains the style decorations.
/// </summary>
public struct Style {
public class Style : IEquatable<Style> {
/// <summary>
/// Interferes with <see cref="Faint"/>.
/// </summary>
Expand All @@ -12,25 +12,15 @@ public struct Style {
/// Interferes with <see cref="Bold"/>.
/// </summary>
public bool Faint = false;
/// <summary>
///
/// </summary>
public bool Italic = false;
/// <summary>
///
/// </summary>
public bool Underline = false;
/// <summary>
///
/// </summary>
public bool Blink = false;
/// <summary>
///
/// </summary>
public bool Inverse = false;
/// <summary>
///
/// </summary>
public bool Invisible = false;
/// <summary>
/// </summary>
Expand Down Expand Up @@ -58,7 +48,7 @@ public Style() {}
/// Creates an ANSI coded string for the chosen decorations.
/// </summary>
/// <returns>The ANSI string.</returns>
public readonly string ToANSI() {
public string ToANSI() {
return
(Bold ? ANSI.Styles.Bold : "") +
(Faint ? ANSI.Styles.Faint : "") +
Expand All @@ -73,4 +63,64 @@ public readonly string ToANSI() {
((!(Underline||DoubleUnderline)) ? ANSI.Styles.ResetUnderline : "") +
BackgroundColor.ToBackgroundANSI() + ForegroundColor.ToForegroundANSI();
}

///
public static bool operator ==(Style? left, Style? right) {
if (left is null && right is null) {
return true;
} else if (left is null) {
return false;
}
return left.Equals(right);
}
///
public static bool operator !=(Style? left, Style? right) {
return !(left == right);
}
/// <inheritdoc/>
/// <remarks>
/// Checks if the that color is identical to this one.
/// </remarks>
public bool Equals(Style? other) {
if (other is null) {
return false;
}
if (ReferenceEquals(this, other)) {
return true;
}
if (GetType() != other.GetType()) {
return false;
}
return (Bold == other.Bold) &&
(Faint == other.Faint) &&
(Italic == other.Italic) &&
(Underline == other.Underline) &&
(Blink == other.Blink) &&
(Inverse == other.Inverse) &&
(Invisible == other.Invisible) &&
(Striketrough == other.Striketrough) &&
(DoubleUnderline == other.DoubleUnderline) &&
(ForegroundColor == other.ForegroundColor) &&
(BackgroundColor == other.BackgroundColor);
}
/// <inheritdoc/>
/// <remarks>
/// Checks if the that color is identical to this one.
/// </remarks>
public override bool Equals(object? obj) {
return Equals(obj as Color);
}
/// <inheritdoc/>
public override int GetHashCode() {
return (((((((Bold ? 1 : 0)
<< 1 ^ (Faint ? 1 : 0))
<< 1 ^ (Italic ? 1 : 0))
<< 1 ^ (Underline ? 1 : 0))
<< 1 ^ (Inverse ? 1 : 0))
<< 1 ^ (Invisible ? 1 : 0))
<< 1 ^ (Striketrough ? 1 : 0))
<< 1 ^ (DoubleUnderline ? 1 : 0)
^ ForegroundColor.GetHashCode()
^ ForegroundColor.GetHashCode();
}
}
2 changes: 1 addition & 1 deletion Terminal/Terminal.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PublishTrimmed>true</PublishTrimmed>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageId>0xDED.Terminal</PackageId>
<Version>3.1.2</Version>
<Version>3.1.3</Version>
<Authors>0xDED</Authors>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down

0 comments on commit 76334e5

Please sign in to comment.