diff --git a/BFR/Helpers/Extensions.cs b/BFR/Helpers/Extensions.cs index c18649d..7b309e2 100644 --- a/BFR/Helpers/Extensions.cs +++ b/BFR/Helpers/Extensions.cs @@ -1,4 +1,6 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; +using System.Text.RegularExpressions; namespace BFR.Helpers { @@ -11,4 +13,18 @@ namespace BFR.Helpers list.Add(t); } } + + public static class StringExtensions + { + public static string Replace(this string input, string pattern, string replacement, bool useRegex = false, bool useRegexReplace = false) => + Regex.Replace(input, + useRegex ? pattern : Regex.Escape(pattern), + useRegexReplace ? replacement : replacement.Replace("$", "$$")); + + public static bool RegexContains(this string input, string pattern) + { + try { return Regex.IsMatch(input, pattern); } + catch(Exception) { return false; } + } + } } diff --git a/BFR/MainWindow.xaml b/BFR/MainWindow.xaml index 427882c..001aa27 100644 --- a/BFR/MainWindow.xaml +++ b/BFR/MainWindow.xaml @@ -16,13 +16,13 @@ - + - + - + - + diff --git a/BFR/MainWindow.xaml.cs b/BFR/MainWindow.xaml.cs index 5243748..d95b0b0 100644 --- a/BFR/MainWindow.xaml.cs +++ b/BFR/MainWindow.xaml.cs @@ -1,14 +1,17 @@ using System; -using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; +using System.Collections.Generic; +using System.Text.RegularExpressions; +using Avalonia.Input; using Avalonia.Controls; using Avalonia.Markup.Xaml; +using Avalonia.Interactivity; -using BFR.DataModels; using BFR.Helpers; +using BFR.DataModels; namespace BFR { @@ -24,11 +27,14 @@ namespace BFR Filter(); } + public void FilterChangedAny(object sender, KeyEventArgs e) => Filter(); + public void FilterChangedAny(object sender, RoutedEventArgs e) => Filter(); public void Filter() { - Files.ReplaceAll(AllFiles); - - // TODO: Apply filters + // Filter all files in the directory for those satisfying the given filters + Files.ReplaceAll(AllFiles.Where(x => + (FilterExtension == "" || x.Extension == FilterExtension) + && (FilterFullName ? x.FullName : x.Name).RegexContains(FilterRegex ? FilterPattern : Regex.Escape(FilterPattern)))); Preview(); } @@ -45,7 +51,7 @@ namespace BFR Files.ReplaceAll(new List(Files)); } - public MainWindow() => + public MainWindow() => InitializeComponent(); private void InitializeComponent() diff --git a/BFR/MainWindowUIProperties.cs b/BFR/MainWindowUIProperties.cs index ee88bd2..40ffad9 100644 --- a/BFR/MainWindowUIProperties.cs +++ b/BFR/MainWindowUIProperties.cs @@ -1,6 +1,4 @@ -using System; - -using Avalonia; +using Avalonia; using Avalonia.Collections; using Avalonia.Controls; @@ -16,5 +14,11 @@ namespace BFR public AvaloniaList AllFiles { get; } = new AvaloniaList(); public AvaloniaList Files { get; } = new AvaloniaList(); + + // Filters + public string FilterExtension { get; set; } = ""; + public string FilterPattern { get; set; } = ""; + public bool FilterFullName { get; set; } = false; + public bool FilterRegex { get; set; } = false; } }