Skip to content

Commit 24ce101

Browse files
taoocerosbao-qian
andcommitted
Improve reindex speed
Co-authored-by: Qian Bao <[email protected]>
1 parent 991227a commit 24ce101

File tree

1 file changed

+36
-67
lines changed
  • Plugins/Flow.Launcher.Plugin.Program/Programs

1 file changed

+36
-67
lines changed

Plugins/Flow.Launcher.Plugin.Program/Programs/Win32.cs

Lines changed: 36 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ public List<Result> ContextMenus(IPublicAPI api)
140140
Title = api.GetTranslation("flowlauncher_plugin_program_open_containing_folder"),
141141
Action = _ =>
142142
{
143-
144-
145143
Main.StartProcess(Process.Start, new ProcessStartInfo("explorer", ParentDirectory));
146144

147145
return true;
@@ -254,10 +252,7 @@ private static Win32 ExeProgram(string path)
254252
{
255253
var program = Win32Program(path);
256254
var info = FileVersionInfo.GetVersionInfo(path);
257-
if (!string.IsNullOrEmpty(info.FileDescription))
258-
{
259-
program.Description = info.FileDescription;
260-
}
255+
program.Description = info.FileDescription;
261256
return program;
262257
}
263258
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
@@ -273,47 +268,23 @@ private static IEnumerable<string> ProgramPaths(string directory, string[] suffi
273268
{
274269
if (!Directory.Exists(directory))
275270
return new string[] { };
276-
var files = new List<string>();
277-
var folderQueue = new Queue<string>();
278-
folderQueue.Enqueue(directory);
279-
do
271+
try
280272
{
281-
var currentDirectory = folderQueue.Dequeue();
282-
try
283-
{
284-
foreach (var suffix in suffixes)
285-
{
286-
try
287-
{
288-
files.AddRange(Directory.EnumerateFiles(currentDirectory, $"*.{suffix}", SearchOption.TopDirectoryOnly));
289-
}
290-
catch (DirectoryNotFoundException e)
291-
{
292-
ProgramLogger.LogException($"|Win32|ProgramPaths|{currentDirectory}" +
293-
"|The directory trying to load the program from does not exist", e);
294-
}
295-
}
296-
}
297-
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
298-
{
299-
ProgramLogger.LogException($"|Win32|ProgramPaths|{currentDirectory}" +
300-
$"|Permission denied when trying to load programs from {currentDirectory}", e);
301-
}
273+
var paths = Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories)
274+
.Where(x => suffixes.Contains(Extension(x)));
275+
return paths;
302276

303-
try
304-
{
305-
foreach (var childDirectory in Directory.EnumerateDirectories(currentDirectory, "*", SearchOption.TopDirectoryOnly))
306-
{
307-
folderQueue.Enqueue(childDirectory);
308-
}
309-
}
310-
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
311-
{
312-
ProgramLogger.LogException($"|Win32|ProgramPaths|{currentDirectory}" +
313-
$"|Permission denied when trying to load programs from {currentDirectory}", e);
314-
}
315-
} while (folderQueue.Any());
316-
return files;
277+
}
278+
catch (DirectoryNotFoundException e)
279+
{
280+
ProgramLogger.LogException($"Directory not found {directory}", e);
281+
return new string[] { };
282+
}
283+
catch (Exception e) when (e is SecurityException || e is UnauthorizedAccessException)
284+
{
285+
ProgramLogger.LogException($"Permission denied {directory}", e);
286+
return new string[] { };
287+
}
317288
}
318289

319290
private static string Extension(string path)
@@ -331,23 +302,20 @@ private static string Extension(string path)
331302

332303
private static ParallelQuery<Win32> UnregisteredPrograms(List<Settings.ProgramSource> sources, string[] suffixes)
333304
{
334-
var listToAdd = new List<string>();
335-
sources.Where(s => Directory.Exists(s.Location) && s.Enabled)
305+
var paths = sources.Where(s => Directory.Exists(s.Location) && s.Enabled)
336306
.SelectMany(s => ProgramPaths(s.Location, suffixes))
337-
.ToList()
338307
.Where(t1 => !Main._settings.DisabledProgramSources.Any(x => t1 == x.UniqueIdentifier))
339-
.ToList()
340-
.ForEach(x => listToAdd.Add(x));
341-
342-
var paths = listToAdd.Distinct().ToArray();
343-
344-
var programs1 = paths.AsParallel().Where(p => Extension(p) == ExeExtension).Select(ExeProgram);
345-
var programs2 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(LnkProgram);
346-
var programs3 = from p in paths.AsParallel()
347-
let e = Extension(p)
348-
where e != ShortcutExtension && e != ExeExtension
349-
select Win32Program(p);
350-
return programs1.Concat(programs2).Concat(programs3);
308+
.Distinct();
309+
310+
var programs = paths.AsParallel().Select(x => Extension(x) switch
311+
{
312+
ExeExtension => ExeProgram(x),
313+
ShortcutExtension => LnkProgram(x),
314+
_ => Win32Program(x)
315+
});
316+
317+
318+
return programs;
351319
}
352320

353321
private static ParallelQuery<Win32> StartMenuPrograms(string[] suffixes)
@@ -360,15 +328,16 @@ private static ParallelQuery<Win32> StartMenuPrograms(string[] suffixes)
360328
var paths2 = ProgramPaths(directory2, suffixes);
361329

362330
var toFilter = paths1.Concat(paths2);
363-
var paths = toFilter
331+
332+
var programs = toFilter
333+
.AsParallel()
364334
.Where(t1 => !disabledProgramsList.Any(x => x.UniqueIdentifier == t1))
365-
.Select(t1 => t1)
366335
.Distinct()
367-
.ToArray();
368-
369-
var programs1 = paths.AsParallel().Where(p => Extension(p) == ShortcutExtension).Select(LnkProgram);
370-
var programs2 = paths.AsParallel().Where(p => Extension(p) == ApplicationReferenceExtension).Select(Win32Program);
371-
var programs = programs1.Concat(programs2).Where(p => p.Valid);
336+
.Select(x => Extension(x) switch
337+
{
338+
ShortcutExtension => LnkProgram(x),
339+
_ => Win32Program(x)
340+
}).Where(x => x.Valid);
372341
return programs;
373342
}
374343

0 commit comments

Comments
 (0)