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
|
/* LocateFilesAction.cs
*
* GNOME Do is the legal property of its developers. Please refer to the
* COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.IO;
using System.Collections.Generic;
namespace Do.Universe
{
public class LocateFilesAction : AbstractAction
{
bool allowHidden = false;
uint maxResults = 1000;
public override string Name
{
get { return "Locate Files"; }
}
public override string Description
{
get { return "Search your filesystem using locate."; }
}
public override string Icon
{
get { return "search"; }
}
public override Type[] SupportedItemTypes
{
get {
return new Type[] {
typeof (ITextItem)
};
}
}
public override IItem[] Perform (IItem[] items, IItem[] modifierItems)
{
List<IItem> files;
System.Diagnostics.Process locate;
string query;
files = new List<IItem> ();
query = (items[0] as ITextItem).Text;
locate = new System.Diagnostics.Process ();
locate.StartInfo.FileName = "locate";
locate.StartInfo.Arguments = string.Format ("-i -n {0} \"{1}\"", maxResults, query);
locate.StartInfo.RedirectStandardOutput = true;
locate.StartInfo.UseShellExecute = false;
try {
locate.Start ();
} catch {
Console.Error.WriteLine ("LocateFilesAction error: The program 'locate' could not be found.");
return null;
}
string path;
query = query.ToLower ();
while (null != (path = locate.StandardOutput.ReadLine ())) {
// Disallow hidden directories in the absolute path.
// This gets rid of messy .svn directories and their contents.
if (!allowHidden &&
Path.GetDirectoryName (path).Contains ("/."))
continue;
// Only allow files that contain the query as a substring
// of the file name. It may be faster to use grep, but I've
// tested this and it seems prety snappy.
if (Path.GetFileName (path).ToLower().Contains (query))
files.Add (FileItem.Create (path));
}
files.Sort (new FileItemNameComparer (query));
return files.ToArray ();
}
// Order files by (A) position of query in the file name and
// (B) by name length.
private class FileItemNameComparer : IComparer<IItem>
{
string query;
public FileItemNameComparer (string query)
{
this.query = query.ToLower ();
}
public int Compare (IItem a, IItem b)
{
string a_name_lower, b_name_lower;
int a_score, b_score;
a_name_lower = (a as FileItem).Path;
a_name_lower = Path.GetFileName (a_name_lower).ToLower ();
b_name_lower = (b as FileItem).Path;
b_name_lower = Path.GetFileName (b_name_lower).ToLower ();
a_score = a_name_lower.IndexOf (query);
b_score = b_name_lower.IndexOf (query);
if (a_score == b_score)
return a_name_lower.Length - b_name_lower.Length;
else
return a_score - b_score;
}
}
}
}
|