Beispiel #1
0
        public string Edit(string prompt, string initial)
        {
            _editThread       = Thread.CurrentThread;
            _searching        = 0;
            C.CancelKeyPress += InterruptEdit;

            _done = false;
            _history.CursorToEnd();
            _maxRendered = 0;

            Prompt       = prompt;
            _shownPrompt = prompt;
            InitText(initial);
            _history.Append(initial);

            do
            {
                try
                {
                    EditLoop();
                }
                catch (ThreadAbortException)
                {
                    _searching = 0;
                    Thread.ResetAbort();
                    SimpleConsole.Wl();
                    SetPrompt(prompt);
                    SetText("");
                }
            } while (!_done);
            SimpleConsole.Wl();

            C.CancelKeyPress -= InterruptEdit;

            if (_text == null)
            {
                _history.Close();
                return(null);
            }

            var result = _text.ToString();

            if (result != "")
            {
                _history.Accept(result);
            }
            else
            {
                _history.RemoveLast();
            }

            return(result);
        }
Beispiel #2
0
        private void CmdDeleteChar()
        {
            // If there is no input, this behaves like EOF
            if (_text.Length == 0)
            {
                _done = true;
                _text = null;
                SimpleConsole.Wl();
                return;
            }

            if (_cursor == _text.Length)
            {
                return;
            }
            _text.Remove(_cursor, 1);
            ComputeRendered();
            RenderAfter(_cursor);
        }
Beispiel #3
0
        private void Render()
        {
            SimpleConsole.W(_shownPrompt);
            SimpleConsole.W(_renderedText.ToString());

            var max = Math.Max(_renderedText.Length + _shownPrompt.Length, _maxRendered);

            for (var i = _renderedText.Length + _shownPrompt.Length; i < _maxRendered; i++)
            {
                C.Write(' ');
            }
            _maxRendered = _shownPrompt.Length + _renderedText.Length;

            // Write one more to ensure that we always wrap around properly if we are at the
            // end of a line.
            C.Write(' ');

            UpdateHomeRow(max);
        }
Beispiel #4
0
        private void CmdTabOrComplete()
        {
            var complete = false;

            if (AutoCompleteEvent != null)
            {
                if (TabAtStartCompletes)
                {
                    complete = true;
                }
                else
                {
                    for (var i = 0; i < _cursor; i++)
                    {
                        if (char.IsWhiteSpace(_text[i]))
                        {
                            continue;
                        }
                        complete = true;
                        break;
                    }
                }

                if (complete)
                {
                    var completion  = AutoCompleteEvent(_text.ToString(), _cursor);
                    var completions = completion.Result;
                    if (completions == null)
                    {
                        return;
                    }

                    var ncompletions = completions.Length;
                    if (ncompletions == 0)
                    {
                        return;
                    }

                    if (completions.Length == 1)
                    {
                        InsertTextAtCursor(completions[0]);
                    }
                    else
                    {
                        var last = -1;

                        for (var p = 0; p < completions[0].Length; p++)
                        {
                            var c = completions[0][p];


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

                                if (completions[i][p] != c)
                                {
                                    goto mismatch;
                                }
                            }
                            last = p;
                        }
mismatch:
                        if (last != -1)
                        {
                            InsertTextAtCursor(completions[0].Substring(0, last + 1));
                        }
                        SimpleConsole.Wl();
                        foreach (string s in completions)
                        {
                            SimpleConsole.W(completion.Prefix);
                            SimpleConsole.W(s);
                            C.Write(' ');
                        }
                        SimpleConsole.Wl();
                        Render();
                        ForceCursor(_cursor);
                    }
                }
                else
                {
                    HandleChar('\t');
                }
            }
            else
            {
                HandleChar('\t');
            }
        }