Skip to content

Commit fc077e2

Browse files
committed
Ported some of Volte's extensions.
1 parent 0f93b65 commit fc077e2

File tree

4 files changed

+176
-17
lines changed

4 files changed

+176
-17
lines changed

.github/workflows/main.yml

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,31 @@
1-
name: CI
1+
name: Volte CI
22

3-
on: [push]
3+
on:
4+
push:
5+
branches:
6+
- v4
7+
pull_request:
8+
branches:
9+
- v4
410

511
jobs:
612
build:
13+
runs-on: windows-latest
714

8-
runs-on: ubuntu-latest
9-
1015
steps:
11-
- uses: actions/checkout@v1
12-
- uses: actions/setup-dotnet@master
13-
with:
14-
dotnet-version: '3.0.100-preview3-010431'
15-
- name: build the damn project
16-
run: dotnet build
16+
- uses: actions/checkout@main
17+
- uses: actions/setup-dotnet@v1
18+
with:
19+
dotnet-version: '5.0.100-preview.7.20366.6'
20+
- name: Compile
21+
run: bash build.sh
22+
- name: Upload Linux binary
23+
uses: actions/upload-artifact@main
24+
with:
25+
name: Volte-Linux
26+
path: src/bin/Release/net5.0/linux-x64/publish/Volte
27+
- name: Upload Windows binary
28+
uses: actions/upload-artifact@main
29+
with:
30+
name: Volte-Windows
31+
path: src/bin/Release/net5.0/win10-x64/publish/Volte.exe

src/Gommon/Extensions/CollectionExtensions.cs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@ public static bool ContainsIgnoreCase(this IEnumerable<string> strings, string e
2626

2727
/// <summary>
2828
/// Effectively "filters" the current IEnumerable by removing duplicates by the return type of the <paramref name="selector"/>.
29+
/// Automatically removes null entries, which shouldn't happen.
2930
/// </summary>
3031
/// <typeparam name="T">In type.</typeparam>
3132
/// <typeparam name="TKey">Type to check.</typeparam>
3233
/// <param name="coll">Current IEnumerable.</param>
3334
/// <param name="selector">Selector function.</param>
3435
/// <returns>The filtered <code>IEnumerable&lt;<typeparam name="T"/>&gt;</code></returns>
3536
public static IEnumerable<T> DistinctBy<T, TKey>(this IEnumerable<T> coll, Func<T, TKey> selector)
36-
=> coll.GroupBy(selector).Select(x => x.FirstOrDefault());
37+
=> coll.GroupBy(selector).Select(x => x.FirstOrDefault()).Where(x => x != null);
3738

3839
/// <summary>
3940
/// Join the current string Enumerable by the given string <paramref name="separator"/>.
@@ -58,7 +59,7 @@ public static string Join(this IEnumerable<string> list, char separator)
5859
/// </summary>
5960
/// <param name="arr">Current array.</param>
6061
/// <returns>Random element in the current array.</returns>
61-
public static object Random(this object[] arr)
62+
public static T Random<T>(this T[] arr)
6263
=> arr[new Random().Next(0, arr.Length)];
6364

6465

@@ -74,17 +75,25 @@ public static void ForEach<T>(this IEnumerable<T> enumerable, Action<T> action)
7475
}
7576

7677
/// <summary>
77-
/// Converts any given Collection to a human-readable string.
78+
/// Converts the current IEnumerable to a human-readable string.
7879
/// </summary>
7980
/// <typeparam name="T">Type of the current Collection.</typeparam>
80-
/// <param name="coll">The current Collection.</param>
81+
/// <param name="coll">The current Enumerable.</param>
8182
/// <returns>A string representing the contents of the Collection.</returns>
8283

83-
public static string ToReadableString<T>(this ICollection<T> coll)
84+
public static string ToReadableString<T>(this IEnumerable<T> coll)
8485
{
85-
var stringColl = coll.Select(x => $"\"{x.ToString()}\"");
86+
var stringColl = coll.Select(x => $"\"{x}\"");
8687
return $"[{stringColl.Join(", ")}]";
87-
8888
}
89+
90+
/// <summary>
91+
/// Checks whether or not the current IEnumerable is empty.
92+
/// </summary>
93+
/// <param name="coll">The current Enumerable.</param>
94+
/// <typeparam name="T"></typeparam>
95+
/// <returns>True if the current IEnumerable has any elements; false otherwise.</returns>
96+
97+
public static bool IsEmpty<T>(this IEnumerable<T> coll) => !coll.Any();
8998
}
9099
}

src/Gommon/Extensions/HttpExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System.Net.Http;
2+
using System.Text;
3+
using System.Threading.Tasks;
24

35
namespace Gommon
46
{
@@ -11,5 +13,18 @@ public static partial class Extensions
1113
/// <returns><see cref="bool"/></returns>
1214
public static bool IsImage(this HttpResponseMessage msg)
1315
=> msg.Content.Headers.ContentType.MediaType.StartsWith("image/");
16+
17+
/// <summary>
18+
/// Uses the current <see cref="HttpClient"/> to send a <code>POST</code>
19+
/// request to the target <paramref name="url"/> with the JSON content in <paramref name="json"/>.
20+
/// </summary>
21+
/// <param name="http">The current HTTP Client.</param>
22+
/// <param name="url">The target URL</param>
23+
/// <param name="json">JSON content to send</param>
24+
/// <returns>The resulting <see cref="HttpResponseMessage"/>.</returns>
25+
public static async Task<HttpResponseMessage> PostJsonAsync(this HttpClient http, string url, string json)
26+
{
27+
return await http.PostAsync(url, new StringContent(json, Encoding.UTF8, "application/json"));
28+
}
1429
}
1530
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Text.RegularExpressions;
7+
8+
namespace Gommon
9+
{
10+
public partial class Extensions
11+
{
12+
private const int MemoryTierSize = 1024;
13+
14+
/// <summary>
15+
/// Formats a type to a pretty .NET-styled type name.
16+
/// </summary>
17+
/// <param name="type">The current Type.</param>
18+
/// <returns>A pretty string that shows what this type is.</returns>
19+
public static string AsPrettyString(this Type type)
20+
{
21+
string FormatTypeName(Type t)
22+
{
23+
switch (t.Name)
24+
{
25+
case "Boolean": return "bool";
26+
case "Byte": return "byte";
27+
case "SByte": return "sbyte";
28+
case "Int16": return "short";
29+
case "UInt16": return "ushort";
30+
case "Int32": return "int";
31+
case "UInt32": return "uint";
32+
case "Int64": return "long";
33+
case "UInt64": return "ulong";
34+
case "Char": return "char";
35+
case "String": return "string";
36+
default: return t.Name;
37+
}
38+
}
39+
40+
var types = type.GenericTypeArguments;
41+
42+
//thanks .NET for putting an annoying ass backtick and number at the end of type names.
43+
var vs = FormatTypeName(type).Replace($"`{types.Length}", "");
44+
45+
if (!types.IsEmpty()) vs += $"<{types.Select(FormatTypeName).Join(", ")}>";
46+
47+
return vs;
48+
}
49+
50+
/// <summary>
51+
/// Gets all flags in the current enum.
52+
/// </summary>
53+
/// <param name="input">The current enum.</param>
54+
/// <typeparam name="T">The enum's type.</typeparam>
55+
/// <returns>A collection of all the current Enum's members.</returns>
56+
public static IEnumerable<T> GetFlags<T>(this T input) where T : Enum
57+
{
58+
return Enumerable.Cast<T>(Enum.GetValues(input.GetType())).Where(e => input.HasFlag(e));
59+
}
60+
61+
/// <summary>
62+
/// Appends all elements in the specified string array as a line to the current StringBuilder.
63+
/// </summary>
64+
/// <param name="sb">The current StringBuilder.</param>
65+
/// <param name="lines">The lines to append.</param>
66+
/// <returns>The current StringBuilder for chaining convenience.</returns>
67+
public static StringBuilder AppendAllLines(this StringBuilder sb, params string[] lines)
68+
{
69+
foreach (var line in lines)
70+
{
71+
sb.AppendLine(line);
72+
}
73+
74+
return sb;
75+
}
76+
77+
/// <summary>
78+
/// Attempts to match a string against regex and the resulting match is the out variable.
79+
/// </summary>
80+
/// <param name="regex">The current regex.</param>
81+
/// <param name="str">The string to attempt to match to.</param>
82+
/// <param name="match">The resulting match.</param>
83+
/// <returns>True if it was a match and <paramref name="match"/> has a value; false otherwise.</returns>
84+
public static bool IsMatch(this Regex regex, string str, out Match match)
85+
{
86+
match = regex.Match(str);
87+
return match.Success;
88+
}
89+
90+
/// <summary>
91+
/// Gets the current process's memory usage as a pretty string. Can be shown in Bytes or all the way up to Terabytes via the <paramref name="memType"/> parameter.
92+
/// </summary>
93+
/// <param name="process">The current Process.</param>
94+
/// <param name="memType">The MemoryType to format the string to.</param>
95+
/// <returns>The formatted string.</returns>
96+
public static string GetMemoryUsage(this Process process, MemoryType memType = MemoryType.Megabytes)
97+
{
98+
var res = process.PrivateMemorySize64;
99+
switch (memType)
100+
{
101+
case MemoryType.Terabytes:
102+
return $"{res / MemoryTierSize / MemoryTierSize / MemoryTierSize / MemoryTierSize} TB";
103+
case MemoryType.Gigabytes: return $"{res / MemoryTierSize / MemoryTierSize / MemoryTierSize} GB";
104+
case MemoryType.Megabytes: return $"{res / MemoryTierSize / MemoryTierSize} MB";
105+
case MemoryType.Kilobytes: return $"{res / MemoryTierSize} KB";
106+
case MemoryType.Bytes: return $"{res} B";
107+
default: return "null";
108+
}
109+
}
110+
}
111+
112+
public enum MemoryType
113+
{
114+
Terabytes,
115+
Gigabytes,
116+
Megabytes,
117+
Kilobytes,
118+
Bytes
119+
}
120+
}

0 commit comments

Comments
 (0)