Code cleanup and more doc

This commit is contained in:
Stone_Red
2022-10-14 14:48:28 +02:00
parent 69ee283950
commit c17d61b3f0
9 changed files with 76 additions and 70 deletions
+12 -28
View File
@@ -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.Logging;
using Stone_Red_Utilities.Reflection;
using System; using System;
using System.Net.Http; using System.Threading;
using System.Text.Json.Serialization;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Stone_Red_C_Sharp_Utilities_Test namespace Stone_Red_C_Sharp_Utilities_Test
{ {
internal class Program internal static class Program
{ {
private static readonly Logger logger = new Logger() private static readonly Logger logger = new Logger()
{ {
@@ -42,36 +42,20 @@ namespace Stone_Red_C_Sharp_Utilities_Test
}, },
FormatConfig = new FormatConfig() 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() private static async Task Main()
{ {
HttpClient httpClient = new HttpClient(); TaskQueue taskQueue = new TaskQueue();
Quote quote = await httpClient.GetJsonObjectAsync<Quote>("https://api.quotable.io/random"); await taskQueue.Enqueue(() => Console.WriteLine("YES1"));
Console.WriteLine(quote.Id); await taskQueue.Enqueue(() => Console.WriteLine("YES2"));
Console.WriteLine(quote.Content); await taskQueue.Enqueue(() => Thread.Sleep(2000));
Console.WriteLine(quote.Author); await taskQueue.Enqueue(() => Console.WriteLine("YES3"));
Quote q = quote.CopyProperties<Quote>(); logger.Log("wow", LogSeverity.Info);
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; }
} }
} }
} }
@@ -1,4 +1,4 @@
namespace Stone_Red_Utilities.BoolExtentions namespace Stone_Red_C_Sharp_Utilities
{ {
/// <summary> /// <summary>
/// <see cref="bool"/> Extensions /// <see cref="bool"/> Extensions
@@ -12,7 +12,7 @@
/// <param name="input"></param> /// <param name="input"></param>
public static void OneWayTrue(this ref bool bol, bool input) public static void OneWayTrue(this ref bool bol, bool input)
{ {
if (bol == false && input) if (!bol && input)
{ {
bol = true; bol = true;
} }
@@ -25,7 +25,7 @@
/// <param name="input"></param> /// <param name="input"></param>
public static void OneWayFalse(this ref bool bol, bool input) public static void OneWayFalse(this ref bool bol, bool input)
{ {
if (bol == true && !input) if (bol && !input)
{ {
bol = false; bol = false;
} }
@@ -1,7 +1,9 @@
using System; using System;
using System.ComponentModel; 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
{ {
/// <summary> /// <summary>
/// <see cref="Console"/> Extensions /// <see cref="Console"/> Extensions
@@ -40,21 +42,27 @@ namespace Stone_Red_Utilities.ConsoleExtentions
} }
} }
/// <summary>
/// Reads the next line of characters from the standard input stream and tries to convert it to the specified type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns>The input string converted to the specified type.</returns>
/// <exception cref="NotSupportedException"></exception>
public static T ReadLine<T>() public static T ReadLine<T>()
{ {
string attemptedValue = Console.ReadLine(); string attemptedValue = Console.ReadLine();
Type type = typeof(T); Type type = typeof(T);
TypeConverter converter = TypeDescriptor.GetConverter(type); TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter != null && converter.IsValid(attemptedValue))
{ return (T)converter.ConvertFromString(attemptedValue);
return (T)converter.ConvertFromString(attemptedValue);
}
else
{
return default;
}
} }
/// <summary>
/// Reads the next line of characters from the standard input stream and tries to convert it to the specified type.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="input">The input string converted to the specified type.</param>
/// <returns><see langword="true"/> if the conversion was successful. Otherwise <see langword="false"/></returns>
public static bool TryReadLine<T>(out T input) public static bool TryReadLine<T>(out T input)
{ {
string attemptedValue = Console.ReadLine(); string attemptedValue = Console.ReadLine();
@@ -72,27 +80,36 @@ namespace Stone_Red_Utilities.ConsoleExtentions
} }
} }
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="T">The type of the </typeparam>
/// <returns>The input character converted to the specified type.</returns>
/// <exception cref="NotSupportedException"></exception>
public static T ReadKey<T>() public static T ReadKey<T>()
{ {
string attemptedValue = Console.ReadKey().KeyChar.ToString(); string attemptedValue = Console.ReadKey().KeyChar.ToString();
Type type = typeof(T); Type type = typeof(T);
TypeConverter converter = TypeDescriptor.GetConverter(type); TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter != null && converter.IsValid(attemptedValue))
{ return (T)converter.ConvertFromString(attemptedValue);
return (T)converter.ConvertFromString(attemptedValue);
}
else
{
return default;
}
} }
/// <summary>
/// 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.
/// </summary>
/// <param name="input">The input character converted to the specified type.</param>
/// <typeparam name="T">The type of the </typeparam>
/// <returns><see langword="true"/> if the conversion was successful. Otherwise <see langword="false"/></returns>
public static bool TryReadKey<T>(out T input) public static bool TryReadKey<T>(out T input)
{ {
string attemptedValue = Console.ReadKey().KeyChar.ToString(); string attemptedValue = Console.ReadKey().KeyChar.ToString();
Type type = typeof(T); Type type = typeof(T);
TypeConverter converter = TypeDescriptor.GetConverter(type); TypeConverter converter = TypeDescriptor.GetConverter(type);
if (converter != null && converter.IsValid(attemptedValue)) if (converter != null && converter.IsValid(attemptedValue))
{ {
input = (T)converter.ConvertFromString(attemptedValue); input = (T)converter.ConvertFromString(attemptedValue);
return true; return true;
@@ -112,8 +129,10 @@ namespace Stone_Red_Utilities.ConsoleExtentions
public static void Pause(ConsoleKey key, string message = null) public static void Pause(ConsoleKey key, string message = null)
{ {
Console.WriteLine(message ?? $"Press {key} to continue..."); 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...") public static void Pause(string message = "Press any key to continue...")
{ {
Console.WriteLine(message); Console.WriteLine(message);
Console.ReadKey(true); _ = Console.ReadKey(true);
} }
} }
} }
@@ -24,7 +24,7 @@ namespace Stone_Red_Utilities.FluentMath
/// <returns>Number as <see cref="double"/></returns> /// <returns>Number as <see cref="double"/></returns>
public static double ToDouble(this float num) public static double ToDouble(this float num)
{ {
return (float)num; return num;
} }
/// <summary> /// <summary>
@@ -1,4 +1,6 @@
using System; using Stone_Red_C_Sharp_Utilities.Logging;
using System;
namespace Stone_Red_Utilities.Logging namespace Stone_Red_Utilities.Logging
{ {
@@ -3,7 +3,7 @@
/// <summary> /// <summary>
/// Specifies the info type of the log message format. /// Specifies the info type of the log message format.
/// </summary> /// </summary>
public class LogFormatType public static class LogFormatType
{ {
/// <summary> /// <summary>
/// The <see cref="System.DateTime"/> of the log message. /// The <see cref="System.DateTime"/> of the log message.
@@ -1,8 +1,9 @@
namespace Stone_Red_Utilities.Logging namespace Stone_Red_C_Sharp_Utilities.Logging
{ {
/// <summary> /// <summary>
/// Specifies the target of the log message. /// Specifies the target of the log message.
/// </summary> /// </summary>
[System.Flags]
public enum LogTarget public enum LogTarget
{ {
/// <summary> /// <summary>
@@ -1,11 +1,11 @@
using Stone_Red_Utilities.ConsoleExtentions; using Stone_Red_Utilities.Logging;
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
namespace Stone_Red_Utilities.Logging namespace Stone_Red_C_Sharp_Utilities.Logging
{ {
/// <summary> /// <summary>
/// Class used for 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) private string GetFormattedString(string format, LogSeverity logSeverity, string source, string message, string memberName, string sourceFilePath, int sourceLineNumber)
{ {
format = format format = format
.Replace($"{LogFormatType.DateTime}", "0") .Replace(LogFormatType.DateTime, "0")
.Replace($"{LogFormatType.LogSeverity}", "1") .Replace(LogFormatType.LogSeverity, "1")
.Replace($"{LogFormatType.LineNumber}", "2") .Replace(LogFormatType.LineNumber, "2")
.Replace($"{LogFormatType.FilePath}", "3") .Replace(LogFormatType.FilePath, "3")
.Replace($"{LogFormatType.MemberName}", "4") .Replace(LogFormatType.MemberName, "4")
.Replace($"{LogFormatType.Source}", "5") .Replace(LogFormatType.Source, "5")
.Replace($"{LogFormatType.Message}", "6"); .Replace(LogFormatType.Message, "6");
return string.Format(format, DateTime.Now, logSeverity.ToString().ToUpper(), sourceLineNumber, sourceFilePath, memberName, source, message); return string.Format(format, DateTime.Now, logSeverity.ToString().ToUpper(), sourceLineNumber, sourceFilePath, memberName, source, message);
} }
@@ -3,7 +3,7 @@ using System.Globalization;
using System.IO; using System.IO;
using System.Text; using System.Text;
namespace Stone_Red_Utilities.StringExtentions namespace Stone_Red_C_Sharp_Utilities
{ {
/// <summary> /// <summary>
/// <see cref="string"/> Extensions /// <see cref="string"/> Extensions
@@ -71,7 +71,7 @@ namespace Stone_Red_Utilities.StringExtentions
UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark) if (unicodeCategory != UnicodeCategory.NonSpacingMark)
{ {
stringBuilder.Append(c); _ = stringBuilder.Append(c);
} }
} }
@@ -106,7 +106,7 @@ namespace Stone_Red_Utilities.StringExtentions
UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); UnicodeCategory unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
if (unicodeCategory != UnicodeCategory.NonSpacingMark) 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) if (str.Length > length && length > 0)
{ {
return str.Substring(0, length); return str[..length];
} }
return str; return str;
@@ -142,11 +142,11 @@ namespace Stone_Red_Utilities.StringExtentions
{ {
if (ellipsis && length > 3) if (ellipsis && length > 3)
{ {
return $"{str.Substring(0, length - 3)}..."; return $"{str[..(length - 3)]}...";
} }
else else
{ {
return str.Substring(0, length); return str[..length];
} }
} }
@@ -184,7 +184,7 @@ namespace Stone_Red_Utilities.StringExtentions
{ {
if (!char.IsWhiteSpace(c)) if (!char.IsWhiteSpace(c))
{ {
result.Append(c); _ = result.Append(c);
} }
} }
return result.ToString(); return result.ToString();