Improve code quality

This commit is contained in:
Jack251970 2025-09-04 17:26:19 +08:00
parent 56bdfa72d1
commit f848d1b7b2

View file

@ -7,7 +7,7 @@ namespace Flow.Launcher.Plugin.Url
public class Main : IPlugin, IPluginI18n public class Main : IPlugin, IPluginI18n
{ {
//based on https://gist.github.com/dperini/729294 //based on https://gist.github.com/dperini/729294
private const string urlPattern = "^" + private const string UrlPattern = "^" +
// protocol identifier // protocol identifier
"(?:(?:https?|ftp)://|)" + "(?:(?:https?|ftp)://|)" +
// user:pass authentication // user:pass authentication
@ -39,7 +39,7 @@ namespace Flow.Launcher.Plugin.Url
// resource path // resource path
"(?:/\\S*)?" + "(?:/\\S*)?" +
"$"; "$";
private readonly Regex reg = new Regex(urlPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase); private readonly Regex UrlRegex = new(UrlPattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
public static PluginInitContext Context { get; private set; } public static PluginInitContext Context { get; private set; }
@ -50,9 +50,9 @@ namespace Flow.Launcher.Plugin.Url
var raw = query.Search; var raw = query.Search;
if (IsURL(raw)) if (IsURL(raw))
{ {
return new List<Result> return
{ [
new Result new()
{ {
Title = raw, Title = raw,
SubTitle = string.Format(Context.API.GetTranslation("flowlauncher_plugin_url_open_url"),raw), SubTitle = string.Format(Context.API.GetTranslation("flowlauncher_plugin_url_open_url"),raw),
@ -60,7 +60,7 @@ namespace Flow.Launcher.Plugin.Url
Score = 8, Score = 8,
Action = _ => Action = _ =>
{ {
if (!raw.ToLower().StartsWith("http")) if (!raw.StartsWith("http", StringComparison.OrdinalIgnoreCase))
{ {
raw = "http://" + raw; raw = "http://" + raw;
} }
@ -77,16 +77,17 @@ namespace Flow.Launcher.Plugin.Url
} }
} }
} }
}; ];
} }
return new List<Result>(0);
return [];
} }
public bool IsURL(string raw) public bool IsURL(string raw)
{ {
raw = raw.ToLower(); raw = raw.ToLower();
if (reg.Match(raw).Value == raw) return true; if (UrlRegex.Match(raw).Value == raw) return true;
if (raw == "localhost" || raw.StartsWith("localhost:") || if (raw == "localhost" || raw.StartsWith("localhost:") ||
raw == "http://localhost" || raw.StartsWith("http://localhost:") || raw == "http://localhost" || raw.StartsWith("http://localhost:") ||