From efd0041c0eae00aa2ef7301b267a5e95b8d4099c Mon Sep 17 00:00:00 2001 From: Thomas Date: Sat, 4 Mar 2023 12:00:07 +0000 Subject: [PATCH] Add support for adjusting search matching --- Terminal.Gui/Windows/FileDialog2.cs | 48 +++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/Terminal.Gui/Windows/FileDialog2.cs b/Terminal.Gui/Windows/FileDialog2.cs index b3e162046..583daaf0c 100644 --- a/Terminal.Gui/Windows/FileDialog2.cs +++ b/Terminal.Gui/Windows/FileDialog2.cs @@ -464,6 +464,13 @@ namespace Terminal.Gui { /// public Func IconGetter { get; set; } = null; + /// + /// Defines how the dialog matches files/folders when using the search + // box. Provide a custom implementation if you want to tailor how matching + /// is performed. + /// + public ISearchMatcher SearchMatcher {get;set;} = new DefaultSearchMatcher(); + /// /// Gets or Sets a value indicating whether to allow selecting /// multiple existing files/directories. @@ -1242,6 +1249,41 @@ namespace Terminal.Gui { } } + + /// + /// Defines whether a given file/directory matches a set of + /// search terms. + /// + public interface ISearchMatcher + { + /// + /// Called once for each new search. Defines the string + /// the user has provided as search terms. + /// + void Initialize(string terms); + + /// + /// Return true if is a match to the + /// last provided search terms + /// + bool IsMatch(FileSystemInfo f); + } + + class DefaultSearchMatcher : ISearchMatcher + { + string terms; + + public void Initialize (string terms) + { + this.terms = terms; + } + + public bool IsMatch (FileSystemInfo f) + { + return f.Name.Contains(terms); + } + } + /// /// Wrapper for that contains additional information /// (e.g. ) and helper methods. @@ -1367,7 +1409,7 @@ namespace Terminal.Gui { /// downwards. /// internal class SearchState : FileDialogState { - readonly string searchTerms; + bool cancel = false; bool finished = false; @@ -1378,7 +1420,7 @@ namespace Terminal.Gui { public SearchState (DirectoryInfo dir, FileDialog2 parent, string searchTerms) : base (dir, parent) { - this.searchTerms = searchTerms; + parent.SearchMatcher.Initialize(searchTerms); Children = new FileSystemInfoStats [0]; BeginSearch (); } @@ -1451,7 +1493,7 @@ namespace Terminal.Gui { continue; } - if (f.Name.Contains (searchTerms)) { + if (Parent.SearchMatcher.IsMatch(f.FileSystemInfo)) { lock (oLockFound) { found.Add (f);