CommandAlias.cs
 1  // Copyright (c) Microsoft Corporation
 2  // The Microsoft Corporation licenses this file to you under the MIT license.
 3  // See the LICENSE file in the project root for more information.
 4  
 5  using System.Text.Json.Serialization;
 6  
 7  namespace Microsoft.CmdPal.UI.ViewModels;
 8  
 9  public class CommandAlias
10  {
11      public string CommandId { get; set; }
12  
13      public string Alias { get; set; }
14  
15      public bool IsDirect { get; set; }
16  
17      [JsonIgnore]
18      public string SearchPrefix => Alias + (IsDirect ? string.Empty : " ");
19  
20      public CommandAlias(string shortcut, string commandId, bool direct = false)
21      {
22          CommandId = commandId;
23          Alias = shortcut;
24          IsDirect = direct;
25      }
26  
27      public CommandAlias()
28          : this(string.Empty, string.Empty, false)
29      {
30      }
31  
32      public static CommandAlias FromSearchText(string text, string commandId)
33      {
34          var trailingSpace = text.EndsWith(' ');
35          var realAlias = trailingSpace ? text.Substring(0, text.Length - 1) : text;
36          return new CommandAlias(realAlias, commandId, !trailingSpace);
37      }
38  }