Beispiel #1
0
        private CommandTreeNode CreateNode(CommandBase command)
        {
            if (command is ActionCommandBase action)
            {
                return(new CommandTreeNode
                {
                    Keyword = action.Keyword,
                    Descrption = action.Description,
                    Command = action,
                    MaxKeywordLength = action.Keyword.Length,
                    MaxDescriptionLength = action.Description?.Length ?? 0
                });
            }

            if (command is ContainerCommandBase container)
            {
                var node = new CommandTreeNode
                {
                    Keyword              = container.Keyword,
                    Descrption           = container.Description,
                    Command              = container,
                    MaxKeywordLength     = container.Keyword.Length,
                    MaxDescriptionLength = container.Description?.Length ?? 0
                };

                foreach (var containerCommand in container.Commands)
                {
                    var commandInstance = Resolve(containerCommand);

                    var commandTreeNode = CreateNode(commandInstance);

                    node.SubNodes.Add(commandTreeNode);

                    if (commandTreeNode.MaxKeywordLength > node.MaxKeywordLength)
                    {
                        node.MaxKeywordLength = commandTreeNode.MaxKeywordLength;
                    }

                    if (commandTreeNode.MaxDescriptionLength > node.MaxDescriptionLength)
                    {
                        node.MaxDescriptionLength = commandTreeNode.MaxDescriptionLength;
                    }
                }

                return(node);
            }

            throw new Exception();
        }
Beispiel #2
0
        public CommandTreeNode FindNode(string input)
        {
            if (string.IsNullOrEmpty(input))
            {
                return(null);
            }

            var parts = input
                        .Split(' ')
                        .Where(x => !string.IsNullOrEmpty(x))
                        .Select(x => x.ToLower())
                        .ToArray();

            CommandTreeNode node = null;

            for (int i = 0; i < parts.Length; i++)
            {
                var part = parts[i].ToLower();

                if (string.IsNullOrEmpty(part))
                {
                    continue;
                }

                if (node == null)
                {
                    node = Nodes.SingleOrDefault(x => x.Keyword == part);
                }
                else
                {
                    node = node.SubNodes.SingleOrDefault(x => x.Keyword == part);
                }

                if (node == null)
                {
                    return(null);
                }
            }

            return(node);
        }
        // text - The current text entered in the console
        // index - The index of the terminal cursor within {text}
        public string[] GetSuggestions(string text, int index)
        {
            if (string.IsNullOrEmpty(text))
            {
                return(_commandTree.Nodes.Select(x => x.Keyword).ToArray());
            }

            CommandTreeNode node = null;

            var parts = text
                        .Split(' ')
                        .Where(x => !string.IsNullOrEmpty(x))
                        .Select(x => x.ToLower())
                        .ToArray();

            for (var i = 0; i < parts.Length; i++)
            {
                var isLast = i == parts.Length - 1;

                var part = parts[i];

                IEnumerable <CommandTreeNode> tNodes;

                if (node == null)
                {
                    //Check for matches
                    tNodes = _commandTree.Nodes.Where(x => x.Keyword.StartsWith(part));
                }
                else
                {
                    //Check for matches
                    tNodes = node.SubNodes.Where(x => x.Keyword.StartsWith(part));
                }

                //If none found then return no suggestions
                if (!tNodes.Any())
                {
                    return(new string[0]);
                }

                //If multiple found and we are on the last keyword then print all suggestions
                if (isLast && tNodes.Count() > 1)
                {
                    return(tNodes.Select(x => x.Keyword).ToArray());
                }
                //If not on last but multiple matches
                else if (tNodes.Count() > 1)
                {
                    //Check for exact match
                    node = tNodes.SingleOrDefault(x => x.Keyword == part);
                }
                else
                {
                    //Other take the single node
                    var tNode = tNodes.Single();

                    //Check for exact match
                    if (tNode.Keyword == part)
                    {
                        node = tNode;
                    }
                    //If we are on the last, then take the single node and display suggestion
                    else if (isLast)
                    {
                        return(new[] { tNode.Keyword });
                    }
                }

                if (node == null)
                {
                    return(new string[0]);
                }
            }

            if (node == null)
            {
                return(new string[0]);
            }

            return(node.SubNodes.Select(x => x.Keyword).ToArray());
        }