@@ -33,29 +33,30 @@ public class Win32 : IProgram
3333 private const string ApplicationReferenceExtension = "appref-ms" ;
3434 private const string ExeExtension = "exe" ;
3535
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-
4536
4637 public Result Result ( string query , IPublicAPI api )
4738 {
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 )
5151 return null ;
52- }
5352
5453 var result = new Result
5554 {
55+ Title = title ,
5656 SubTitle = FullPath ,
5757 IcoPath = IcoPath ,
58- Score = score ,
58+ Score = matchResult . Score ,
59+ TitleHighlightData = matchResult . MatchData ,
5960 ContextData = this ,
6061 Action = e =>
6162 {
@@ -72,24 +73,6 @@ public Result Result(string query, IPublicAPI api)
7273 }
7374 } ;
7475
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-
9376 return result ;
9477 }
9578
@@ -141,18 +124,20 @@ public List<Result> ContextMenus(IPublicAPI api)
141124 Action = _ =>
142125 {
143126 var args = ! string . IsNullOrWhiteSpace ( Main . _settings . CustomizedArgs )
144- ? Main . _settings . CustomizedArgs
127+ ? Main . _settings . CustomizedArgs
145128 . 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+
156141 return true ;
157142 } ,
158143 IcoPath = "Images/folder.png"
@@ -264,9 +249,7 @@ private static Win32 ExeProgram(string path)
264249 var program = Win32Program ( path ) ;
265250 var info = FileVersionInfo . GetVersionInfo ( path ) ;
266251 if ( ! string . IsNullOrEmpty ( info . FileDescription ) )
267- {
268252 program . Description = info . FileDescription ;
269- }
270253 return program ;
271254 }
272255 catch ( Exception e ) when ( e is SecurityException || e is UnauthorizedAccessException )
@@ -282,47 +265,23 @@ private static IEnumerable<string> ProgramPaths(string directory, string[] suffi
282265 {
283266 if ( ! Directory . Exists ( directory ) )
284267 return new string [ ] { } ;
285- var files = new List < string > ( ) ;
286- var folderQueue = new Queue < string > ( ) ;
287- folderQueue . Enqueue ( directory ) ;
288- do
268+ try
289269 {
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 ;
311273
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+ }
326285 }
327286
328287 private static string Extension ( string path )
@@ -340,23 +299,20 @@ private static string Extension(string path)
340299
341300 private static ParallelQuery < Win32 > UnregisteredPrograms ( List < Settings . ProgramSource > sources , string [ ] suffixes )
342301 {
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 )
345303 . SelectMany ( s => ProgramPaths ( s . Location , suffixes ) )
346- . ToList ( )
347304 . 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 ;
360316 }
361317
362318 private static ParallelQuery < Win32 > StartMenuPrograms ( string [ ] suffixes )
@@ -369,15 +325,16 @@ private static ParallelQuery<Win32> StartMenuPrograms(string[] suffixes)
369325 var paths2 = ProgramPaths ( directory2 , suffixes ) ;
370326
371327 var toFilter = paths1 . Concat ( paths2 ) ;
372- var paths = toFilter
328+
329+ var programs = toFilter
330+ . AsParallel ( )
373331 . Where ( t1 => ! disabledProgramsList . Any ( x => x . UniqueIdentifier == t1 ) )
374- . Select ( t1 => t1 )
375332 . 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 ) ;
381338 return programs ;
382339 }
383340
0 commit comments