mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 11:16:51 +02:00
Add find file plugin.
This commit is contained in:
74
Plugins/Wox.Plugin.FindFile/Main.cs
Normal file
74
Plugins/Wox.Plugin.FindFile/Main.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Serialization.Formatters.Binary;
|
||||
using System.Text;
|
||||
using Wox.Infrastructure;
|
||||
using Wox.Infrastructure.MFTSearch;
|
||||
|
||||
namespace Wox.Plugin.FindFile
|
||||
{
|
||||
public class Main : IPlugin
|
||||
{
|
||||
private PluginInitContext context;
|
||||
private bool initial = false;
|
||||
|
||||
public List<Result> Query(Query query)
|
||||
{
|
||||
if (!initial)
|
||||
{
|
||||
return new List<Result>()
|
||||
{
|
||||
new Result("Wox is indexing your files, please try later.","Images/warning.png")
|
||||
};
|
||||
}
|
||||
|
||||
string q = query.GetAllRemainingParameter();
|
||||
return MFTSearcher.Search(q).Take(100).Select(t => ConvertMFTSearch(t, q)).ToList();
|
||||
}
|
||||
|
||||
public void Init(PluginInitContext context)
|
||||
{
|
||||
this.context = context;
|
||||
var searchtimestart = DateTime.Now;
|
||||
MFTSearcher.IndexAllVolumes();
|
||||
initial = true;
|
||||
var searchtimeend = DateTime.Now;
|
||||
Debug.WriteLine(string.Format("{0} file, {1} folder indexed, {2}ms has spent.", MFTSearcher.IndexedFileCount, MFTSearcher.IndexedFolderCount, searchtimeend.Subtract(searchtimestart).TotalMilliseconds));
|
||||
}
|
||||
|
||||
private Result ConvertMFTSearch(MFTSearchRecord record, string query)
|
||||
{
|
||||
string icoPath = "Images/file.png";
|
||||
if (record.IsFolder)
|
||||
{
|
||||
icoPath = "Images/folder.png";
|
||||
}
|
||||
|
||||
string name = Path.GetFileName(record.FullPath);
|
||||
FuzzyMatcher matcher = FuzzyMatcher.Create(query);
|
||||
return new Result()
|
||||
{
|
||||
Title = name,
|
||||
Score = matcher.Evaluate(name).Score,
|
||||
SubTitle = record.FullPath,
|
||||
IcoPath = icoPath,
|
||||
Action = _ =>
|
||||
{
|
||||
try
|
||||
{
|
||||
Process.Start(record.FullPath);
|
||||
}
|
||||
catch
|
||||
{
|
||||
context.API.ShowMsg("Can't open " + record.FullPath, string.Empty, string.Empty);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user