-
Notifications
You must be signed in to change notification settings - Fork 6
/
Sample.cs
138 lines (114 loc) · 3.86 KB
/
Sample.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Licensed under the Apache License, Version 2.0 (the "License").
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace HIDDevices.Sample;
public abstract class Sample : ISample
{
private readonly SimpleConsoleLogger<Sample> _logger;
private readonly HashSet<string> _shortNames;
protected Sample(string? fullName = null, params string[] shortNames)
{
FullName = fullName ?? GetFullName(GetType().Name);
_shortNames = shortNames.Length > 0 ? new HashSet<string>(shortNames) : GetShortNames(FullName);
_logger = new SimpleConsoleLogger<Sample>(LogLevel.Information, GetType().Name);
}
protected ILogger Logger => _logger;
public LogLevel LogLevel { get => _logger.LogLevel; set => _logger.LogLevel = value; }
/// <inheritdoc />
public string FullName { get; }
/// <inheritdoc />
public IReadOnlyCollection<string> ShortNames => _shortNames;
/// <inheritdoc />
public virtual Task ExecuteAsync(CancellationToken token = default) => Task.Run(Execute, token);
/// <inheritdoc />
public abstract string Description { get; }
/// <summary>
/// Execute the example synchronously.
/// </summary>
protected virtual void Execute() { }
public static ILogger<T> CreateLogger<T>(LogLevel logLevel = LogLevel.Information) =>
new SimpleConsoleLogger<T>(logLevel);
/// <summary>
/// Gets a friendly full name
/// </summary>
/// <param name="typeName"></param>
/// <returns></returns>
protected static string GetFullName(string typeName)
{
var builder = new StringBuilder(typeName.Length + 5);
var first = true;
foreach (var c in typeName)
{
if (first)
{
first = false;
}
else if (char.IsUpper(c))
{
builder.Append(' ');
}
builder.Append(c);
}
var fullName = builder.ToString();
if (fullName.EndsWith("Sample", StringComparison.InvariantCultureIgnoreCase))
{
fullName = fullName[..^6].TrimEnd();
}
return fullName;
}
/// <summary>
/// Gets some short names based on the first word and initials.
/// </summary>
/// <param name="fullName">The full name of the sample.</param>
/// <returns>An array of short names.</returns>
protected static HashSet<string> GetShortNames(string fullName)
{
var shortNames = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
var initials = new StringBuilder();
var word = new StringBuilder();
var titleCase = new StringBuilder();
var afterSpace = true;
var firstWord = true;
foreach (var c in fullName)
{
if (char.GetUnicodeCategory(c) == UnicodeCategory.SpaceSeparator)
{
afterSpace = true;
firstWord = false;
continue;
}
if (afterSpace)
{
afterSpace = false;
var ch = char.ToUpperInvariant(c);
initials.Append(ch);
titleCase.Append(ch);
}
else
{
titleCase.Append(c);
}
if (firstWord)
{
word.Append(c);
}
}
shortNames.Add(initials.ToString(0, 1));
if (initials.Length > 1)
{
shortNames.Add(initials.ToString());
}
shortNames.Add(word.ToString());
if (titleCase.Length < 10)
{
shortNames.Add(titleCase.ToString());
}
return shortNames;
}
}