-
Notifications
You must be signed in to change notification settings - Fork 397
Expand file tree
/
Copy pathApp.Extensions.cs
More file actions
105 lines (91 loc) · 2.89 KB
/
App.Extensions.cs
File metadata and controls
105 lines (91 loc) · 2.89 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Avalonia.Media;
namespace SourceGit
{
public static class StringExtensions
{
public static string Quoted(this string value)
{
return $"\"{Escaped(value)}\"";
}
public static string Escaped(this string value)
{
return value.Replace("\"", "\\\"", StringComparison.Ordinal);
}
public static string FormatFontNames(string input)
{
if (string.IsNullOrEmpty(input))
return string.Empty;
var parts = input.Split(',');
var trimmed = new List<string>();
foreach (var part in parts)
{
var t = part.Trim();
if (string.IsNullOrEmpty(t))
continue;
var sb = new StringBuilder();
var prevChar = '\0';
foreach (var c in t)
{
if (c == ' ' && prevChar == ' ')
continue;
sb.Append(c);
prevChar = c;
}
var name = sb.ToString();
try
{
var fontFamily = FontFamily.Parse(name);
if (fontFamily.FamilyTypefaces.Count > 0)
trimmed.Add(name);
}
catch
{
// Ignore exceptions.
}
}
return trimmed.Count > 0 ? string.Join(',', trimmed) : string.Empty;
}
}
public static class CommandExtensions
{
public static T Use<T>(this T cmd, Models.ICommandLog log) where T : Commands.Command
{
cmd.Log = log;
return cmd;
}
}
public static class DirectoryInfoExtension
{
public static void WalkFiles(this DirectoryInfo dir, Action<string> onFile, int maxDepth = 4)
{
try
{
var options = new EnumerationOptions()
{
IgnoreInaccessible = true,
RecurseSubdirectories = false,
};
foreach (var file in dir.GetFiles("*", options))
onFile(file.FullName);
if (maxDepth > 0)
{
foreach (var subDir in dir.GetDirectories("*", options))
{
if (subDir.Name.StartsWith(".", StringComparison.Ordinal) ||
subDir.Name.Equals("node_modules", StringComparison.OrdinalIgnoreCase))
continue;
WalkFiles(subDir, onFile, maxDepth - 1);
}
}
}
catch
{
// Ignore exceptions.
}
}
}
}