-
Notifications
You must be signed in to change notification settings - Fork 1
/
Main.cs
182 lines (169 loc) · 6.26 KB
/
Main.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
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Text;
using System.Collections.Generic;
using Flow.Launcher.Plugin.FendCalculator.ViewModels;
using Flow.Launcher.Plugin.FendCalculator.Views;
namespace Flow.Launcher.Plugin.FendCalculator
{
/// <Summary>
/// Fend Calculator Plugin logic
/// </Summary>
public class FendCalculator : IPlugin, IPluginI18n, ISettingProvider
{
private PluginInitContext _context;
private Settings _settings;
private static SettingsViewModel _viewModel;
private readonly string[] FEND_PATHS = { "fend", "C:\\Program Files\\fend\\bin\\fend.exe", "C:\\Program Files (x86)\\fend\\bin\\fend.exe" };
private enum ExitCode
{
Success = 0,
Timeout = 258
}
/// <Summary>
/// Runs on plugin intialisation.
/// Ensures fend command is not empty
/// </Summary>
public void Init(PluginInitContext context)
{
_context = context;
_settings = context.API.LoadSettingJsonStorage<Settings>();
_viewModel = new SettingsViewModel(_settings);
updateFendPath();
}
/// <Summary>
/// Runs each query through fend calculator and displays the results
/// </Summary>
public List<Result> Query(Query query)
{
var results = new List<Result> { };
if (string.IsNullOrEmpty(query.Search))
{
return results;
}
updateFendPath();
if (string.IsNullOrEmpty(_settings.FendCommand))
{
// TODO: Prompt for install
return results;
}
try
{
(string output, int exitCode) = invokeFend(_settings.FendCommand, query.Search);
if (string.IsNullOrEmpty(output))
{
return results;
}
else if (exitCode == (int)ExitCode.Success)
{
results.Add(new Result
{
Title = output,
SubTitle = _context.API.GetTranslation("flowlauncher_plugin_fend_calculator_copy"),
IcoPath = "Images/calculator.png",
Score = 300,
CopyText = output,
Action = c =>
{
try
{
Clipboard.SetDataObject(output);
return true;
}
catch (ExternalException)
{
MessageBox.Show(_context.API.GetTranslation("flowlauncher_plugin_fend_calculator_copy_error"));
return false;
}
}
});
}
else if (exitCode == (int)ExitCode.Timeout)
{
results.Add(new Result
{
Title = _context.API.GetTranslation("flowlauncher_plugin_fend_calculator_timeout_title"),
SubTitle = _context.API.GetTranslation("flowlauncher_plugin_fend_calculator_timeout_description") + ": " + _settings.Timeout + "ms",
IcoPath = "Images/calculator.png",
Score = 300,
});
}
}
catch (ExternalException)
{
results.Add(new Result
{
Title = _context.API.GetTranslation("flowlauncher_plugin_fend_calculator_error_title"),
SubTitle = _context.API.GetTranslation("flowlauncher_plugin_fend_calculator_error_description"),
IcoPath = "Images/calculator.png",
Score = 300
});
}
return results;
}
/// <Summary>
/// Invokes fend with presets
/// </Summary>
private (string output, int exitCode) invokeFend(string fendPath, string query)
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardOutput = true,
StandardOutputEncoding = Encoding.UTF8,
FileName = fendPath,
Arguments = $"\"{query}\""
};
Process process = Process.Start(startInfo);
if (!process.WaitForExit( _settings.Timeout )) {
process.Kill();
return ("Computation Timeout: 5000ms", (int)ExitCode.Timeout );
}
string output = process.StandardOutput.ReadToEnd().TrimEnd();
return (output, process.ExitCode);
}
/// <Summary>
/// Automatically detect fend if no path is configured
/// </Summary>
private void updateFendPath()
{
if (string.IsNullOrEmpty(_settings.FendCommand))
{
foreach (string fend_path in FEND_PATHS)
{
try
{
invokeFend(fend_path, "1+1");
_settings.FendCommand = fend_path;
break;
}
catch (ExternalException) { }
}
}
}
/// <Summary>
/// Translates the plugin title
/// </Summary>
public string GetTranslatedPluginTitle()
{
return _context.API.GetTranslation("flowlauncher_plugin_fend_calculator_plugin_name");
}
/// <Summary>
/// Translates the plugin description
/// </Summary>
public string GetTranslatedPluginDescription()
{
return _context.API.GetTranslation("flowlauncher_plugin_fend_calculator_plugin_description");
}
/// <Summary>
/// Creates the setting panel
/// </Summary>
public Control CreateSettingPanel()
{
return new FendCalculatorSettings(_viewModel);
}
}
}