Skip to content

Commit e9b301e

Browse files
committed
press Enter to search, Press Escape to clear search, add icon fixes#6, add double click to open file fixes#5, replace new lines on snippet preview fixes#2
1 parent 85a0cbe commit e9b301e

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

FindInFiles/FindInFiles.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<ErrorReport>prompt</ErrorReport>
3333
<WarningLevel>4</WarningLevel>
3434
</PropertyGroup>
35+
<PropertyGroup>
36+
<ApplicationIcon>findinfiles.ico</ApplicationIcon>
37+
</PropertyGroup>
3538
<ItemGroup>
3639
<Reference Include="System" />
3740
<Reference Include="System.Data" />
@@ -92,5 +95,8 @@
9295
<ItemGroup>
9396
<None Include="App.config" />
9497
</ItemGroup>
98+
<ItemGroup>
99+
<Resource Include="findinfiles.ico" />
100+
</ItemGroup>
95101
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
96102
</Project>

FindInFiles/MainWindow.xaml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@
88
mc:Ignorable="d"
99
Title="FindInFiles" Height="392.388" Width="781.747">
1010
<Grid>
11-
<Button x:Name="btnFind" Content="Find" HorizontalAlignment="Left" Margin="242,19,0,0" VerticalAlignment="Top" Width="75" Click="btnFind_Click"/>
12-
<Button x:Name="btnBrowse" Content="..." HorizontalAlignment="Left" Margin="686,17,0,0" VerticalAlignment="Top" Width="75" Click="btnBrowse_Click"/>
13-
<TextBox x:Name="txtString" HorizontalAlignment="Left" Height="23" Margin="10,18,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="227"/>
14-
<TextBox x:Name="txtFolder" HorizontalAlignment="Left" Height="23" Margin="356,15,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="325"/>
15-
<DataGrid x:Name="gridResults" HorizontalAlignment="Left" Height="296" Margin="10,55,0,0" VerticalAlignment="Top" Width="754"/>
11+
<Grid.ColumnDefinitions>
12+
<ColumnDefinition Width="29*"/>
13+
<ColumnDefinition Width="100*"/>
14+
</Grid.ColumnDefinitions>
15+
<Button x:Name="btnFind" Content="Find" HorizontalAlignment="Left" Margin="68.255,19,0,0" VerticalAlignment="Top" Width="75" Click="btnFind_Click" Grid.Column="1"/>
16+
<Button x:Name="btnBrowse" Content="..." HorizontalAlignment="Left" Margin="512.255,17,0,0" VerticalAlignment="Top" Width="75" Click="btnBrowse_Click" Grid.Column="1"/>
17+
<TextBox x:Name="txtSearch" HorizontalAlignment="Left" Height="23" Margin="10,18,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="227" KeyDown="OnKeyDownSearch" Grid.ColumnSpan="2"/>
18+
<TextBox x:Name="txtFolder" HorizontalAlignment="Left" Height="23" Margin="182.255,15,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="325" Grid.Column="1"/>
19+
<DataGrid x:Name="gridResults" HorizontalAlignment="Left" Height="296" Margin="10,55,0,0" VerticalAlignment="Top" Width="754" Grid.ColumnSpan="2" MouseDoubleClick="gridResults_MouseDoubleClick" IsReadOnly="True"/>
1620

1721
</Grid>
1822
</Window>

FindInFiles/MainWindow.xaml.cs

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// simple Find In Files tool for searching files containting given string
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.IO;
56
using System.Linq;
67
using System.Windows;
8+
using System.Windows.Input;
79

810
namespace FindInFiles
911
{
@@ -36,9 +38,9 @@ void Start()
3638

3739
private void btnFind_Click(object sender, RoutedEventArgs e)
3840
{
39-
if (string.IsNullOrEmpty(txtString.Text) == false)
41+
if (string.IsNullOrEmpty(txtSearch.Text) == false)
4042
{
41-
SearchFiles(txtString.Text, txtFolder.Text);
43+
SearchFiles(txtSearch.Text, txtFolder.Text);
4244
}
4345
}
4446

@@ -47,10 +49,38 @@ private void btnBrowse_Click(object sender, RoutedEventArgs e)
4749
// TODO browse for folder
4850
}
4951

52+
// special keys for search field
53+
private void OnKeyDownSearch(object sender, KeyEventArgs e)
54+
{
55+
switch (e.Key)
56+
{
57+
case Key.Escape:
58+
txtSearch.Text = "";
59+
break;
60+
case Key.Return:
61+
SearchFiles(txtSearch.Text, txtFolder.Text);
62+
break;
63+
default:
64+
break;
65+
}
66+
}
67+
68+
// open file on double click
69+
private void gridResults_MouseDoubleClick(object sender, MouseButtonEventArgs e)
70+
{
71+
if (gridResults.SelectedItem == null) return;
72+
var selectedRow = gridResults.SelectedItem as ResultItem;
73+
74+
Process myProcess = new Process();
75+
myProcess.StartInfo.FileName = selectedRow.path;
76+
//myProcess.StartInfo.Arguments = "-n###" ;// TODO jump to line in notepad++, but need to know linenumber..
77+
myProcess.Start();
78+
}
79+
5080
void SearchFiles(string searchString, string sourceFolder)
5181
{
5282
// validate folder
53-
if (Directory.Exists(sourceFolder)==false)
83+
if (Directory.Exists(sourceFolder) == false)
5484
{
5585
return;
5686
}
@@ -74,6 +104,13 @@ void SearchFiles(string searchString, string sourceFolder)
74104
var o = new ResultItem();
75105
o.path = files[i];
76106
o.snippet = wholeFile.Substring(hitIndex, (previewSnippetLength + hitIndex >= wholeFile.Length) ? wholeFile.Length : previewSnippetLength);
107+
o.snippet = o.snippet.Replace('\n', '¶'); // replace end of lines
108+
109+
// TODO get linenumber, or estimate?
110+
// but that would mean have to parse again??
111+
// maybe do this only if selected file.. (but that means need to read file again..)
112+
// o.lineNumber = ...
113+
77114
results.Add(o);
78115
continue;
79116
}
@@ -87,6 +124,7 @@ public class ResultItem
87124
{
88125
public string path { get; set; }
89126
public string snippet { get; set; }
127+
// public int lineNumber { get; set; }
90128
}
91129

92130

FindInFiles/findinfiles.ico

18.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)