Пример #1
0
        public override string ReadLine(string p, bool isCommand, bool e)
        {
            m_cursorXPosition = 0;
            prompt            = p;
            m_echo            = e;
            int historyLine = m_history.Count;

            SetCursorLeft(0);          // Needed for mono
            System.Console.Write(" "); // Needed for mono

            lock (m_commandLine)
            {
                m_cursorYPosition = System.Console.CursorTop;
                m_commandLine.Remove(0, m_commandLine.Length);
            }

            while (true)
            {
                Show();

                ConsoleKeyInfo key         = System.Console.ReadKey(true);
                char           enteredChar = key.KeyChar;

                if (!Char.IsControl(enteredChar))
                {
                    if (m_cursorXPosition >= 318)
                    {
                        continue;
                    }

                    if (enteredChar == '?' && isCommand)
                    {
                        if (ContextHelp())
                        {
                            continue;
                        }
                    }

                    m_commandLine.Insert(m_cursorXPosition, enteredChar);
                    m_cursorXPosition++;
                }
                else
                {
                    switch (key.Key)
                    {
                    case ConsoleKey.Backspace:
                        if (m_cursorXPosition == 0)
                        {
                            break;
                        }
                        m_commandLine.Remove(m_cursorXPosition - 1, 1);
                        m_cursorXPosition--;

                        SetCursorLeft(0);
                        m_cursorYPosition = SetCursorTop(m_cursorYPosition);

                        if (m_echo)
                        {
                            System.Console.Write("{0}{1} ", prompt, m_commandLine);
                        }
                        else
                        {
                            System.Console.Write("{0}", prompt);
                        }

                        break;

                    case ConsoleKey.Delete:
                        if (m_cursorXPosition == m_commandLine.Length)
                        {
                            break;
                        }

                        m_commandLine.Remove(m_cursorXPosition, 1);

                        SetCursorLeft(0);
                        m_cursorYPosition = SetCursorTop(m_cursorYPosition);

                        if (m_echo)
                        {
                            System.Console.Write("{0}{1} ", prompt, m_commandLine);
                        }
                        else
                        {
                            System.Console.Write("{0}", prompt);
                        }

                        break;

                    case ConsoleKey.End:
                        m_cursorXPosition = m_commandLine.Length;
                        break;

                    case ConsoleKey.Home:
                        m_cursorXPosition = 0;
                        break;

                    case ConsoleKey.UpArrow:
                        if (historyLine < 1)
                        {
                            break;
                        }
                        historyLine--;
                        LockOutput();
                        m_commandLine.Remove(0, m_commandLine.Length);
                        m_commandLine.Append(m_history[historyLine]);
                        m_cursorXPosition = m_commandLine.Length;
                        UnlockOutput();
                        break;

                    case ConsoleKey.DownArrow:
                        if (historyLine >= m_history.Count)
                        {
                            break;
                        }
                        historyLine++;
                        LockOutput();
                        if (historyLine == m_history.Count)
                        {
                            m_commandLine.Remove(0, m_commandLine.Length);
                        }
                        else
                        {
                            m_commandLine.Remove(0, m_commandLine.Length);
                            m_commandLine.Append(m_history[historyLine]);
                        }
                        m_cursorXPosition = m_commandLine.Length;
                        UnlockOutput();
                        break;

                    case ConsoleKey.LeftArrow:
                        if (m_cursorXPosition > 0)
                        {
                            m_cursorXPosition--;
                        }
                        break;

                    case ConsoleKey.RightArrow:
                        if (m_cursorXPosition < m_commandLine.Length)
                        {
                            m_cursorXPosition++;
                        }
                        break;

                    case ConsoleKey.Enter:
                        SetCursorLeft(0);
                        m_cursorYPosition = SetCursorTop(m_cursorYPosition);

                        System.Console.WriteLine();
                        //Show();

                        lock (m_commandLine)
                        {
                            m_cursorYPosition = -1;
                        }

                        string commandLine = m_commandLine.ToString();

                        if (isCommand)
                        {
                            string[] cmd = Commands.Resolve(Parser.Parse(commandLine));

                            if (cmd.Length != 0)
                            {
                                int index;

                                for (index = 0; index < cmd.Length; index++)
                                {
                                    if (cmd[index].Contains(" "))
                                    {
                                        cmd[index] = "\"" + cmd[index] + "\"";
                                    }
                                }
                                AddToHistory(String.Join(" ", cmd));
                                return(String.Empty);
                            }
                        }

                        // If we're not echoing to screen (e.g. a password) then we probably don't want it in history
                        if (m_echo && commandLine != "")
                        {
                            AddToHistory(commandLine);
                        }

                        return(commandLine);

                    default:
                        break;
                    }
                }
            }
        }