Skip to content

Commit 33dafbf

Browse files
authored
♻️ 💬 Cleanup & Add WriteDebug() Messages (#38)
* 💬 Add debug messages. * ♻️ cleanup code and update style * ♻️ Cleanup / update code styles
1 parent 8fb4d6b commit 33dafbf

File tree

3 files changed

+353
-322
lines changed

3 files changed

+353
-322
lines changed

Module/src/PSWordCloud/Attributes.cs

+109-92
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,8 @@ public IEnumerable<CompletionResult> CompleteArgument(
167167
string matchString = wordToComplete.TrimStart('"').TrimEnd('"');
168168
foreach (string font in WCUtils.FontList)
169169
{
170-
if (string.IsNullOrEmpty(wordToComplete) ||
171-
font.StartsWith(matchString, StringComparison.OrdinalIgnoreCase))
170+
if (string.IsNullOrEmpty(wordToComplete)
171+
|| font.StartsWith(matchString, StringComparison.OrdinalIgnoreCase))
172172
{
173173
if (font.Contains(' ') || font.Contains('#') || wordToComplete.StartsWith("\""))
174174
{
@@ -188,50 +188,56 @@ public class TransformToSKTypefaceAttribute : ArgumentTransformationAttribute
188188
{
189189
public override object Transform(EngineIntrinsics engineIntrinsics, object inputData)
190190
{
191-
switch (inputData)
191+
return inputData switch
192192
{
193-
case SKTypeface t:
194-
return t;
195-
case string s:
196-
return WCUtils.FontManager.MatchFamily(s, SKFontStyle.Normal);
197-
default:
198-
IEnumerable properties;
199-
if (inputData is Hashtable ht)
200-
{
201-
properties = ht;
202-
}
203-
else
204-
{
205-
properties = PSObject.AsPSObject(inputData).Properties;
206-
}
193+
SKTypeface t => t,
194+
string s => WCUtils.FontManager.MatchFamily(s, SKFontStyle.Normal),
195+
_ => CreateTypefaceFromObject(inputData),
196+
};
197+
}
207198

208-
SKFontStyle style;
209-
if (properties.GetValue("FontWeight") == null
210-
|| properties.GetValue("FontSlant") == null
211-
|| properties.GetValue("FontWidth") == null)
212-
{
213-
SKFontStyleWeight weight = properties.GetValue("FontWeight") == null ?
214-
SKFontStyleWeight.Normal : LanguagePrimitives.ConvertTo<SKFontStyleWeight>(
215-
properties.GetValue("FontWeight"));
216-
SKFontStyleSlant slant = properties.GetValue("FontSlant") == null ?
217-
SKFontStyleSlant.Upright : LanguagePrimitives.ConvertTo<SKFontStyleSlant>(
218-
properties.GetValue("FontSlant"));
219-
SKFontStyleWidth width = properties.GetValue("FontWidth") == null ?
220-
SKFontStyleWidth.Normal : LanguagePrimitives.ConvertTo<SKFontStyleWidth>(
221-
properties.GetValue("FontWidth"));
222-
style = new SKFontStyle(weight, width, slant);
223-
}
224-
else
225-
{
226-
style = properties.GetValue("FontStyle") is SKFontStyle customStyle
227-
? customStyle
228-
: SKFontStyle.Normal;
229-
}
199+
private static SKTypeface CreateTypefaceFromObject(object input)
200+
{
201+
IEnumerable properties;
202+
if (input is Hashtable ht)
203+
{
204+
properties = ht;
205+
}
206+
else
207+
{
208+
properties = PSObject.AsPSObject(input).Properties;
209+
}
210+
211+
SKFontStyle style;
212+
if (properties.GetValue("FontWeight") != null
213+
|| properties.GetValue("FontSlant") != null
214+
|| properties.GetValue("FontWidth") != null)
215+
{
216+
SKFontStyleWeight weight = properties.GetValue("FontWeight") == null
217+
? SKFontStyleWeight.Normal
218+
: LanguagePrimitives.ConvertTo<SKFontStyleWeight>(properties.GetValue("FontWeight"));
230219

231-
string familyName = LanguagePrimitives.ConvertTo<string>(properties.GetValue("FamilyName"));
232-
return WCUtils.FontManager.MatchFamily(familyName, style);
220+
SKFontStyleSlant slant = properties.GetValue("FontSlant") == null
221+
? SKFontStyleSlant.Upright
222+
: LanguagePrimitives.ConvertTo<SKFontStyleSlant>(properties.GetValue("FontSlant"));
223+
224+
SKFontStyleWidth width = properties.GetValue("FontWidth") == null
225+
? SKFontStyleWidth.Normal
226+
: LanguagePrimitives.ConvertTo<SKFontStyleWidth>(properties.GetValue("FontWidth"));
227+
228+
style = new SKFontStyle(weight, width, slant);
233229
}
230+
else
231+
{
232+
style = properties.GetValue("FontStyle") is SKFontStyle customStyle
233+
? customStyle
234+
: SKFontStyle.Normal;
235+
}
236+
237+
string familyName = LanguagePrimitives.ConvertTo<string>(properties.GetValue("FamilyName"));
238+
return WCUtils.FontManager.MatchFamily(familyName, style);
234239
}
240+
235241
}
236242

237243
public class SKColorCompleter : IArgumentCompleter
@@ -245,13 +251,16 @@ public IEnumerable<CompletionResult> CompleteArgument(
245251
{
246252
foreach (string color in WCUtils.ColorNames)
247253
{
248-
if (string.IsNullOrEmpty(wordToComplete) ||
249-
color.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase))
254+
if (string.IsNullOrEmpty(wordToComplete)
255+
|| color.StartsWith(wordToComplete, StringComparison.OrdinalIgnoreCase))
250256
{
251257
SKColor colorValue = WCUtils.ColorLibrary[color];
252258
yield return new CompletionResult(
253-
color, color, CompletionResultType.ParameterValue,
254-
string.Format("{0} (R: {1}, G: {2}, B: {3}, A: {4})",
259+
completionText: color,
260+
listItemText: color,
261+
CompletionResultType.ParameterValue,
262+
toolTip: string.Format(
263+
"{0} (R: {1}, G: {2}, B: {3}, A: {4})",
255264
color, colorValue.Red, colorValue.Green, colorValue.Blue, colorValue.Alpha));
256265
}
257266
}
@@ -260,6 +269,60 @@ public IEnumerable<CompletionResult> CompleteArgument(
260269

261270
public class TransformToSKColorAttribute : ArgumentTransformationAttribute
262271
{
272+
private IEnumerable<SKColor> TransformObject(object input)
273+
{
274+
object[] array;
275+
if (input is object[] o)
276+
{
277+
array = o;
278+
}
279+
else
280+
{
281+
array = new[] { input };
282+
}
283+
284+
foreach (object item in array)
285+
{
286+
if (item is string str)
287+
{
288+
foreach (var color in MatchColor(str))
289+
{
290+
yield return color;
291+
}
292+
293+
continue;
294+
}
295+
296+
IEnumerable properties;
297+
if (item is Hashtable ht)
298+
{
299+
properties = ht;
300+
}
301+
else
302+
{
303+
properties = PSObject.AsPSObject(item).Properties;
304+
}
305+
306+
byte red = properties.GetValue("red") == null
307+
? (byte)0
308+
: LanguagePrimitives.ConvertTo<byte>(properties.GetValue("red"));
309+
310+
byte green = properties.GetValue("green") == null
311+
? (byte)0
312+
: LanguagePrimitives.ConvertTo<byte>(properties.GetValue("green"));
313+
314+
byte blue = properties.GetValue("blue") == null
315+
? (byte)0
316+
: LanguagePrimitives.ConvertTo<byte>(properties.GetValue("blue"));
317+
318+
byte alpha = properties.GetValue("alpha") == null
319+
? (byte)255
320+
: LanguagePrimitives.ConvertTo<byte>(properties.GetValue("alpha"));
321+
322+
yield return new SKColor(red, green, blue, alpha);
323+
}
324+
}
325+
263326
private IEnumerable<SKColor> MatchColor(string name)
264327
{
265328
if (WCUtils.ColorNames.Contains(name))
@@ -278,6 +341,7 @@ private IEnumerable<SKColor> MatchColor(string name)
278341
{
279342
bool foundMatch = false;
280343
var pattern = new WildcardPattern(name, WildcardOptions.IgnoreCase);
344+
281345
foreach (var color in WCUtils.ColorLibrary)
282346
{
283347
if (pattern.IsMatch(color.Key))
@@ -302,53 +366,6 @@ private IEnumerable<SKColor> MatchColor(string name)
302366
throw new ArgumentTransformationMetadataException();
303367
}
304368

305-
private IEnumerable<SKColor> TransformObject(object input)
306-
{
307-
object[] array;
308-
if (input is object[] o)
309-
{
310-
array = o;
311-
}
312-
else
313-
{
314-
array = new[] { input };
315-
}
316-
317-
foreach (object item in array)
318-
{
319-
if (item is string s)
320-
{
321-
foreach (var color in MatchColor(s))
322-
{
323-
yield return color;
324-
}
325-
326-
continue;
327-
}
328-
329-
IEnumerable properties;
330-
if (item is Hashtable ht)
331-
{
332-
properties = ht;
333-
}
334-
else
335-
{
336-
properties = PSObject.AsPSObject(item).Properties;
337-
}
338-
339-
byte red = properties.GetValue("red") == null ?
340-
(byte)0 : (byte)LanguagePrimitives.ConvertTo<byte>(properties.GetValue("red"));
341-
byte green = properties.GetValue("green") == null ?
342-
(byte)0 : (byte)LanguagePrimitives.ConvertTo<byte>(properties.GetValue("green"));
343-
byte blue = properties.GetValue("blue") == null ?
344-
(byte)0 : (byte)LanguagePrimitives.ConvertTo<byte>(properties.GetValue("blue"));
345-
byte alpha = properties.GetValue("alpha") == null ?
346-
(byte)255 : (byte)LanguagePrimitives.ConvertTo<byte>(properties.GetValue("alpha"));
347-
348-
yield return new SKColor(red, green, blue, alpha);
349-
}
350-
}
351-
352369
private object Normalize(IEnumerable<SKColor> results)
353370
{
354371
if (results.Count() == 1)

0 commit comments

Comments
 (0)