Skip to content

Commit

Permalink
feat: add timeout
Browse files Browse the repository at this point in the history
closes #3
  • Loading branch information
IsaacTay committed Aug 26, 2023
1 parent 051179c commit 040181f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 19 deletions.
4 changes: 3 additions & 1 deletion Languages/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@
<system:String x:Key="flowlauncher_plugin_fend_calculator_copy_error">Copy failed, please try later</system:String>
<system:String x:Key="flowlauncher_plugin_fend_calculator_error_title">Error</system:String>
<system:String x:Key="flowlauncher_plugin_fend_calculator_error_description">fend command error. Check installation path or fend config file</system:String>
</ResourceDictionary>
<system:String x:Key="flowlauncher_plugin_fend_calculator_timeout_title">Fend Timeout</system:String>
<system:String x:Key="flowlauncher_plugin_fend_calculator_timeout_description">Current Timeout</system:String>
</ResourceDictionary>
34 changes: 28 additions & 6 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ public class FendCalculator : IPlugin, IPluginI18n, ISettingProvider
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
Expand Down Expand Up @@ -53,9 +59,13 @@ public List<Result> Query(Query query)
try
{
(string output, int exitCode) = invokeFend(_settings.FendCommand, query.Search);
if (exitCode == 0 && !string.IsNullOrEmpty(output))
if (string.IsNullOrEmpty(output))
{
return results;
}
else if (exitCode == (int)ExitCode.Success)
{
var result = new Result
results.Add(new Result
{
Title = output,
SubTitle = _context.API.GetTranslation("flowlauncher_plugin_fend_calculator_copy"),
Expand All @@ -75,8 +85,17 @@ public List<Result> Query(Query query)
return false;
}
}
};
results.Add(result);
});
}
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") + ": 5000ms",
IcoPath = "Images/calculator.png",
Score = 300,
});
}
}
catch (ExternalException)
Expand All @@ -88,7 +107,6 @@ public List<Result> Query(Query query)
IcoPath = "Images/calculator.png",
Score = 300
});
return results;
}

return results;
Expand All @@ -109,6 +127,10 @@ public List<Result> Query(Query query)
Arguments = $"\"{query}\""
};
Process process = Process.Start(startInfo);
if (!process.WaitForExit( 5000 )) {
process.Kill();
return ("Computation Timeout: 5000ms", (int)ExitCode.Timeout );
}
string output = process.StandardOutput.ReadToEnd().TrimEnd();
return (output, process.ExitCode);
}
Expand Down Expand Up @@ -157,4 +179,4 @@ public Control CreateSettingPanel()
return new FendCalculatorSettings(_viewModel);
}
}
}
}
24 changes: 12 additions & 12 deletions plugin.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"ID": "E5E5A4BD-39C7-4B41-93F8-0D4850BC317C",
"ActionKeyword": "*",
"Name": "FendCalculator",
"Description": "Arbitrary-precision unit-aware calculator. https://printfn.github.io/fend/",
"Author": "IsaacTay",
"Version": "1.0.2",
"Language": "csharp",
"Website": "https://github.com/IsaacTay/Flow.Launcher.Plugin.FendCalculator",
"IcoPath": "Images\\calculator.png",
"ExecuteFileName": "Flow.Launcher.Plugin.FendCalculator.dll",
"disabled": false
}
"ID": "E5E5A4BD-39C7-4B41-93F8-0D4850BC317C",
"ActionKeyword": "*",
"Name": "FendCalculator",
"Description": "Arbitrary-precision unit-aware calculator. https://printfn.github.io/fend/",
"Author": "IsaacTay",
"Version": "1.1.0",
"Language": "csharp",
"Website": "https://github.com/IsaacTay/Flow.Launcher.Plugin.FendCalculator",
"IcoPath": "Images\\calculator.png",
"ExecuteFileName": "Flow.Launcher.Plugin.FendCalculator.dll",
"disabled": false
}

0 comments on commit 040181f

Please sign in to comment.