Exemple #1
0
        /// <summary>
        /// Handle UP arrow key to get the previous command executed
        /// </summary>
        void HandleUpArrow()
        {
            if (!cmdhistory.PreviousAvailable())
            {
                return;
            }

            cmdhistory.Update(buffer.ToString());

            string value = cmdhistory.Previous();

            if (value != null)
            {
                // If max window size is reached, start writing on the next line
                if (buffer.Length > (Console.WindowWidth - prompt.Length))
                {
                    // Calculate the number of lines based on the buffer length and window size
                    int line = ((buffer.Length + prompt.Length) / Console.BufferWidth);

                    if (cursorPtr > (Console.BufferWidth - prompt.Length))
                    {
                        ClearConsoleLine();
                        if (Console.CursorTop - line >= 0)
                        {
                            Console.SetCursorPosition(prompt.Length, Console.CursorTop - line);
                        }
                        ClearConsoleLine();
                    }
                    else
                    {
                        if (Console.CursorTop + 1 <= Console.BufferHeight)
                        {
                            Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop + 1);
                        }
                        ClearConsoleLine();
                        if (Console.CursorTop - 1 >= 0)
                        {
                            Console.SetCursorPosition(0, Console.CursorTop - 1);
                        }
                        if (Console.CursorTop - line >= 0)
                        {
                            Console.SetCursorPosition(prompt.Length, Console.CursorTop - line);
                        }
                    }
                }
                else
                {
                    ClearConsoleLine();
                }
                SetText(value);
            }
        }
        /// <summary>
        /// Clear screen and display history for commands for Arrow keys.
        /// </summary>
        internal static void ProcessArrowKeys(byte outbyte)
        {
            // if '65' then UP arror, else if '66' then down arrow.
            if (UpArrowPresent(outbyte) || DownArrowPresent(outbyte))
            {
                if (UpArrowPresent(outbyte) && historySerial.PreviousAvailable() || DownArrowPresent(outbyte))
                {
                    //clear the previous command
                    Clear(_userCommandInput.Count);

                    //Clear the input buffer
                    _userCommandInput.Clear();

                    byte[] outputData = null;

                    if (UpArrowPresent(outbyte))
                    {
                        string previous = GetPrevious();
                        if (!String.IsNullOrEmpty(previous))
                        {
                            // get previous command
                            outputData = Encoding.ASCII.GetBytes(previous);
                        }
                    }
                    else
                    {
                        // get next command
                        string next = GetNext();
                        if (!String.IsNullOrEmpty(next))
                        {
                            outputData = Encoding.ASCII.GetBytes(next);
                        }
                    }

                    // Write to serial if there are any bytes.
                    if (outputData != null)
                    {
                        // write command to output
                        WriteBytestoSerial(outputData);
                    }
                }
            } // end of loop check for Up arrow & Down arrow
        }