|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | + |
| 8 | +#nullable enable |
| 9 | + |
| 10 | +namespace CachedPathSuggestBoxDemo.Infrastructure |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Defines a suggestion object to generate suggestions |
| 14 | + /// based on sub entries of specified string. |
| 15 | + /// </summary> |
| 16 | + public class DirectorySuggest : ISuggest |
| 17 | + { |
| 18 | + #region fields |
| 19 | + private readonly Dictionary<string, CancellationTokenSource> _Queue; |
| 20 | + private readonly SemaphoreSlim _SlowStuffSemaphore; |
| 21 | + #endregion fields |
| 22 | + |
| 23 | + #region ctors |
| 24 | + /// <summary> |
| 25 | + /// Class constructor |
| 26 | + /// </summary> |
| 27 | + public DirectorySuggest() |
| 28 | + { |
| 29 | + _Queue = new Dictionary<string, CancellationTokenSource>(); |
| 30 | + _SlowStuffSemaphore = new SemaphoreSlim(1, 1); |
| 31 | + } |
| 32 | + #endregion ctors |
| 33 | + |
| 34 | + public async Task<IEnumerable<object>?> MakeSuggestions(string queryThis) |
| 35 | + { |
| 36 | + // Cancel current task(s) if there is any... |
| 37 | + var queueList = _Queue.Values.ToList(); |
| 38 | + |
| 39 | + foreach (var t in queueList) |
| 40 | + t.Cancel(); |
| 41 | + |
| 42 | + var tokenSource = new CancellationTokenSource(); |
| 43 | + _Queue.Add(queryThis, tokenSource); |
| 44 | + |
| 45 | + // Make sure the task always processes the last input but is not started twice |
| 46 | + await _SlowStuffSemaphore.WaitAsync(tokenSource.Token); |
| 47 | + try |
| 48 | + { |
| 49 | + // There is more recent input to process so we ignore this one |
| 50 | + if (_Queue.Count <= 1) |
| 51 | + return await (string.IsNullOrEmpty(queryThis) |
| 52 | + ? Task.FromResult(EnumerateSubDirs(queryThis)) |
| 53 | + : Task.FromResult(queryThis.Length <= 3 ? EnumerateDrives(queryThis) : EnumerateSubDirs(queryThis))); |
| 54 | + _Queue.Remove(queryThis); |
| 55 | + return null; |
| 56 | + |
| 57 | + } |
| 58 | + catch (Exception exp) |
| 59 | + { |
| 60 | + Console.WriteLine(exp.Message); |
| 61 | + } |
| 62 | + finally |
| 63 | + { |
| 64 | + _Queue.Remove(queryThis); |
| 65 | + _SlowStuffSemaphore.Release(); |
| 66 | + } |
| 67 | + |
| 68 | + return null; |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + static IEnumerable<object>? EnumerateSubDirs(string input) |
| 73 | + { |
| 74 | + if (string.IsNullOrEmpty(input)) |
| 75 | + return EnumerateLogicalDrives(); |
| 76 | + |
| 77 | + var subDirs = EnumerateLogicalDriveOrSubDirs(input, input); |
| 78 | + |
| 79 | + return subDirs!=null? subDirs.Any()? subDirs: Get():null; |
| 80 | + |
| 81 | + // Find last separator and list directories underneath |
| 82 | + // with * search-pattern |
| 83 | + IEnumerable<object> Get() |
| 84 | + { |
| 85 | + int sepIdx = input.LastIndexOf('\\'); |
| 86 | + |
| 87 | + if (sepIdx >= input.Length) return EnumerateLogicalDrives(); |
| 88 | + string folder = input.Substring(0, sepIdx + 1); |
| 89 | + string searchPattern = input.Substring(sepIdx + 1) + "*"; |
| 90 | + string[]? directories = null; |
| 91 | + try |
| 92 | + { |
| 93 | + directories = Directory.GetDirectories(folder, searchPattern); |
| 94 | + } |
| 95 | + catch |
| 96 | + { |
| 97 | + // Catch invalid path exceptions here ... |
| 98 | + } |
| 99 | + |
| 100 | + if (directories == null) return EnumerateLogicalDrives(); |
| 101 | + var dirs = new List<object>(); |
| 102 | + |
| 103 | + foreach (var t in directories) |
| 104 | + dirs.Add(new { Header = t, Value = t }); |
| 105 | + |
| 106 | + return dirs; |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + static IEnumerable<object> EnumerateDrives(string input) |
| 111 | + { |
| 112 | + if (string.IsNullOrEmpty(input)) |
| 113 | + return EnumerateLogicalDrives(); |
| 114 | + |
| 115 | + return EnumeratePaths(input) ?? EnumerateLogicalDrives(); |
| 116 | + |
| 117 | + |
| 118 | + static IEnumerable<object>? EnumeratePaths(string input) => input.Length switch |
| 119 | + { |
| 120 | + 1 when char.ToUpper(input[0]) >= 'A' && char.ToUpper(input[0]) <= 'Z' => |
| 121 | + EnumerateLogicalDriveOrSubDirs(input + ":\\", input), |
| 122 | + |
| 123 | + 2 when char.ToUpper(input[1]) == ':' && |
| 124 | + char.ToUpper(input[0]) >= 'A' && char.ToUpper(input[0]) <= 'Z' => |
| 125 | + EnumerateLogicalDriveOrSubDirs(input + "\\", input), |
| 126 | + |
| 127 | + 2 => new List<object>(), |
| 128 | + |
| 129 | + _ when (char.ToUpper(input[1]) == ':' && |
| 130 | + char.ToUpper(input[2]) == '\\' && |
| 131 | + char.ToUpper(input[0]) >= 'A' && char.ToUpper(input[0]) <= 'Z') => |
| 132 | + // Check if we know this drive and list it with sub-folders if we do |
| 133 | + EnumerateLogicalDriveOrSubDirs(input, input), |
| 134 | + _ => new List<object>() |
| 135 | + }; |
| 136 | + } |
| 137 | + |
| 138 | + |
| 139 | + static IEnumerable<object> EnumerateLogicalDrives() |
| 140 | + { |
| 141 | + foreach (var driveName in Environment.GetLogicalDrives() |
| 142 | + .Where(driveName => string.IsNullOrEmpty(driveName) == false)) |
| 143 | + { |
| 144 | + string header; |
| 145 | + |
| 146 | + try |
| 147 | + { |
| 148 | + DriveInfo d = new DriveInfo(driveName); |
| 149 | + header = string.IsNullOrEmpty(d.VolumeLabel) == false |
| 150 | + ? $"{d.VolumeLabel} ({d.Name})" |
| 151 | + : driveName; |
| 152 | + } |
| 153 | + catch |
| 154 | + { |
| 155 | + header = driveName; |
| 156 | + } |
| 157 | + |
| 158 | + yield return new {Header = header, Value = driveName}; |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + static IEnumerable<object>? EnumerateLogicalDriveOrSubDirs(string testDrive, string input) |
| 163 | + { |
| 164 | + return System.IO.Directory.Exists(testDrive) ? |
| 165 | + GetDirectories(testDrive) is {} array? |
| 166 | + GetLogicalDriveOrSubDirs2(testDrive, input, array): |
| 167 | + null: |
| 168 | + null; |
| 169 | + |
| 170 | + |
| 171 | + static IEnumerable<object>? GetLogicalDriveOrSubDirs2(string testDrive, string input, IEnumerable<string> directories) |
| 172 | + { |
| 173 | + // List the drive itself if there was only 1 or 2 letters |
| 174 | + // since this is not a valid drive and we don'nt know if the user |
| 175 | + // wants to go to the drive or a folder contained in it |
| 176 | + if (input.Length <= 2) |
| 177 | + yield return new {Header = testDrive, Value = testDrive}; |
| 178 | + |
| 179 | + foreach (var item in directories) |
| 180 | + yield return new { Header = item, Value = item }; |
| 181 | + } |
| 182 | + |
| 183 | + static string[]? GetDirectories(string testDrive) |
| 184 | + { |
| 185 | + string[] directories; |
| 186 | + try |
| 187 | + { |
| 188 | + directories = Directory.GetDirectories(testDrive); |
| 189 | + } |
| 190 | + catch (UnauthorizedAccessException) |
| 191 | + { |
| 192 | + return null; |
| 193 | + } |
| 194 | + |
| 195 | + return directories; |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | +} |
0 commit comments