From c17d61b3f07559963ce69fa682acf593e1bb1d7f Mon Sep 17 00:00:00 2001 From: Stone_Red <56473591+Stone-Red-Code@users.noreply.github.com> Date: Fri, 14 Oct 2022 14:48:28 +0200 Subject: [PATCH] Code cleanup and more doc --- Stone_Red-C-Sharp-Utilities-Test/Program.cs | 40 ++++--------- Stone_Red-C-Sharp-Utilities/BoolExtentions.cs | 6 +- .../ConsoleExtentions.cs | 57 ++++++++++++------- .../FluentMath/TypeExtentions/SingleFluent.cs | 2 +- .../Logging/LogConfig.cs | 4 +- .../Logging/LogFormatType.cs | 2 +- .../Logging/LogTarget.cs | 3 +- .../Logging/Logging.cs | 18 +++--- .../StringExtentions.cs | 14 ++--- 9 files changed, 76 insertions(+), 70 deletions(-) diff --git a/Stone_Red-C-Sharp-Utilities-Test/Program.cs b/Stone_Red-C-Sharp-Utilities-Test/Program.cs index 52f66b3..177bc70 100644 --- a/Stone_Red-C-Sharp-Utilities-Test/Program.cs +++ b/Stone_Red-C-Sharp-Utilities-Test/Program.cs @@ -1,15 +1,15 @@ -using Stone_Red_Utilities.Http; +using Stone_Red_C_Sharp_Utilities; +using Stone_Red_C_Sharp_Utilities.Logging; + using Stone_Red_Utilities.Logging; -using Stone_Red_Utilities.Reflection; using System; -using System.Net.Http; -using System.Text.Json.Serialization; +using System.Threading; using System.Threading.Tasks; namespace Stone_Red_C_Sharp_Utilities_Test { - internal class Program + internal static class Program { private static readonly Logger logger = new Logger() { @@ -42,36 +42,20 @@ namespace Stone_Red_C_Sharp_Utilities_Test }, FormatConfig = new FormatConfig() { - ConsoleFormat = $"{{{LogFormatType.DateTime}:hh:mm:ss}} | {{{LogFormatType.LogSeverity},-5}} | {{{LogFormatType.Message}}}" + ConsoleFormat = $"{{{LogFormatType.DateTime}:HH:mm:ss}} | {{{LogFormatType.LogSeverity},-5}} | {{{LogFormatType.Message}}}" } } }; private static async Task Main() { - HttpClient httpClient = new HttpClient(); - Quote quote = await httpClient.GetJsonObjectAsync("https://api.quotable.io/random"); - Console.WriteLine(quote.Id); - Console.WriteLine(quote.Content); - Console.WriteLine(quote.Author); + TaskQueue taskQueue = new TaskQueue(); + await taskQueue.Enqueue(() => Console.WriteLine("YES1")); + await taskQueue.Enqueue(() => Console.WriteLine("YES2")); + await taskQueue.Enqueue(() => Thread.Sleep(2000)); + await taskQueue.Enqueue(() => Console.WriteLine("YES3")); - Quote q = quote.CopyProperties(); - - Console.WriteLine(q.Id); - Console.WriteLine(q.Content); - Console.WriteLine(q.Author); - } - - private class Quote - { - [JsonPropertyName("_id")] - public string Id { get; set; } - - [JsonPropertyName("content")] - public string Content { get; set; } - - [JsonPropertyName("author")] - public string Author { get; set; } + logger.Log("wow", LogSeverity.Info); } } } \ No newline at end of file diff --git a/Stone_Red-C-Sharp-Utilities/BoolExtentions.cs b/Stone_Red-C-Sharp-Utilities/BoolExtentions.cs index fbccf40..f920099 100644 --- a/Stone_Red-C-Sharp-Utilities/BoolExtentions.cs +++ b/Stone_Red-C-Sharp-Utilities/BoolExtentions.cs @@ -1,4 +1,4 @@ -namespace Stone_Red_Utilities.BoolExtentions +namespace Stone_Red_C_Sharp_Utilities { /// /// Extensions @@ -12,7 +12,7 @@ /// public static void OneWayTrue(this ref bool bol, bool input) { - if (bol == false && input) + if (!bol && input) { bol = true; } @@ -25,7 +25,7 @@ /// public static void OneWayFalse(this ref bool bol, bool input) { - if (bol == true && !input) + if (bol && !input) { bol = false; } diff --git a/Stone_Red-C-Sharp-Utilities/ConsoleExtentions.cs b/Stone_Red-C-Sharp-Utilities/ConsoleExtentions.cs index bc87038..0cb96cd 100644 --- a/Stone_Red-C-Sharp-Utilities/ConsoleExtentions.cs +++ b/Stone_Red-C-Sharp-Utilities/ConsoleExtentions.cs @@ -1,7 +1,9 @@ using System; using System.ComponentModel; -namespace Stone_Red_Utilities.ConsoleExtentions +#pragma warning disable S3998 // Threads should not lock on objects with weak identity + +namespace Stone_Red_C_Sharp_Utilities { /// /// Extensions @@ -40,21 +42,27 @@ namespace Stone_Red_Utilities.ConsoleExtentions } } + /// + /// Reads the next line of characters from the standard input stream and tries to convert it to the specified type. + /// + /// + /// The input string converted to the specified type. + /// public static T ReadLine() { string attemptedValue = Console.ReadLine(); Type type = typeof(T); TypeConverter converter = TypeDescriptor.GetConverter(type); - if (converter != null && converter.IsValid(attemptedValue)) - { - return (T)converter.ConvertFromString(attemptedValue); - } - else - { - return default; - } + + return (T)converter.ConvertFromString(attemptedValue); } + /// + /// Reads the next line of characters from the standard input stream and tries to convert it to the specified type. + /// + /// + /// The input string converted to the specified type. + /// if the conversion was successful. Otherwise public static bool TryReadLine(out T input) { string attemptedValue = Console.ReadLine(); @@ -72,27 +80,36 @@ namespace Stone_Red_Utilities.ConsoleExtentions } } + /// + /// Obtains the next character or function key pressed by the user and converts it to the specified type. + /// The pressed key is displayed in the console window. + /// + /// The type of the + /// The input character converted to the specified type. + /// public static T ReadKey() { string attemptedValue = Console.ReadKey().KeyChar.ToString(); Type type = typeof(T); TypeConverter converter = TypeDescriptor.GetConverter(type); - if (converter != null && converter.IsValid(attemptedValue)) - { - return (T)converter.ConvertFromString(attemptedValue); - } - else - { - return default; - } + + return (T)converter.ConvertFromString(attemptedValue); } + /// + /// Obtains the next character or function key pressed by the user and tries to convert it to the specified type. + /// The pressed key is displayed in the console window. + /// + /// The input character converted to the specified type. + /// The type of the + /// if the conversion was successful. Otherwise public static bool TryReadKey(out T input) { string attemptedValue = Console.ReadKey().KeyChar.ToString(); Type type = typeof(T); TypeConverter converter = TypeDescriptor.GetConverter(type); if (converter != null && converter.IsValid(attemptedValue)) + { input = (T)converter.ConvertFromString(attemptedValue); return true; @@ -112,8 +129,10 @@ namespace Stone_Red_Utilities.ConsoleExtentions public static void Pause(ConsoleKey key, string message = null) { Console.WriteLine(message ?? $"Press {key} to continue..."); - while (Console.ReadKey(true).Key != key) + ConsoleKey? consoleKey = null; + while (consoleKey != key) { + consoleKey = Console.ReadKey(true).Key; } } @@ -124,7 +143,7 @@ namespace Stone_Red_Utilities.ConsoleExtentions public static void Pause(string message = "Press any key to continue...") { Console.WriteLine(message); - Console.ReadKey(true); + _ = Console.ReadKey(true); } } } \ No newline at end of file diff --git a/Stone_Red-C-Sharp-Utilities/FluentMath/TypeExtentions/SingleFluent.cs b/Stone_Red-C-Sharp-Utilities/FluentMath/TypeExtentions/SingleFluent.cs index f2d2a80..67c4d0d 100644 --- a/Stone_Red-C-Sharp-Utilities/FluentMath/TypeExtentions/SingleFluent.cs +++ b/Stone_Red-C-Sharp-Utilities/FluentMath/TypeExtentions/SingleFluent.cs @@ -24,7 +24,7 @@ namespace Stone_Red_Utilities.FluentMath /// Number as public static double ToDouble(this float num) { - return (float)num; + return num; } /// diff --git a/Stone_Red-C-Sharp-Utilities/Logging/LogConfig.cs b/Stone_Red-C-Sharp-Utilities/Logging/LogConfig.cs index 18f5dde..e1e743c 100644 --- a/Stone_Red-C-Sharp-Utilities/Logging/LogConfig.cs +++ b/Stone_Red-C-Sharp-Utilities/Logging/LogConfig.cs @@ -1,4 +1,6 @@ -using System; +using Stone_Red_C_Sharp_Utilities.Logging; + +using System; namespace Stone_Red_Utilities.Logging { diff --git a/Stone_Red-C-Sharp-Utilities/Logging/LogFormatType.cs b/Stone_Red-C-Sharp-Utilities/Logging/LogFormatType.cs index d3947d1..964b960 100644 --- a/Stone_Red-C-Sharp-Utilities/Logging/LogFormatType.cs +++ b/Stone_Red-C-Sharp-Utilities/Logging/LogFormatType.cs @@ -3,7 +3,7 @@ /// /// Specifies the info type of the log message format. /// - public class LogFormatType + public static class LogFormatType { /// /// The of the log message. diff --git a/Stone_Red-C-Sharp-Utilities/Logging/LogTarget.cs b/Stone_Red-C-Sharp-Utilities/Logging/LogTarget.cs index 49ae29c..629d923 100644 --- a/Stone_Red-C-Sharp-Utilities/Logging/LogTarget.cs +++ b/Stone_Red-C-Sharp-Utilities/Logging/LogTarget.cs @@ -1,8 +1,9 @@ -namespace Stone_Red_Utilities.Logging +namespace Stone_Red_C_Sharp_Utilities.Logging { /// /// Specifies the target of the log message. /// + [System.Flags] public enum LogTarget { /// diff --git a/Stone_Red-C-Sharp-Utilities/Logging/Logging.cs b/Stone_Red-C-Sharp-Utilities/Logging/Logging.cs index 3bfb2e2..0ae0690 100644 --- a/Stone_Red-C-Sharp-Utilities/Logging/Logging.cs +++ b/Stone_Red-C-Sharp-Utilities/Logging/Logging.cs @@ -1,11 +1,11 @@ -using Stone_Red_Utilities.ConsoleExtentions; +using Stone_Red_Utilities.Logging; using System; using System.Diagnostics; using System.IO; using System.Runtime.CompilerServices; -namespace Stone_Red_Utilities.Logging +namespace Stone_Red_C_Sharp_Utilities.Logging { /// /// Class used for logging @@ -137,13 +137,13 @@ namespace Stone_Red_Utilities.Logging private string GetFormattedString(string format, LogSeverity logSeverity, string source, string message, string memberName, string sourceFilePath, int sourceLineNumber) { format = format - .Replace($"{LogFormatType.DateTime}", "0") - .Replace($"{LogFormatType.LogSeverity}", "1") - .Replace($"{LogFormatType.LineNumber}", "2") - .Replace($"{LogFormatType.FilePath}", "3") - .Replace($"{LogFormatType.MemberName}", "4") - .Replace($"{LogFormatType.Source}", "5") - .Replace($"{LogFormatType.Message}", "6"); + .Replace(LogFormatType.DateTime, "0") + .Replace(LogFormatType.LogSeverity, "1") + .Replace(LogFormatType.LineNumber, "2") + .Replace(LogFormatType.FilePath, "3") + .Replace(LogFormatType.MemberName, "4") + .Replace(LogFormatType.Source, "5") + .Replace(LogFormatType.Message, "6"); return string.Format(format, DateTime.Now, logSeverity.ToString().ToUpper(), sourceLineNumber, sourceFilePath, memberName, source, message); } diff --git a/Stone_Red-C-Sharp-Utilities/StringExtentions.cs b/Stone_Red-C-Sharp-Utilities/StringExtentions.cs index 06d6426..1948192 100644 --- a/Stone_Red-C-Sharp-Utilities/StringExtentions.cs +++ b/Stone_Red-C-Sharp-Utilities/StringExtentions.cs @@ -3,7 +3,7 @@ using System.Globalization; using System.IO; using System.Text; -namespace Stone_Red_Utilities.StringExtentions +namespace Stone_Red_C_Sharp_Utilities { /// /// Extensions @@ -71,7 +71,7 @@ namespace Stone_Red_Utilities.StringExtentions UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); if (unicodeCategory != UnicodeCategory.NonSpacingMark) { - stringBuilder.Append(c); + _ = stringBuilder.Append(c); } } @@ -106,7 +106,7 @@ namespace Stone_Red_Utilities.StringExtentions UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); if (unicodeCategory != UnicodeCategory.NonSpacingMark) { - stringBuilder.Append(c); + _ = stringBuilder.Append(c); } } @@ -123,7 +123,7 @@ namespace Stone_Red_Utilities.StringExtentions { if (str.Length > length && length > 0) { - return str.Substring(0, length); + return str[..length]; } return str; @@ -142,11 +142,11 @@ namespace Stone_Red_Utilities.StringExtentions { if (ellipsis && length > 3) { - return $"{str.Substring(0, length - 3)}..."; + return $"{str[..(length - 3)]}..."; } else { - return str.Substring(0, length); + return str[..length]; } } @@ -184,7 +184,7 @@ namespace Stone_Red_Utilities.StringExtentions { if (!char.IsWhiteSpace(c)) { - result.Append(c); + _ = result.Append(c); } } return result.ToString();