Exemplo n.º 1
0
        /// <summary>
        /// Handle Tab key, by autocompleting the command
        /// </summary>
        void HandleTab()
        {
            string searchWord;

            if (buffer.ToString().Contains(' '))
            {
                searchWord = buffer.ToString().Split(' ').Last();
            }
            else
            {
                searchWord = buffer.ToString();
            }

            // Send the word string to Auto tab handler
            string[] results = TabHelper.GetTabOptions(searchWord.ToLower());

            if (results == null || results.Length == 0)
            {
                return;
            }

            int  count    = results.Length;
            bool flagDone = false;

            // if there is just one matching string, print it to string.
            if (results.Length == 1)
            {
                WriteText(results[0]);
            }
            else
            {
                int countDisplayedOptions = -1;
                int p = 0;

                // find the first character that differs from user entered string
                for (p = 0; p < results[0].Length; p++)
                {
                    char c = results[0][p];

                    for (int i = 1; i < count; i++)
                    {
                        if (results[i].Length < p)
                        {
                            DisplayTabOptions(countDisplayedOptions, results, searchWord);
                            flagDone = true;
                            break;
                        }

                        if (results[i][p] != c)
                        {
                            DisplayTabOptions(countDisplayedOptions, results, searchWord);
                            flagDone = true;
                            break;
                        }
                    }

                    if (!flagDone)
                    {
                        countDisplayedOptions = p;
                    }
                    else
                    {
                        break;
                    }
                }

                // Handle the case when the command string is a subset of another existing command string
                // Eg. setbladepowerlimit is a subset of setbladepowerlimiton/off
                if (!flagDone)
                {
                    countDisplayedOptions = p - 1;
                    DisplayTabOptions(countDisplayedOptions, results, searchWord);
                }
            }
        }
        /// <summary>
        /// Handle tab auto completion
        /// </summary>
        private static void HandleTab()
        {
            // Get the word last entered in buffer separated by a space.
            string text   = Encoding.UTF8.GetString(_userCommandInput.ToArray(), 0, _userCommandInput.Count);
            string prefix = null;

            if (text.ToString().Contains(' '))
            {
                prefix = text.ToString().Split(' ').Last();
            }
            else
            {
                prefix = text.ToString();
            }

            // Send the word string to Auto tab handler
            string[] completions = TabHelper.GetTabOptions(prefix.ToLower());

            if (completions == null)
            {
                return;
            }

            int ncompletions = completions.Length;

            if (ncompletions == 0)
            {
                return;
            }

            // if there is just one matching string, print it to string.
            if (completions.Length == 1)
            {
                InsertTextAtCursor(completions[0]);
                AddToBuffer(completions[0]);
            }
            else
            {
                int last = -1;

                // find the first character that differs from user entered string
                for (int p = 0; p < completions[0].Length; p++)
                {
                    char c = completions[0][p];

                    for (int i = 1; i < ncompletions; i++)
                    {
                        if (completions[i].Length < p)
                        {
                            goto mismatch;
                        }

                        if (completions[i][p] != c)
                        {
                            goto mismatch;
                        }
                    }
                    last = p;
                }
mismatch:
                // if first time and there is string that matches, add to screen
                // example: User entered wcscli -getc followed by tab
                // we can complete it by wcscli -getchassis and also provide other
                // options:  -getchassisinfo -getchassishealth -getchassisattentionledstatus -getchassismanagerstatus -getchassismanagerassetinfo
                // following code handles above scenario
                if (last != -1)
                {
                    InsertTextAtCursor(completions[0].Substring(0, last + 1));
                    AddToBuffer(completions[0].Substring(0, last + 1));
                }

                // Insert New line
                EchoBytesToSerial(0x0A);

                for (int i = 0; i < _userCommandInput.Count + (WcsCliConstants.consoleString.Length + 1); i++)
                {
                    EchoBytesToSerial(0x1b);
                    EchoBytesToSerial(0x5b);
                    EchoBytesToSerial(0x44);
                }

                int count = 0;

                // Print all options to screen
                foreach (string s in completions)
                {
                    InsertTextAtCursor(prefix);
                    count = count + prefix.Length;
                    InsertTextAtCursor(s);
                    count = count + s.Length;
                    InsertTextAtCursor(new string(' ', 1));
                    count = count + 1;
                }

                // Calculate cursor position
                int charToRemove = count % window_width;

                // Insert New line
                EchoBytesToSerial(0x0A);

                // Move cursor left
                for (int i = 0; i < charToRemove; i++)
                {
                    EchoBytesToSerial(0x1b);
                    EchoBytesToSerial(0x5b);
                    EchoBytesToSerial(0x44);
                }

                // print WCSCLI prompt
                InsertTextAtCursor(WcsCliConstants.consoleString + " " + "");
                InsertTextAtCursor(Encoding.UTF8.GetString(_userCommandInput.ToArray(), 0, _userCommandInput.Count));
            }
        }