1
1
// simple Find In Files tool for searching files containting given string
2
2
using System ;
3
3
using System . Collections . Generic ;
4
+ using System . Collections . Specialized ;
5
+ using System . ComponentModel ;
4
6
using System . Diagnostics ;
5
7
using System . IO ;
6
8
using System . Linq ;
7
9
using System . Windows ;
10
+ using System . Windows . Controls ;
8
11
using System . Windows . Input ;
9
12
10
13
namespace FindInFiles
@@ -16,6 +19,7 @@ public partial class MainWindow : Window
16
19
{
17
20
string [ ] fileExtensions = new [ ] { "*.txt" , "*.shader" , "*.cs" , "*.log" , "*.js" , "*.cging" } ;
18
21
const int previewSnippetLength = 32 ;
22
+ const int maxRecentItems = 32 ;
19
23
20
24
public MainWindow ( )
21
25
{
@@ -31,17 +35,75 @@ void Start()
31
35
// have any args? first item is exe name, skip that
32
36
if ( args . Length > 1 )
33
37
{
34
- txtFolder . Text = args [ 1 ] ;
38
+ cmbFolder . Text = args [ 1 ] ;
35
39
}
36
40
41
+ // window size
42
+ this . Width = Properties . Settings . Default . windowWidth ;
43
+ this . Height = Properties . Settings . Default . windowHeight ;
44
+
45
+ // search history
46
+ //cmbSearch.Items.Add(Properties.Settings.Default.recentSearches.Cast<string[]>());
47
+ cmbSearch . ItemsSource = Properties . Settings . Default . recentSearches ;
48
+
49
+ // focus on searchbox
50
+ cmbSearch . Focus ( ) ;
51
+
52
+ // get close event, so can save settings
53
+ Application . Current . MainWindow . Closing += new CancelEventHandler ( OnWindowClose ) ;
54
+ }
55
+
56
+ // force combobox text to be selected at start https://stackoverflow.com/q/31483650/5452781
57
+ private void cmbSearch_Loaded ( object sender , RoutedEventArgs e )
58
+ {
59
+ ComboBox cmBox = ( System . Windows . Controls . ComboBox ) sender ;
60
+ var textBox = ( cmBox . Template . FindName ( "PART_EditableTextBox" ,
61
+ cmBox ) as TextBox ) ;
62
+ if ( textBox != null )
63
+ {
64
+ textBox . Focus ( ) ;
65
+ textBox . SelectAll ( ) ;
66
+ }
67
+ }
68
+
69
+ void OnWindowClose ( object sender , CancelEventArgs e )
70
+ {
71
+ // save settings
72
+ Properties . Settings . Default . windowWidth = ( int ) this . Width ;
73
+ Properties . Settings . Default . windowHeight = ( int ) this . Height ;
74
+ Properties . Settings . Default . Save ( ) ;
37
75
}
38
76
39
77
private void btnFind_Click ( object sender , RoutedEventArgs e )
40
78
{
41
- if ( string . IsNullOrEmpty ( txtSearch . Text ) == false )
79
+ Search ( cmbSearch . Text , cmbFolder . Text ) ;
80
+ }
81
+
82
+ void AddSearchHistory ( string searchString )
83
+ {
84
+ // handle settings
85
+ if ( Properties . Settings . Default . recentSearches == null )
86
+ {
87
+ Properties . Settings . Default . recentSearches = new StringCollection ( ) ;
88
+ }
89
+
90
+ // remove old items
91
+ if ( Properties . Settings . Default . recentSearches . Count > maxRecentItems )
92
+ {
93
+ Properties . Settings . Default . recentSearches . RemoveAt ( 0 ) ;
94
+ }
95
+
96
+ // skip if duplicate
97
+ if ( Properties . Settings . Default . recentSearches . Contains ( searchString ) == false )
42
98
{
43
- SearchFiles ( txtSearch . Text , txtFolder . Text ) ;
99
+ Properties . Settings . Default . recentSearches . Add ( searchString ) ;
100
+ Console . WriteLine ( "added" ) ;
44
101
}
102
+ Properties . Settings . Default . Save ( ) ;
103
+
104
+ // rebuild dropdown
105
+ cmbSearch . ItemsSource = null ;
106
+ cmbSearch . ItemsSource = Properties . Settings . Default . recentSearches ;
45
107
}
46
108
47
109
private void btnBrowse_Click ( object sender , RoutedEventArgs e )
@@ -55,10 +117,10 @@ private void OnKeyDownSearch(object sender, KeyEventArgs e)
55
117
switch ( e . Key )
56
118
{
57
119
case Key . Escape :
58
- txtSearch . Text = "" ;
120
+ cmbSearch . Text = "" ;
59
121
break ;
60
122
case Key . Return :
61
- SearchFiles ( txtSearch . Text , txtFolder . Text ) ;
123
+ Search ( cmbSearch . Text , cmbFolder . Text ) ;
62
124
break ;
63
125
default :
64
126
break ;
@@ -77,8 +139,14 @@ private void gridResults_MouseDoubleClick(object sender, MouseButtonEventArgs e)
77
139
myProcess . Start ( ) ;
78
140
}
79
141
80
- void SearchFiles ( string searchString , string sourceFolder )
142
+ // main search method
143
+ void Search ( string searchString , string sourceFolder )
81
144
{
145
+ if ( string . IsNullOrEmpty ( searchString ) == true ) return ;
146
+ if ( string . IsNullOrEmpty ( sourceFolder ) == true ) return ;
147
+
148
+ AddSearchHistory ( cmbSearch . Text ) ;
149
+
82
150
// validate folder
83
151
if ( Directory . Exists ( sourceFolder ) == false )
84
152
{
@@ -117,6 +185,12 @@ void SearchFiles(string searchString, string sourceFolder)
117
185
}
118
186
gridResults . ItemsSource = results ;
119
187
}
188
+
189
+ // recent search item selected
190
+ private void cmbSearch_DropDownClosed ( object sender , EventArgs e )
191
+ {
192
+ Search ( cmbSearch . Text , cmbFolder . Text ) ;
193
+ }
120
194
}
121
195
}
122
196
0 commit comments