@@ -33,29 +33,30 @@ public class Win32 : IProgram
33
33
private const string ApplicationReferenceExtension = "appref-ms" ;
34
34
private const string ExeExtension = "exe" ;
35
35
36
- private int Score ( string query )
37
- {
38
- var nameMatch = StringMatcher . FuzzySearch ( query , Name ) ;
39
- var descriptionMatch = StringMatcher . FuzzySearch ( query , Description ) ;
40
- var executableNameMatch = StringMatcher . FuzzySearch ( query , ExecutableName ) ;
41
- var score = new [ ] { nameMatch . Score , descriptionMatch . Score , executableNameMatch . Score } . Max ( ) ;
42
- return score ;
43
- }
44
-
45
36
46
37
public Result Result ( string query , IPublicAPI api )
47
38
{
48
- var score = Score ( query ) ;
49
- if ( score <= 0 )
50
- { // no need to create result if this is zero
39
+ var title = ( Name , Description ) switch
40
+ {
41
+ ( var n , null ) => n ,
42
+ ( var n , var d ) when d . StartsWith ( n ) => d ,
43
+ ( var n , var d ) when n . StartsWith ( d ) => n ,
44
+ ( var n , var d ) when ! string . IsNullOrEmpty ( d ) => $ "{ n } : { d } ",
45
+ _ => Name
46
+ } ;
47
+
48
+ var matchResult = StringMatcher . FuzzySearch ( query , title ) ;
49
+
50
+ if ( ! matchResult . Success )
51
51
return null ;
52
- }
53
52
54
53
var result = new Result
55
54
{
55
+ Title = title ,
56
56
SubTitle = FullPath ,
57
57
IcoPath = IcoPath ,
58
- Score = score ,
58
+ Score = matchResult . Score ,
59
+ TitleHighlightData = matchResult . MatchData ,
59
60
ContextData = this ,
60
61
Action = e =>
61
62
{
@@ -72,24 +73,6 @@ public Result Result(string query, IPublicAPI api)
72
73
}
73
74
} ;
74
75
75
- if ( Description . Length >= Name . Length &&
76
- Description . Substring ( 0 , Name . Length ) == Name )
77
- {
78
- result . Title = Description ;
79
- result . TitleHighlightData = StringMatcher . FuzzySearch ( query , Description ) . MatchData ;
80
- }
81
- else if ( ! string . IsNullOrEmpty ( Description ) )
82
- {
83
- var title = $ "{ Name } : { Description } ";
84
- result . Title = title ;
85
- result . TitleHighlightData = StringMatcher . FuzzySearch ( query , title ) . MatchData ;
86
- }
87
- else
88
- {
89
- result . Title = Name ;
90
- result . TitleHighlightData = StringMatcher . FuzzySearch ( query , Name ) . MatchData ;
91
- }
92
-
93
76
return result ;
94
77
}
95
78
@@ -141,18 +124,20 @@ public List<Result> ContextMenus(IPublicAPI api)
141
124
Action = _ =>
142
125
{
143
126
var args = ! string . IsNullOrWhiteSpace ( Main . _settings . CustomizedArgs )
144
- ? Main . _settings . CustomizedArgs
127
+ ? Main . _settings . CustomizedArgs
145
128
. Replace ( "%s" , $ "\" { ParentDirectory } \" ")
146
- . Replace ( "%f" , $ "\" { FullPath } \" ") :
147
- Main . _settings . CustomizedExplorer == Settings . Explorer
148
- ? $ "/select,\" { FullPath } \" "
149
- : Settings . ExplorerArgs ;
150
-
151
- Main . StartProcess ( Process . Start , new ProcessStartInfo (
152
- ! string . IsNullOrWhiteSpace ( Main . _settings . CustomizedExplorer )
153
- ? Main . _settings . CustomizedExplorer
154
- : Settings . Explorer ,
155
- args ) ) ;
129
+ . Replace ( "%f" , $ "\" { FullPath } \" ")
130
+ : Main . _settings . CustomizedExplorer == Settings . Explorer
131
+ ? $ "/select,\" { FullPath } \" "
132
+ : Settings . ExplorerArgs ;
133
+
134
+ Main . StartProcess ( Process . Start ,
135
+ new ProcessStartInfo (
136
+ ! string . IsNullOrWhiteSpace ( Main . _settings . CustomizedExplorer )
137
+ ? Main . _settings . CustomizedExplorer
138
+ : Settings . Explorer ,
139
+ args ) ) ;
140
+
156
141
return true ;
157
142
} ,
158
143
IcoPath = "Images/folder.png"
@@ -264,9 +249,7 @@ private static Win32 ExeProgram(string path)
264
249
var program = Win32Program ( path ) ;
265
250
var info = FileVersionInfo . GetVersionInfo ( path ) ;
266
251
if ( ! string . IsNullOrEmpty ( info . FileDescription ) )
267
- {
268
252
program . Description = info . FileDescription ;
269
- }
270
253
return program ;
271
254
}
272
255
catch ( Exception e ) when ( e is SecurityException || e is UnauthorizedAccessException )
@@ -282,47 +265,23 @@ private static IEnumerable<string> ProgramPaths(string directory, string[] suffi
282
265
{
283
266
if ( ! Directory . Exists ( directory ) )
284
267
return new string [ ] { } ;
285
- var files = new List < string > ( ) ;
286
- var folderQueue = new Queue < string > ( ) ;
287
- folderQueue . Enqueue ( directory ) ;
288
- do
268
+ try
289
269
{
290
- var currentDirectory = folderQueue . Dequeue ( ) ;
291
- try
292
- {
293
- foreach ( var suffix in suffixes )
294
- {
295
- try
296
- {
297
- files . AddRange ( Directory . EnumerateFiles ( currentDirectory , $ "*.{ suffix } ", SearchOption . TopDirectoryOnly ) ) ;
298
- }
299
- catch ( DirectoryNotFoundException e )
300
- {
301
- ProgramLogger . LogException ( $ "|Win32|ProgramPaths|{ currentDirectory } " +
302
- "|The directory trying to load the program from does not exist" , e ) ;
303
- }
304
- }
305
- }
306
- catch ( Exception e ) when ( e is SecurityException || e is UnauthorizedAccessException )
307
- {
308
- ProgramLogger . LogException ( $ "|Win32|ProgramPaths|{ currentDirectory } " +
309
- $ "|Permission denied when trying to load programs from { currentDirectory } ", e ) ;
310
- }
270
+ var paths = Directory . EnumerateFiles ( directory , "*" , SearchOption . AllDirectories )
271
+ . Where ( x => suffixes . Contains ( Extension ( x ) ) ) ;
272
+ return paths ;
311
273
312
- try
313
- {
314
- foreach ( var childDirectory in Directory . EnumerateDirectories ( currentDirectory , "*" , SearchOption . TopDirectoryOnly ) )
315
- {
316
- folderQueue . Enqueue ( childDirectory ) ;
317
- }
318
- }
319
- catch ( Exception e ) when ( e is SecurityException || e is UnauthorizedAccessException )
320
- {
321
- ProgramLogger . LogException ( $ "|Win32|ProgramPaths|{ currentDirectory } " +
322
- $ "|Permission denied when trying to load programs from { currentDirectory } ", e ) ;
323
- }
324
- } while ( folderQueue . Any ( ) ) ;
325
- return files ;
274
+ }
275
+ catch ( DirectoryNotFoundException e )
276
+ {
277
+ ProgramLogger . LogException ( $ "Directory not found { directory } ", e ) ;
278
+ return new string [ ] { } ;
279
+ }
280
+ catch ( Exception e ) when ( e is SecurityException || e is UnauthorizedAccessException )
281
+ {
282
+ ProgramLogger . LogException ( $ "Permission denied { directory } ", e ) ;
283
+ return new string [ ] { } ;
284
+ }
326
285
}
327
286
328
287
private static string Extension ( string path )
@@ -340,23 +299,20 @@ private static string Extension(string path)
340
299
341
300
private static ParallelQuery < Win32 > UnregisteredPrograms ( List < Settings . ProgramSource > sources , string [ ] suffixes )
342
301
{
343
- var listToAdd = new List < string > ( ) ;
344
- sources . Where ( s => Directory . Exists ( s . Location ) && s . Enabled )
302
+ var paths = sources . Where ( s => Directory . Exists ( s . Location ) && s . Enabled )
345
303
. SelectMany ( s => ProgramPaths ( s . Location , suffixes ) )
346
- . ToList ( )
347
304
. Where ( t1 => ! Main . _settings . DisabledProgramSources . Any ( x => t1 == x . UniqueIdentifier ) )
348
- . ToList ( )
349
- . ForEach ( x => listToAdd . Add ( x ) ) ;
350
-
351
- var paths = listToAdd . Distinct ( ) . ToArray ( ) ;
352
-
353
- var programs1 = paths . AsParallel ( ) . Where ( p => Extension ( p ) == ExeExtension ) . Select ( ExeProgram ) ;
354
- var programs2 = paths . AsParallel ( ) . Where ( p => Extension ( p ) == ShortcutExtension ) . Select ( LnkProgram ) ;
355
- var programs3 = from p in paths . AsParallel ( )
356
- let e = Extension ( p )
357
- where e != ShortcutExtension && e != ExeExtension
358
- select Win32Program ( p ) ;
359
- return programs1 . Concat ( programs2 ) . Concat ( programs3 ) ;
305
+ . Distinct ( ) ;
306
+
307
+ var programs = paths . AsParallel ( ) . Select ( x => Extension ( x ) switch
308
+ {
309
+ ExeExtension => ExeProgram ( x ) ,
310
+ ShortcutExtension => LnkProgram ( x ) ,
311
+ _ => Win32Program ( x )
312
+ } ) ;
313
+
314
+
315
+ return programs ;
360
316
}
361
317
362
318
private static ParallelQuery < Win32 > StartMenuPrograms ( string [ ] suffixes )
@@ -369,15 +325,16 @@ private static ParallelQuery<Win32> StartMenuPrograms(string[] suffixes)
369
325
var paths2 = ProgramPaths ( directory2 , suffixes ) ;
370
326
371
327
var toFilter = paths1 . Concat ( paths2 ) ;
372
- var paths = toFilter
328
+
329
+ var programs = toFilter
330
+ . AsParallel ( )
373
331
. Where ( t1 => ! disabledProgramsList . Any ( x => x . UniqueIdentifier == t1 ) )
374
- . Select ( t1 => t1 )
375
332
. Distinct ( )
376
- . ToArray ( ) ;
377
-
378
- var programs1 = paths . AsParallel ( ) . Where ( p => Extension ( p ) == ShortcutExtension ) . Select ( LnkProgram ) ;
379
- var programs2 = paths . AsParallel ( ) . Where ( p => Extension ( p ) == ApplicationReferenceExtension ) . Select ( Win32Program ) ;
380
- var programs = programs1 . Concat ( programs2 ) . Where ( p => p . Valid ) ;
333
+ . Select ( x => Extension ( x ) switch
334
+ {
335
+ ShortcutExtension => LnkProgram ( x ) ,
336
+ _ => Win32Program ( x )
337
+ } ) . Where ( x => x . Valid ) ;
381
338
return programs ;
382
339
}
383
340
0 commit comments