Flow.Launcher/Flow.Launcher.Plugin/Query.cs

128 lines
5 KiB
C#
Raw Normal View History

using System;
using System.Text.Json.Serialization;
2013-12-19 15:51:20 +00:00
2020-04-21 09:12:17 +00:00
namespace Flow.Launcher.Plugin
2013-12-19 15:51:20 +00:00
{
2025-02-24 07:37:13 +00:00
/// <summary>
/// Represents a query that is sent to a plugin.
/// </summary>
2013-12-19 15:51:20 +00:00
public class Query
{
/// <summary>
/// Original query, exactly how the user has typed into the search box.
/// We don't recommend using this property directly. You should always use Search property.
/// </summary>
public string OriginalQuery { get; internal init; }
/// <summary>
/// Raw query, this includes action keyword if it has.
/// It has handled built-in custom query hotkeys and built-in shortcuts, and it trims the whitespace.
/// We don't recommend using this property directly. You should always use Search property.
/// </summary>
[Obsolete("RawQuery is renamed to TrimmedQuery. This property will be removed. Update the code to use TrimmedQuery instead.")]
public string RawQuery {
get => TrimmedQuery;
internal init { TrimmedQuery = value; }
}
/// <summary>
/// Original query but with trimmed whitespace. Includes action keyword.
/// It has handled built-in custom query hotkeys and build-in shortcuts.
/// If you need the exact original query from the search box, use OriginalQuery property instead.
/// We don't recommend using this property directly. You should always use Search property.
/// </summary>
public string TrimmedQuery { get; internal init; }
/// <summary>
/// Determines whether the query was forced to execute again.
/// For example, the value will be true when the user presses Ctrl + R.
/// When this property is true, plugins handling this query should avoid serving cached results.
/// </summary>
public bool IsReQuery { get; internal set; } = false;
/// <summary>
/// Determines whether the query is a home query.
/// </summary>
public bool IsHomeQuery { get; internal init; } = false;
/// <summary>
/// Search part of a query.
/// This will not include action keyword if exclusive plugin gets it, otherwise it should be same as TrimmedQuery.
2023-06-18 19:29:40 +00:00
/// Since we allow user to switch a exclusive plugin to generic plugin,
2015-10-31 16:02:56 +00:00
/// so this property will always give you the "real" query part of the query
/// </summary>
public string Search { get; internal init; }
2015-10-31 16:02:56 +00:00
/// <summary>
/// The search string split into a string array.
2023-06-18 19:29:40 +00:00
/// Does not include the <see cref="ActionKeyword"/>.
2015-10-31 16:02:56 +00:00
/// </summary>
public string[] SearchTerms { get; init; }
2021-09-21 02:51:07 +00:00
/// <summary>
/// Query can be splited into multiple terms by whitespace
/// </summary>
public const string TermSeparator = " ";
/// <summary>
2025-02-21 04:59:56 +00:00
/// User can set multiple action keywords seperated by whitespace
/// </summary>
2025-02-21 04:59:56 +00:00
public const string ActionKeywordSeparator = TermSeparator;
2015-10-31 16:02:56 +00:00
2015-11-02 00:09:42 +00:00
/// <summary>
2023-06-18 19:29:40 +00:00
/// Wildcard action keyword. Plugins using this value will be queried on every search.
2015-11-02 00:09:42 +00:00
/// </summary>
public const string GlobalPluginWildcardSign = "*";
2015-11-02 00:09:42 +00:00
2023-06-18 19:29:40 +00:00
/// <summary>
/// The action keyword part of this query.
/// For global plugins this value will be empty.
/// </summary>
public string ActionKeyword { get; init; }
/// <summary>
2023-06-18 19:29:40 +00:00
/// Splits <see cref="SearchTerms"/> by spaces and returns the first item.
/// </summary>
2023-06-18 19:29:40 +00:00
/// <remarks>
/// returns an empty string when <see cref="SearchTerms"/> does not have enough items.
/// </remarks>
2025-02-24 07:37:13 +00:00
[JsonIgnore]
2015-10-31 16:02:56 +00:00
public string FirstSearch => SplitSearch(0);
2022-09-01 02:34:47 +00:00
[JsonIgnore]
private string _secondToEndSearch;
/// <summary>
/// strings from second search (including) to last search
/// </summary>
2022-09-01 02:34:47 +00:00
[JsonIgnore]
public string SecondToEndSearch => SearchTerms.Length > 1 ? (_secondToEndSearch ??= string.Join(' ', SearchTerms[1..])) : "";
/// <summary>
2023-06-18 19:29:40 +00:00
/// Splits <see cref="SearchTerms"/> by spaces and returns the second item.
/// </summary>
2023-06-18 19:29:40 +00:00
/// <remarks>
/// returns an empty string when <see cref="SearchTerms"/> does not have enough items.
/// </remarks>
2022-09-01 02:34:47 +00:00
[JsonIgnore]
2015-10-31 16:02:56 +00:00
public string SecondSearch => SplitSearch(1);
/// <summary>
2023-06-18 19:29:40 +00:00
/// Splits <see cref="SearchTerms"/> by spaces and returns the third item.
/// </summary>
2023-06-18 19:29:40 +00:00
/// <remarks>
/// returns an empty string when <see cref="SearchTerms"/> does not have enough items.
/// </remarks>
2022-09-01 02:34:47 +00:00
[JsonIgnore]
2015-10-31 16:02:56 +00:00
public string ThirdSearch => SplitSearch(2);
private string SplitSearch(int index)
{
return index < SearchTerms.Length ? SearchTerms[index] : string.Empty;
}
2023-06-18 19:29:40 +00:00
/// <inheritdoc />
public override string ToString() => TrimmedQuery;
2013-12-19 15:51:20 +00:00
}
}