Skip to content

Commit 9c4bae4

Browse files
committed
fix progress bar
1 parent b7f1c1a commit 9c4bae4

File tree

4 files changed

+66
-6
lines changed

4 files changed

+66
-6
lines changed

GitContentSearch.UI/ViewModels/MainWindowViewModel.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
130130
[ObservableProperty]
131131
private double searchProgress;
132132

133+
[ObservableProperty]
134+
private bool showProgress;
135+
133136
private ObservableCollection<string> _logOutput = new();
134137
public ObservableCollection<string> LogOutput
135138
{
@@ -305,12 +308,15 @@ private async Task BrowseLogDirectoryAsync()
305308
private async Task StartSearchAsync()
306309
{
307310
IsSearching = true;
311+
ShowProgress = true;
312+
SearchProgress = 0;
308313
LogOutput.Clear();
309314
try
310315
{
311316
if (!Directory.Exists(WorkingDirectory))
312317
{
313318
LogOutput.Add($"Error: Working directory '{WorkingDirectory}' does not exist or is invalid.");
319+
ShowProgress = false;
314320
return;
315321
}
316322

@@ -339,13 +345,23 @@ private async Task StartSearchAsync()
339345

340346
var gitContentSearcher = new GitContentSearcher(_gitHelper, _fileSearcher, _fileManager, DisableLinearSearch, writer);
341347

348+
var progress = new Progress<double>(value =>
349+
{
350+
// Ensure UI updates happen on the UI thread
351+
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
352+
{
353+
SearchProgress = value * 100;
354+
});
355+
});
356+
342357
// Since we've already validated in CanStartSearch that FilePath and SearchString are non-empty,
343358
// and EarliestCommit and LatestCommit have default empty string values, we can safely pass them
344359
await Task.Run(() => gitContentSearcher.SearchContent(
345360
FilePath,
346361
SearchString,
347362
EarliestCommit,
348-
LatestCommit));
363+
LatestCommit,
364+
progress));
349365

350366
writer.WriteLine($"GitContentSearch completed at {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
351367
writer.WriteLine(new string('=', 50));
@@ -361,6 +377,8 @@ await Task.Run(() => gitContentSearcher.SearchContent(
361377
{
362378
LogOutput.Add($"Inner Error: {ex.InnerException.Message}");
363379
}
380+
SearchProgress = 0;
381+
ShowProgress = false;
364382
}
365383
finally
366384
{

GitContentSearch.UI/Views/MainWindow.axaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
<!-- Search Button and Progress -->
8080
<Grid Grid.Row="6" Margin="0,8">
8181
<Grid.ColumnDefinitions>
82-
<ColumnDefinition Width="120"/>
82+
<ColumnDefinition Width="140"/>
8383
<ColumnDefinition Width="*"/>
8484
</Grid.ColumnDefinitions>
8585
<Button Grid.Column="0"
@@ -88,9 +88,10 @@
8888
Command="{Binding StartSearchCommand}"/>
8989
<ProgressBar Grid.Column="1"
9090
Height="4"
91+
Maximum="100"
9192
Value="{Binding SearchProgress}"
92-
IsVisible="{Binding IsSearching}"
93-
Margin="8,0"/>
93+
IsVisible="{Binding ShowProgress}"
94+
Margin="8,0,0,0"/>
9495
</Grid>
9596

9697
<!-- Log Output -->

GitContentSearch/GitContentSearcher.cs

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ public class GitContentSearcher : IGitContentSearcher
99
private readonly IFileManager _fileManager;
1010
private readonly TextWriter _logWriter;
1111
private readonly bool _disableLinearSearch;
12+
private IProgress<double>? _progress;
13+
private double _currentProgress = 0;
1214

1315
public GitContentSearcher(IGitHelper gitHelper, IFileSearcher fileSearcher, IFileManager fileManager, bool disableLinearSearch, TextWriter? logWriter = null)
1416
{
@@ -37,8 +39,12 @@ private bool FileExistsInCurrentCommit(string filePath)
3739
}
3840
}
3941

40-
public void SearchContent(string filePath, string searchString, string earliestCommit = "", string latestCommit = "")
42+
public void SearchContent(string filePath, string searchString, string earliestCommit = "", string latestCommit = "", IProgress<double>? progress = null)
4143
{
44+
_progress = progress;
45+
_currentProgress = 0;
46+
_progress?.Report(0.05); // Initial 5% progress to show activity
47+
4248
if (!FileExistsInCurrentCommit(filePath))
4349
{
4450
_logWriter.WriteLine($"Warning: The file '{filePath}' does not exist in the current commit.");
@@ -53,12 +59,16 @@ public void SearchContent(string filePath, string searchString, string earliestC
5359
if (commits == null || commits.Count == 0)
5460
{
5561
_logWriter.WriteLine("No commits found in the specified range.");
62+
_progress?.Report(1.0);
5663
return;
5764
}
5865

66+
_progress?.Report(0.25); // Commits retrieved
67+
5968
if (commits.FindIndex(c => c.CommitHash == earliestCommit) > commits.FindIndex(c => c.CommitHash == latestCommit))
6069
{
6170
_logWriter.WriteLine("Error: The earliest commit is more recent than the latest commit.");
71+
_progress?.Report(1.0);
6272
return;
6373
}
6474

@@ -69,13 +79,18 @@ public void SearchContent(string filePath, string searchString, string earliestC
6979
int firstMatchIndex = FindFirstMatchIndex(commits, filePath, searchString, lastMatchIndex);
7080

7181
LogResults(firstMatchIndex, lastMatchIndex, commits, searchString);
82+
83+
_progress?.Report(1.0);
7284
}
7385

7486
private int FindFirstMatchIndex(List<Commit> commits, string filePath, string searchString, int lastMatchIndex)
7587
{
7688
int left = 0;
7789
int right = lastMatchIndex; // Use lastMatchIndex as the upper bound
7890
int? firstMatchIndex = null;
91+
// For binary search, we'll make approximately log2(n) comparisons
92+
int expectedSearches = (int)Math.Ceiling(Math.Log2(Math.Max(1, right - left + 1)));
93+
int searchesDone = 0;
7994

8095
while (left <= right)
8196
{
@@ -100,6 +115,12 @@ private int FindFirstMatchIndex(List<Commit> commits, string filePath, string se
100115
_logWriter.WriteLine($"Checked commit: {commit.CommitHash} at {commitTime}, found: {found}");
101116
_logWriter.Flush();
102117

118+
searchesDone++;
119+
// Calculate progress between 62.5% and 100%
120+
double searchProgress = (double)searchesDone / expectedSearches;
121+
_currentProgress = 0.625 + (searchProgress * 0.375);
122+
_progress?.Report(_currentProgress);
123+
103124
if (found)
104125
{
105126
firstMatchIndex = mid;
@@ -121,6 +142,9 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
121142
int left = searchStartIndex == -1 ? 0 : searchStartIndex;
122143
int right = commits.Count - 1;
123144
int? lastMatchIndex = null;
145+
// For binary search, we'll make approximately log2(n) comparisons
146+
int expectedSearches = (int)Math.Ceiling(Math.Log2(Math.Max(1, right - left + 1)));
147+
int searchesDone = 0;
124148

125149
while (left <= right)
126150
{
@@ -145,6 +169,12 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
145169
_logWriter.WriteLine($"Checked commit: {commit.CommitHash} at {commitTime}, found: {found}");
146170
_logWriter.Flush();
147171

172+
searchesDone++;
173+
// Calculate progress between 25% and 62.5%
174+
double searchProgress = (double)searchesDone / expectedSearches;
175+
_currentProgress = 0.25 + (searchProgress * 0.375);
176+
_progress?.Report(_currentProgress);
177+
148178
if (found)
149179
{
150180
lastMatchIndex = mid;
@@ -159,6 +189,9 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
159189
if (linearSearchResult.HasValue)
160190
{
161191
lastMatchIndex = linearSearchResult;
192+
// Update progress to 62.5% since we're done with this phase
193+
_currentProgress = 0.625;
194+
_progress?.Report(_currentProgress);
162195
break;
163196
}
164197
}
@@ -177,6 +210,8 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
177210
int step = reverse ? -1 : 1; // Use step to control direction of iteration
178211
int start = reverse ? right : left;
179212
int end = reverse ? left : right;
213+
int totalSearches = Math.Abs(end - start) + 1;
214+
int searchesDone = 0;
180215

181216
for (int i = start; reverse ? i >= end : i <= end; i += step)
182217
{
@@ -200,6 +235,12 @@ private int FindLastMatchIndex(List<Commit> commits, string filePath, string sea
200235
_logWriter.WriteLine($"Checked commit: {commit.CommitHash} at {commitTime}, found: {found}");
201236
_logWriter.Flush();
202237

238+
searchesDone++;
239+
// Calculate progress for linear search portion
240+
double searchProgress = (double)searchesDone / totalSearches;
241+
_currentProgress = 0.25 + (searchProgress * 0.375);
242+
_progress?.Report(_currentProgress);
243+
203244
_fileManager.DeleteTempFile(tempFileName); // Always clean up the temp file
204245

205246
if (found)

GitContentSearch/Interfaces/IGitContentSearcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ namespace GitContentSearch
88
{
99
public interface IGitContentSearcher
1010
{
11-
void SearchContent(string filePath, string searchString, string earliestCommit = "", string latestCommit = "");
11+
void SearchContent(string filePath, string searchString, string earliestCommit = "", string latestCommit = "", IProgress<double>? progress = null);
1212
}
1313
}

0 commit comments

Comments
 (0)