Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Keep grid visible during search. Update matches in batches to reduce flicker.
  • Loading branch information
ChrisTheisen committed Feb 13, 2023
1 parent 744c039 commit 2ad57a1
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 35 deletions.
4 changes: 2 additions & 2 deletions ROOSFAFS/About.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

100 changes: 67 additions & 33 deletions ROOSFAFS/Utility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;


namespace Searcher
{
internal class Utility
{
{

public static string GetVersion(string file)
{
var vers = FileVersionInfo.GetVersionInfo(file);
Expand Down Expand Up @@ -67,48 +68,50 @@ public static void TestRegexPattern(string pattern)
#region search stuff methods
internal static void SearchFolder(SearchParameters sp,
IDictionary<string, Exception> failedFiles, List<Match> matches,
ToolStripStatusLabel status, Label fileCount)
ToolStripStatusLabel status, Label fileCount, DataGridView resultGrid, DataTable dataSource)
{
searchFolder(sp, sp.RootFolder, failedFiles, matches, status, fileCount);
searchFolder(sp, sp.RootFolder, failedFiles, matches, status, fileCount, resultGrid, dataSource);
}

private static void searchFolder(SearchParameters sp, string searchPath, IDictionary<string, Exception> failedFiles, List<Match> matches, ToolStripStatusLabel status, Control fileCount)
private static void searchFolder(SearchParameters sp, string searchPath, IDictionary<string, Exception> failedFiles, List<Match> matches, ToolStripStatusLabel status, Control fileCount, DataGridView resultGrid, DataTable dataSource)
{
if (sp.CancellationToken.IsCancellationRequested
|| matches.Count >= sp.StopAfterN
|| !Directory.Exists(searchPath)
|| sp.SkipFolders().Any(skip => searchPath.ToUpper().Contains(skip.ToUpper()) || skip.ToUpper().Contains(searchPath.ToUpper())))
return;

MultiThread.UpdateToolStripStatus(status, searchPath);
searchFiles(sp, searchPath, failedFiles, matches);
|| sp.SkipFolders().Any(skip => searchPath.ToUpper().Contains(skip.ToUpper()) || skip.ToUpper().Contains(searchPath.ToUpper())))
return;

MultiThread.UpdateToolStripStatus(status, searchPath);
searchFiles(sp, searchPath, failedFiles, matches, resultGrid, dataSource);
MultiThread.SetProperty(fileCount, "Text", "Hits:" + matches.Count);

if (!sp.IsRecursive)
return;
return;

var dirs = Directory.GetDirectories(searchPath);
foreach (var dir in dirs)
{
searchFolder(sp, dir, failedFiles, matches, status, fileCount);
searchFolder(sp, dir, failedFiles, matches, status, fileCount, resultGrid, dataSource);
}
}

private static void searchFiles(SearchParameters sp, string filePath, IDictionary<string, Exception> failedFiles, List<Match> matches)
private static void searchFiles(SearchParameters sp, string filePath, IDictionary<string, Exception> failedFiles, List<Match> matches, DataGridView resultGrid, DataTable dataSource)
{
try
{
var matchBatch = new List<Match>();
var di = new DirectoryInfo(filePath);
if (di.LastAccessTime < sp.AccessMin || di.LastAccessTime > sp.AccessMax
|| di.LastWriteTime < sp.WriteMin || di.LastWriteTime > sp.WriteMax)
return;

if (sp.SearchType == SearchType.Foldername)
{
if (searchString(sp, filePath))
{
matches.Add(new Match(di));
}
if (searchString(sp, filePath))
{
var m = new Match(di);
addMatch(sp, matches, resultGrid, dataSource, m);
}
return;
}

Expand All @@ -119,7 +122,7 @@ private static void searchFiles(SearchParameters sp, string filePath, IDictionar
{
if (sp.SearchType == SearchType.FileContents || sp.SearchType == SearchType.FileName)
{
searchFile(sp, file, matches);
searchFile(sp, file, matches, resultGrid, dataSource);
}
}
catch (Exception x) { failedFiles.Add(file, x); }
Expand All @@ -132,25 +135,26 @@ private static void searchFiles(SearchParameters sp, string filePath, IDictionar
}
}

private static void searchFile(SearchParameters sp, string filePath, List<Match> matches)
private static void searchFile(SearchParameters sp, string filePath, List<Match> matches, DataGridView resultGrid, DataTable dataSource)
{
if (sp.CancellationToken.IsCancellationRequested || matches.Count >= sp.StopAfterN)
return;

var fi = new FileInfo(filePath);
if (SkipFile(sp, fi))
if (skipFile(sp, fi))
return;

var matchLine = string.Empty;
var match = searchString(sp, filePath);
if (!match && sp.SearchType == SearchType.FileContents)
var isMatch = searchString(sp, filePath);
if (!isMatch && sp.SearchType == SearchType.FileContents)
{
match = SearchContents(sp, filePath, out matchLine);
isMatch = SearchContents(sp, filePath, out matchLine);
}

if (match)
if (isMatch)
{
matches.Add(new Match(fi, matchLine));
var m = new Match(fi, matchLine);
addMatch(sp, matches, resultGrid, dataSource, m);
}
}

Expand Down Expand Up @@ -189,16 +193,43 @@ private static bool searchString(SearchParameters sp, string hayStack)
{
return Regex.IsMatch(hayStack, sp.SearchString);
}
return CultureInfo.CurrentCulture.CompareInfo.IndexOf(hayStack, sp.SearchString, CompareOptions.IgnoreCase) >= 0;
}

return CultureInfo.CurrentCulture.CompareInfo.IndexOf(hayStack, sp.SearchString, CompareOptions.IgnoreCase) >= 0;
}

private static void addMatch(SearchParameters sp, List<Match> matches,
DataGridView dataGrid, DataTable dataSource, Match match)
{
matches.Add(match);

//add matches in batches to limit flicker
var batch = matches.Where(x => !x.IsAdded);
if (batch.Count() < 20) { return; }

var vScroll = Math.Max(0, dataGrid.FirstDisplayedScrollingRowIndex);
MultiThread.SetProperty(dataGrid, "Visible", false);
MultiThread.SetProperty(dataGrid, "DataSource", null);

foreach (var m in batch)
{
var newRow = dataSource.NewRow();
new RowData(m, sp.SearchType).BuildRow(newRow, dataSource.Columns, m.FirstMatch);
dataSource.Rows.Add(newRow);
m.IsAdded = true;
}

MultiThread.SetProperty(dataGrid, "DataSource", dataSource);
MultiThread.SetProperty(dataGrid, "FirstDisplayedScrollingRowIndex", vScroll);
MultiThread.SetProperty(dataGrid, "Visible", true);
}


/// <summary>
/// Return true if file should be skipped and not searched.
/// </summary>
/// <param name="sp"></param>
/// <param name="fi"></param>
/// <returns></returns>
private static bool SkipFile(SearchParameters sp, FileInfo fi)
private static bool skipFile(SearchParameters sp, FileInfo fi)
{

var skip = sp.SkipExtensions();
Expand Down Expand Up @@ -230,18 +261,21 @@ public class Match
public FileInfo FileInfo { get; set; }
public DirectoryInfo DirectoryInfo { get; set; }
public string FirstMatch { get; set; }
public bool IsAdded { get; set; }

public Match(FileInfo fileInfo, string firstMatch)
{
FileInfo = fileInfo;
FirstMatch = firstMatch;
DirectoryInfo = null;
IsAdded = false;
}
public Match(DirectoryInfo directoryInfo)
{
FileInfo = null;
FirstMatch = null;
DirectoryInfo = directoryInfo;
IsAdded = false;
}
}

Expand Down Expand Up @@ -379,12 +413,12 @@ public void BuildRow(DataRow row, DataColumnCollection columns, string matchLine
case SearchType.FileName:
case SearchType.FileContents:
{
PopulateFileRow(row, columns);
populateFileRow(row, columns);
break;
}
case SearchType.Foldername:
{
PopulateDirectoryRow(row, columns);
populateDirectoryRow(row, columns);
break;
}
}
Expand All @@ -396,7 +430,7 @@ public void BuildRow(DataRow row, DataColumnCollection columns, string matchLine
}


private void PopulateFileRow(DataRow row, DataColumnCollection columns)
private void populateFileRow(DataRow row, DataColumnCollection columns)
{
foreach (var prop in _fileInfo.GetType().GetProperties())
{
Expand All @@ -407,7 +441,7 @@ private void PopulateFileRow(DataRow row, DataColumnCollection columns)
}
}

private void PopulateDirectoryRow(DataRow row, DataColumnCollection columns)
private void populateDirectoryRow(DataRow row, DataColumnCollection columns)
{
foreach (var prop in _directoryInfo.GetType().GetProperties())
{
Expand Down

0 comments on commit 2ad57a1

Please sign in to comment.