-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtil.cs
More file actions
80 lines (71 loc) · 2.72 KB
/
Util.cs
File metadata and controls
80 lines (71 loc) · 2.72 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Grasshopper.Kernel.Special;
namespace FileHopper
{
internal static class Util
{
private static string WildCardToRegex(string value)
{
return "^" + Regex.Escape(value).Replace("\\?", ".").Replace("\\*", ".*") + "$";
}
public static bool WildCardMatch(this string text, string pattern)
{
return Regex.IsMatch(text, WildCardToRegex(pattern));
}
public static HashSet<string> GetFolders(List<DirectoryInfo> paths)
{
return paths.Where(dir => dir.Exists)
.Select(dir => dir.GetDirectories())
.SelectMany(subDirs => subDirs)
.Select(subDir => subDir.Name)
.ToHashSet();
}
public static HashSet<string> GetFiles(List<DirectoryInfo> paths)
{
return paths.Where(dir => dir.Exists)
.Select(dir => dir.GetFiles())
.SelectMany(files => files)
.Select(file => file.Name)
.ToHashSet();
}
public static HashSet<string> GetValueListNames(GH_ValueList valueList)
{
return valueList.ListItems.Select(item => item.Name).ToHashSet();
}
public static void RebuildValueList(GH_ValueList valueList, IEnumerable<(string Key, string Value)> keyValuePairs)
{
var selectedNames = valueList.SelectedItems.Select(item => item.Name);
valueList.ListItems.Clear();
valueList.SelectedItems.Clear();
valueList.ListItems.AddRange(keyValuePairs.Select(pair => new GH_ValueListItem(pair.Key, pair.Value)));
var names = valueList.ListItems.Select(item => item.Name).ToList();
foreach (var selectedName in selectedNames)
{
if (names.Contains(selectedName))
{
var index = names.IndexOf(selectedName);
valueList.SelectItem(index);
}
}
if (valueList.SelectedItems.Count == 0)
{
valueList.SelectItem(0);
}
valueList.Refresh();
}
public static string ToValueListString(this string text) => "\"" + text + "\"";
static void Refresh(this GH_ValueList valueList)
{
valueList.Attributes.ExpireLayout();
valueList.OnAttributesChanged();
valueList.ExpireSolution(true);
valueList.OnDisplayExpired(true);
}
}
}