Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
yatima1460 committed May 15, 2024
1 parent 26fd112 commit 7b7b903
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 117 deletions.
160 changes: 44 additions & 116 deletions Core/Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,45 @@ public void StartAsync()
throw new DrillException("Can't scan using an empty string");
}


directoriesToExplore.Add(@"C:\Program Files (x86)\Steam\steamapps\common");
directoriesToExplore.Add($"C:\\Users\\{UserName}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Steam");
directoriesToExplore.Add($"C:\\Users\\{UserName}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu");
directoriesToExplore.Add(@"C:\ProgramData\Microsoft\Windows\Start Menu");
directoriesToExplore.Add(Environment.SpecialFolder.UserProfile);
directoriesToExplore.Add(Environment.SpecialFolder.Recent);
directoriesToExplore.Add(Environment.SpecialFolder.MyMusic);
directoriesToExplore.Add(Environment.SpecialFolder.ProgramFiles);
directoriesToExplore.Add(Environment.SpecialFolder.ProgramFilesX86);
directoriesToExplore.Add(Environment.SpecialFolder.Desktop);
directoriesToExplore.Add(Environment.SpecialFolder.MyDocuments);
directoriesToExplore.Add(Environment.SpecialFolder.MyVideos);
directoriesToExplore.Add($"C:\\Users\\{UserName}\\Downloads");
directoriesToExplore.Add($"/Users/{UserName}/Library/Mobile Documents/com~apple~CloudDocs/");


DriveInfo[] allDrives = [];
try
{
allDrives = DriveInfo.GetDrives();
}
catch (Exception e)
{
Debug.Print(e.Message);
}

foreach (DriveInfo d in allDrives)
scan = new Task(() =>
{
if (cancellationTokenSource.IsCancellationRequested)
return;
if (d.IsReady == true && d.RootDirectory.Exists)
// Set interesting directories as roots
directoriesToExplore.Add(@"C:\Program Files (x86)\Steam\steamapps\common");
directoriesToExplore.Add($"C:\\Users\\{UserName}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Steam");
directoriesToExplore.Add($"C:\\Users\\{UserName}\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu");
directoriesToExplore.Add(@"C:\ProgramData\Microsoft\Windows\Start Menu");
directoriesToExplore.Add(Environment.SpecialFolder.UserProfile);
directoriesToExplore.Add(Environment.SpecialFolder.Recent);
directoriesToExplore.Add(Environment.SpecialFolder.MyMusic);
directoriesToExplore.Add(Environment.SpecialFolder.ProgramFiles);
directoriesToExplore.Add(Environment.SpecialFolder.ProgramFilesX86);
directoriesToExplore.Add(Environment.SpecialFolder.Desktop);
directoriesToExplore.Add(Environment.SpecialFolder.MyDocuments);
directoriesToExplore.Add(Environment.SpecialFolder.MyVideos);
directoriesToExplore.Add($"C:\\Users\\{UserName}\\Downloads");
directoriesToExplore.Add($"/Users/{UserName}/Library/Mobile Documents/com~apple~CloudDocs/");

// Set all drives as roots
DriveInfo[] allDrives = [];
try
{
directoriesToExplore.Add(d.RootDirectory);
allDrives = DriveInfo.GetDrives();
}
catch (Exception e)
{
Debug.Print(e.Message);
}
foreach (DriveInfo d in allDrives)
{
if (cancellationTokenSource.IsCancellationRequested)
return;
if (d.IsReady == true && d.RootDirectory.Exists)
{
directoriesToExplore.Add(d.RootDirectory);
}
}
}

scan = new Task(() =>
{
// Now we start to scan for real
while (!cancellationTokenSource.IsCancellationRequested && directoriesToExplore.PopHighestPriority(out DirectoryInfo? rootFolderInfo))
{
// We have explored everything possible, it's time to stop
Expand Down Expand Up @@ -194,89 +194,17 @@ public void StartAsync()
}

Debug.WriteLine("Search done.");
});
}, cancellationTokenSource.Token, TaskCreationOptions.LongRunning);
scan.Start();

}

readonly Dictionary<string, int> subDirectoriesCountCache = new();

/// <summary>
/// Given a list of directories filters them to generate drill results
/// </summary>
/// <param name="rootFolderInfo"></param>
/// <param name="directories"></param>
/// <param name="searchString"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private static List<DirectoryInfo> GenerateDrillResults(in DirectoryInfo[] directories, in string searchString, in CancellationToken cancellationToken)
{



List<DirectoryInfo> results = [];
foreach (DirectoryInfo sub in directories)
{
if (cancellationToken.IsCancellationRequested)
return [];
// TODO move to Platforms
if (
sub.FullName == $"/Users/{UserName}/Pictures/Photos Library.photoslibrary" ||
sub.FullName == $"/Users/{UserName}/Library/Calendars" ||
sub.FullName == $"/Users/{UserName}/Library/Reminders" ||
sub.FullName == $"/Users/{UserName}/Library/Contacts"
)
{
continue;
}

if (StringUtils.TokenMatching(searchString, sub.Name))
{


// this may stall for a sec
results.Add(new DirectoryInfo(sub.FullName));
}




}

return results;
}


/// <summary>
/// Given a folder generates Drill results of all the files inside
/// Stops the search
/// Does nothing if already stopped
/// </summary>
/// <param name="rootFolderInfo"></param>
/// <param name="searchString"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
private static List<FileInfo> GenerateDrillResults(in FileInfo[] subs, in string searchString, in CancellationToken cancellationToken)
{
// Directory.GetFileSystemEntries()

List<FileInfo> results = new();

foreach (FileInfo file in subs)
{
if (cancellationToken.IsCancellationRequested)
return [];
if (StringUtils.TokenMatching(searchString, file.Name))
{

// this may stall for a sec
results.Add(new FileInfo(file.FullName));
}
}

return results;
}



/// <returns>AggregateException? if it happened in the Task run</returns>
public AggregateException? Stop()
{
cancellationTokenSource.Cancel();
Expand All @@ -292,6 +220,11 @@ private static List<FileInfo> GenerateDrillResults(in FileInfo[] subs, in string
return null;
}

/// <summary>
/// Pop the latest results from the search
/// </summary>
/// <param name="count">The number of results to pop</param>
/// <returns>The latest results or empty if stop requested</returns>
public List<FileSystemInfo> PopResults(in int count)
{
int minSize = Math.Min(count, ParallelResults.Count);
Expand All @@ -309,10 +242,5 @@ public List<FileSystemInfo> PopResults(in int count)
}
return results;
}

public override string? ToString()
{
return searchString;
}
}
}
4 changes: 3 additions & 1 deletion Core/StringUtils.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@

using System.Collections.Concurrent;

namespace Drill.Core
{
public static class StringUtils
{

internal static readonly Dictionary<Tuple<string, string>, bool> tokenCache = [];
internal static readonly ConcurrentDictionary<Tuple<string, string>, bool> tokenCache = [];

internal static bool TokenMatching(in string searchString, in string fileName)
{
Expand Down

0 comments on commit 7b7b903

Please sign in to comment.