コード例 #1
0
ファイル: Replace.vi.cs プロジェクト: HBelusca/PSReadLine
        private static void ViReplaceUntilEsc(ConsoleKeyInfo?key, object arg)
        {
            if (_singleton._current >= _singleton._buffer.Length)
            {
                Ding();
                return;
            }

            int           startingCursor = _singleton._current;
            StringBuilder deletedStr     = new StringBuilder();

            ConsoleKeyInfo nextKey = ReadKey();

            while (!nextKey.EqualsNormalized(Keys.Escape) && !nextKey.EqualsNormalized(Keys.Enter))
            {
                if (nextKey.EqualsNormalized(Keys.Backspace))
                {
                    if (_singleton._current == startingCursor)
                    {
                        Ding();
                    }
                    else
                    {
                        if (deletedStr.Length == _singleton._current - startingCursor)
                        {
                            _singleton._buffer[_singleton._current - 1] = deletedStr[deletedStr.Length - 1];
                            deletedStr.Remove(deletedStr.Length - 1, 1);
                        }
                        else
                        {
                            _singleton._buffer.Remove(_singleton._current - 1, 1);
                        }
                        _singleton._current--;
                        _singleton.Render();
                    }
                }
                else if (nextKey.ShouldInsert())
                {
                    if (_singleton._current >= _singleton._buffer.Length)
                    {
                        _singleton._buffer.Append(nextKey.KeyChar);
                    }
                    else
                    {
                        deletedStr.Append(_singleton._buffer[_singleton._current]);
                        _singleton._buffer[_singleton._current] = nextKey.KeyChar;
                    }
                    _singleton._current++;
                    _singleton.Render();
                }
                nextKey = ReadKey();
            }

            if (_singleton._current > startingCursor)
            {
                _singleton.StartEditGroup();
                string insStr = _singleton._buffer.ToString(startingCursor, _singleton._current - startingCursor);
                _singleton.SaveEditItem(EditItemDelete.Create(deletedStr.ToString(), startingCursor));
                _singleton.SaveEditItem(EditItemInsertString.Create(insStr, startingCursor));
                _singleton.EndEditGroup();
            }

            if (nextKey.EqualsNormalized(Keys.Enter))
            {
                ViAcceptLine(nextKey);
            }
        }