-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindInFilesForm.cs
230 lines (212 loc) · 7.61 KB
/
FindInFilesForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
using System.Diagnostics;
using System.Text;
namespace FindInFiles {
public partial class FindInFilesForm : Form {
public FindInFilesForm(string[] args) {
InitializeComponent();
richTextBox.AllowDrop = true;
richTextBox.DragEnter += FindInFilesForm_DragEnter;
richTextBox.DragDrop += FindInFilesForm_DragDrop;
lineRender = new OutputLineRender(richTextBox);
lineParser = new OutputLineParser(this, lineRender);
var settings = Properties.Settings.Default;
var font = settings.FindResultFont;
if (font != null) {
lineRender.Font = font;
}
saveSearchHistoryToolStripMenuItem.Checked = settings.SaveSearchHistory;
comboBoxSearchPath.AddRange(settings.SearchPathHistory);
comboBoxSearchPattern.AddRange(settings.SearchPatternHistory);
textBoxEncoding.Text = defaultEncoding;
var searchPath = false;
for (var index = 0; index < args.Length; index++) {
var arg = args[index];
if (arg == "-E" && index + 1 < args.Length) {
++index;
textBoxEncoding.Text = args[index];
} else if (Util.PathExists(arg).exist) {
searchPath = true;
comboBoxSearchPath.Text = arg;
break;
}
}
if (!searchPath) {
lineRender.AppendText($"drag & drop file or folder to search!{Environment.NewLine}", Color.Gray);
}
}
private async void buttonStart_Click(object sender, EventArgs e) {
var exePath = Util.FindExePath("rg.exe");
if (!File.Exists(exePath)) {
lineRender.AppendText($"ripgrep (rg.exe) not found!{Environment.NewLine}", Color.Red);
return;
}
var searchPath = comboBoxSearchPath.Text.Trim();
if (string.IsNullOrEmpty(searchPath)) {
lineRender.AppendText($"drag & drop file or folder to search!{Environment.NewLine}", Color.Red);
return;
}
var (exist, directory) = Util.PathExists(searchPath);
if (!exist) {
lineRender.AppendText($"file or folder \"{searchPath}\" not exists!{Environment.NewLine}", Color.Red);
return;
}
var pattern = comboBoxSearchPattern.Text;
if (string.IsNullOrEmpty(pattern)) {
lineRender.AppendText($"empty search pattern!{Environment.NewLine}", Color.Red);
return;
}
var argList = new List<string> {
"--json --crlf"
};
var text = textBoxContexLine.Text.Trim();
lineParser.MaxContextLine = 0;
if (int.TryParse(text, out int line) && line > 0) {
lineParser.MaxContextLine = line;
argList.Add($"-C {line}");
}
if (!checkBoxRegex.Checked) {
argList.Add("-F");
}
if (!checkBoxMatchCase.Checked) {
argList.Add("-i");
}
if (checkBoxWholeWord.Checked) {
argList.Add("-w");
}
if (checkBoxMultiline.Checked) {
argList.Add("-U --multiline-dotall");
}
if (checkBoxPcre2.Checked) {
argList.Add("-P");
}
if (checkBoxInvert.Checked) {
argList.Add("-v");
}
text = textBoxEncoding.Text.Trim();
if (!string.IsNullOrEmpty(text) && !text.Equals(defaultEncoding, StringComparison.OrdinalIgnoreCase)) {
argList.Add($"-E \"{text.ToLowerInvariant()}\"");
}
if (directory) {
if (!checkBoxRecursive.Checked) {
argList.Add("-d 1");
}
var items = textBoxGlob.Text.Split(';');
for (var i = 0; i < items.Length; i++) {
var item = items[i].Trim();
if (!string.IsNullOrEmpty(item) && item != "*.*") {
argList.Add($"-g \"{item}\"");
}
}
}
argList.Add($"-e \"{pattern}\"");
argList.Add($"\"{searchPath}\"");
var argument = string.Join(" ", argList);
ResetMatch();
lineRender.AppendText($"{argument}{Environment.NewLine}", Color.Gray);
using (var process = new Process()) {
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
process.StartInfo.StandardErrorEncoding = Encoding.UTF8;
process.StartInfo.FileName = exePath;
process.StartInfo.Arguments = argument;
process.OutputDataReceived += Process_OutputDataReceived;
process.Start();
process.BeginOutputReadLine();
var error = await process.StandardError.ReadToEndAsync();
await process.WaitForExitAsync();
lineParser.Flush();
if (lineParser.TotalMatchCount > 0) {
SaveSearchHistory(searchPath, pattern);
}
if (!string.IsNullOrEmpty(error)) {
lineRender.AppendText($"{error}{Environment.NewLine}", Color.Red);
}
}
}
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e) {
var data = e.Data;
if (!string.IsNullOrEmpty(data)) {
lineParser.Parse(data);
}
}
private readonly OutputLineRender lineRender;
private readonly OutputLineParser lineParser;
private static readonly string defaultEncoding = "Unicode (UTF-8, UTF-16)";
private void ResetMatch() {
var page = lineRender.Reset();
lineParser.Reset(page);
}
private void FindInFilesForm_DragEnter(object? sender, DragEventArgs e) {
if (e.Data != null && e.Data.GetDataPresent(DataFormats.FileDrop)) {
e.Effect = DragDropEffects.Copy;
} else {
e.Effect = DragDropEffects.None;
}
}
private void FindInFilesForm_DragDrop(object? sender, DragEventArgs e) {
if (e.Data != null && e.Data.GetDataPresent(DataFormats.FileDrop)) {
if (e.Data.GetData(DataFormats.FileDrop) is string[] files && files.Length > 0) {
comboBoxSearchPath.Text = files[0];
}
}
}
private void richTextBox_DoubleClick(object sender, EventArgs e) {
lineRender.StartEditor();
}
private void clearFindResultToolStripMenuItem_Click(object sender, EventArgs e) {
lineRender.Clear();
}
private void selectFontToolStripMenuItem_Click(object sender, EventArgs e) {
var font = lineRender.Font;
fontDialog.Font = font;
if (fontDialog.ShowDialog(this) == DialogResult.OK) {
font = fontDialog.Font;
var settings = Properties.Settings.Default;
settings.FindResultFont = font;
settings.Save();
}
lineRender.Font = font;
}
private void fontDialog_Apply(object sender, EventArgs e) {
lineRender.Font = fontDialog.Font;
}
private void FindInFilesForm_FormClosing(object sender, FormClosingEventArgs e) {
Properties.Settings.Default.Save();
}
private const int MaxSearchHistoryCount = 32;
private void SaveSearchHistory(string searchPath, string pattern) {
comboBoxSearchPath.AddToTop(searchPath);
comboBoxSearchPattern.AddToTop(pattern);
var settings = Properties.Settings.Default;
if (settings.SaveSearchHistory) {
var collection = settings.SearchPathHistory;
if (Util.AddHistory(ref collection, searchPath, MaxSearchHistoryCount)) {
settings.SearchPathHistory = collection;
}
collection = settings.SearchPatternHistory;
if (Util.AddHistory(ref collection, pattern, MaxSearchHistoryCount)) {
settings.SearchPatternHistory = collection;
}
}
}
private void saveSearchHistoryToolStripMenuItem_Click(object sender, EventArgs e) {
var save = saveSearchHistoryToolStripMenuItem.Checked;
var settings = Properties.Settings.Default;
settings.SaveSearchHistory = save;
if (!save) {
settings.SearchPathHistory?.Clear();
settings.SearchPatternHistory?.Clear();
}
}
private void clearSearchHistoryToolStripMenuItem_Click(object sender, EventArgs e) {
var settings = Properties.Settings.Default;
settings.SearchPathHistory?.Clear();
settings.SearchPatternHistory?.Clear();
comboBoxSearchPath.Items.Clear();
comboBoxSearchPattern.Items.Clear();
}
}
}