Program plugin: simple refactoring

This commit is contained in:
bao-qian 2016-07-21 20:49:01 +01:00
parent 0816fdf318
commit 10f8941258

View file

@ -27,8 +27,15 @@ namespace Wox.Plugin.Program
{ {
_settingsStorage = new PluginJsonStorage<Settings>(); _settingsStorage = new PluginJsonStorage<Settings>();
_settings = _settingsStorage.Load(); _settings = _settingsStorage.Load();
_cacheStorage = new BinaryStorage<ProgramIndexCache>();
_cache = _cacheStorage.Load(); Stopwatch.Debug("Preload programs", () =>
{
_cacheStorage = new BinaryStorage<ProgramIndexCache>();
_cache = _cacheStorage.Load();
_programs = _cache.Programs;
});
Log.Info($"Preload {_programs.Count} programs from cache");
Stopwatch.Debug("Program Index", IndexPrograms);
} }
public void Save() public void Save()
@ -41,29 +48,35 @@ namespace Wox.Plugin.Program
{ {
var results = _programs.AsParallel() var results = _programs.AsParallel()
.Where(p => Score(p, query.Search) > 0) .Where(p => Score(p, query.Search) > 0)
.Select(ScoreFilter)
.OrderByDescending(p => p.Score) .OrderByDescending(p => p.Score)
.Select(p => new Result .Select(ResultFromProgram)
{ .ToList();
Title = p.Title,
SubTitle = p.Path,
IcoPath = p.IcoPath,
Score = p.Score,
ContextData = p,
Action = e =>
{
var info = new ProcessStartInfo
{
FileName = p.Path,
WorkingDirectory = p.Directory
};
var hide = StartProcess(info);
return hide;
}
}).ToList();
return results; return results;
} }
public Result ResultFromProgram(Program p)
{
var result = new Result
{
Title = p.Title,
SubTitle = p.Path,
IcoPath = p.IcoPath,
Score = p.Score,
ContextData = p,
Action = e =>
{
var info = new ProcessStartInfo
{
FileName = p.Path,
WorkingDirectory = p.Directory
};
var hide = StartProcess(info);
return hide;
}
};
return result;
}
private int Score(Program program, string query) private int Score(Program program, string query)
{ {
var score1 = StringMatcher.Score(program.Title, query); var score1 = StringMatcher.Score(program.Title, query);
@ -77,39 +90,26 @@ namespace Wox.Plugin.Program
public void Init(PluginInitContext context) public void Init(PluginInitContext context)
{ {
_context = context; _context = context;
Stopwatch.Debug("Preload programs", () =>
{
_programs = _cache.Programs;
});
Log.Info($"Preload {_programs.Count} programs from cache");
Stopwatch.Debug("Program Index", IndexPrograms);
} }
public static void IndexPrograms() public static void IndexPrograms()
{ {
var sources = DefaultProgramSources(); var sources = ProgramSources();
if (_settings.ProgramSources != null &&
_settings.ProgramSources.Count(o => o.Enabled) > 0)
{
sources.AddRange(_settings.ProgramSources);
}
_programs = sources.AsParallel() var programs = sources.AsParallel()
.SelectMany(s => s.LoadPrograms()) .SelectMany(s => s.LoadPrograms())
// filter duplicate program // filter duplicate program
.GroupBy(x => new { ExecutePath = x.Path, ExecuteName = x.ExecutableName }) .GroupBy(x => new { ExecutePath = x.Path, ExecuteName = x.ExecutableName })
.Select(g => g.First()) .Select(g => g.First());
.ToList(); programs = programs.Select(ScoreFilter);
_programs = programs.ToList();
_cache.Programs = _programs; _cache.Programs = _programs;
} }
/// <summary> private static List<ProgramSource> ProgramSources()
/// Load program sources that wox always provide
/// </summary>
private static List<ProgramSource> DefaultProgramSources()
{ {
var list = new List<ProgramSource> var sources = new List<ProgramSource>
{ {
new CommonStartMenuProgramSource new CommonStartMenuProgramSource
{ {
@ -127,33 +127,46 @@ namespace Wox.Plugin.Program
Enabled = _settings.EnableRegistrySource, Enabled = _settings.EnableRegistrySource,
} }
}; };
return list;
if (_settings.ProgramSources.Count(o => o.Enabled) > 0)
{
sources.AddRange(_settings.ProgramSources);
}
return sources;
} }
private Program ScoreFilter(Program p) private static Program ScoreFilter(Program p)
{ {
p.Score += p.Source.BonusPoints; p.Score += p.Source.BonusPoints;
var start = new[] { "启动", "start" };
var doc = new[] { "帮助", "help", "文档", "documentation" };
var uninstall = new[] { "卸载", "uninstall" };
if (p.Title.Contains("启动") || p.Title.ToLower().Contains("start")) var contained = start.Any(s => p.Title.ToLower().Contains(s));
if (contained)
{
p.Score += 10; p.Score += 10;
}
if (p.Title.Contains("帮助") || p.Title.ToLower().Contains("help") || p.Title.Contains("文档") || p.Title.ToLower().Contains("documentation")) contained = doc.Any(d => p.Title.ToLower().Contains(d));
if (contained)
{
p.Score -= 10; p.Score -= 10;
}
if (p.Title.Contains("卸载") || p.Title.ToLower().Contains("uninstall")) contained = uninstall.Any(u => p.Title.ToLower().Contains(u));
if (contained)
{
p.Score -= 20; p.Score -= 20;
}
return p; return p;
} }
#region ISettingProvider Members
public Control CreateSettingPanel() public Control CreateSettingPanel()
{ {
return new ProgramSetting(_context, _settings); return new ProgramSetting(_context, _settings);
} }
#endregion
public string GetTranslatedPluginTitle() public string GetTranslatedPluginTitle()
{ {
return _context.API.GetTranslation("wox_plugin_program_plugin_name"); return _context.API.GetTranslation("wox_plugin_program_plugin_name");