コード例 #1
0
        public virtual int Write(string value)
        {
            var writtenLines = (CursorLeft + EastAsianWidth.GetWidth(value) + 1) / Console.BufferWidth;

            Console.Write(value);

            return(writtenLines);
        }
コード例 #2
0
            public void WhenIaAsian()
            {
                var eastAsianWidth = new EastAsianWidth(new TestProvider());

                string value = new string(new [] { (char)0, (char)3 });

                Assert.Equal(3, eastAsianWidth.GetWidth(value, true));
            }
コード例 #3
0
            public void WhenIsNotAsian()
            {
                var eastAsianWidth = new EastAsianWidth(new TestProvider());

                string value = new string(new[] { (char)0, (char)3 });

                Assert.Equal(2, eastAsianWidth.GetWidth(value, CultureInfo.GetCultureInfo("az-Cyrl-AZ")));
            }
コード例 #4
0
            public void WhenIaAsian()
            {
#if netcoreapp31
                CultureInfo.CurrentUICulture = CultureInfo.GetCultureInfo("ja-JP");
#endif
                var eastAsianWidth = new EastAsianWidth(new TestProvider());

                string value = new string(new[] { (char)0, (char)3 });
                Assert.Equal(3, eastAsianWidth.GetWidth(value));
            }
コード例 #5
0
        protected override void InputTemplate(ScreenBuffer screenBuffer)
        {
            screenBuffer.WritePrompt(_message);

            if (_defaultValue != null)
            {
                screenBuffer.Write($"({_defaultValue}) ");
            }

            var(left, top) = screenBuffer.GetCursorPosition();

            var input = _inputBuffer.ToString();

            screenBuffer.Write(input);

            var width = EastAsianWidth.GetWidth(input.Take(_startIndex)) + left;

            screenBuffer.SetCursorPosition(width % screenBuffer.BufferWidth, top + (width / screenBuffer.BufferWidth));
        }
コード例 #6
0
        public virtual string ReadLine()
        {
            int startIndex  = 0;
            var inputBuffer = new StringBuilder();

            while (true)
            {
                var keyInfo = ReadKey();

                if (keyInfo.Key == ConsoleKey.Enter)
                {
                    break;
                }

                if (keyInfo.Key == ConsoleKey.LeftArrow)
                {
                    if (startIndex > 0)
                    {
                        startIndex -= 1;

                        var width = EastAsianWidth.GetWidth(inputBuffer[startIndex]);

                        if (Console.CursorLeft - width < 0)
                        {
                            Console.CursorTop -= 1;
                            Console.CursorLeft = Console.BufferWidth - width;
                        }
                        else
                        {
                            Console.CursorLeft -= width;
                        }
                    }
                    else
                    {
                        Beep();
                    }
                }
                else if (keyInfo.Key == ConsoleKey.RightArrow)
                {
                    if (startIndex < inputBuffer.Length)
                    {
                        var width = EastAsianWidth.GetWidth(inputBuffer[startIndex]);

                        if (Console.CursorLeft + width >= Console.BufferWidth)
                        {
                            Console.CursorTop += 1;
                            Console.CursorLeft = 0;
                        }
                        else
                        {
                            Console.CursorLeft += width;
                        }

                        startIndex += 1;
                    }
                    else
                    {
                        Beep();
                    }
                }
                else if (keyInfo.Key == ConsoleKey.Backspace)
                {
                    if (startIndex > 0)
                    {
                        startIndex -= 1;

                        var width = EastAsianWidth.GetWidth(inputBuffer[startIndex]);

                        if (Console.CursorLeft - width < 0)
                        {
                            Console.CursorTop -= 1;
                            Console.CursorLeft = Console.BufferWidth - 1;
                        }
                        else
                        {
                            Console.CursorLeft -= width;
                        }

                        inputBuffer.Remove(startIndex, 1);

                        var(left, top) = GetCursorPosition();

                        for (int i = startIndex; i < inputBuffer.Length; i++)
                        {
                            Console.Write(inputBuffer[i]);
                        }

                        Console.Write(width == 1 ? " " : "  ");

                        SetCursorPosition(left, top);
                    }
                    else
                    {
                        Beep();
                    }
                }
                else if (keyInfo.Key == ConsoleKey.Delete)
                {
                    if (startIndex < inputBuffer.Length)
                    {
                        var width = EastAsianWidth.GetWidth(inputBuffer[startIndex]);

                        inputBuffer.Remove(startIndex, 1);

                        var(left, top) = GetCursorPosition();

                        for (int i = startIndex; i < inputBuffer.Length; i++)
                        {
                            Console.Write(inputBuffer[i]);
                        }

                        Console.Write(width == 1 ? " " : "  ");

                        SetCursorPosition(left, top);
                    }
                    else
                    {
                        Beep();
                    }
                }
                else if (!char.IsControl(keyInfo.KeyChar))
                {
                    inputBuffer.Insert(startIndex, keyInfo.KeyChar);

                    var(left, top) = GetCursorPosition();

                    for (int i = startIndex; i < inputBuffer.Length; i++)
                    {
                        Console.Write(inputBuffer[i]);
                    }

                    left += EastAsianWidth.GetWidth(keyInfo.KeyChar);

                    if (left >= Console.BufferWidth)
                    {
                        left = 0;
                        top += 1;

                        if (top >= Console.BufferHeight)
                        {
                            Console.BufferHeight += 1;
                        }
                    }

                    SetCursorPosition(left, top);

                    startIndex += 1;
                }
            }

            return(inputBuffer.ToString());
        }