Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="StyledText"/> class.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="sourceStyle">The StyledText to copy the style from.</param>
        public StyledText(string text, StyledText sourceStyle)
        {
            text = text.Replace("\r\n", "\n");
            text = text.Replace("\r", "\n");

            _text = text;
            _color = sourceStyle.Color;
        }
Exemplo n.º 2
0
        public void ConcastTestA()
        {
            var s1 = new StyledText("abcd", Color.Black);
            var s2 = new StyledText("123", Color.Black);
            var s3 = new StyledText("xyz", Color.Black);
            var concat = StyledText.Concat(new StyledText[] { s1, s2, s3 });

            Assert.AreEqual(1, concat.Count());
            Assert.AreEqual(s1.Text + s2.Text + s3.Text, concat.First().Text);
            Assert.IsTrue(s1.HasSameStyle(concat.First()));
        }
Exemplo n.º 3
0
        public void ConstructorCopyStyleTest()
        {
            const string originalString = "asdf werljk xov  .qw 120 xcv;z";
            var s = new StyledText(originalString, Color.Black);
            var s2 = new StyledText("ffjfjfj", s);

            Assert.AreEqual(originalString, s.Text);
            Assert.AreEqual("ffjfjfj", s2.Text);

            Assert.AreEqual(Color.Black, s.Color);
            Assert.AreEqual(Color.Black, s2.Color);
        }
Exemplo n.º 4
0
        public void ConcastTestB()
        {
            var s1 = new StyledText("abcd", Color.Black);
            var s2 = new StyledText("123", Color.Black);
            var s3 = new StyledText("xyz", Color.White);
            var concat = StyledText.Concat(new StyledText[] { s1, s2, s3 }).ToArray();

            Assert.AreEqual(2, concat.Count());
            Assert.AreEqual(s1.Text + s2.Text, concat[0].Text);
            Assert.AreEqual(s3.Text, concat[1].Text);
            Assert.IsTrue(s1.HasSameStyle(concat[0]));
            Assert.IsTrue(s3.HasSameStyle(concat[1]));
        }
Exemplo n.º 5
0
        public void SubstringWithLengthTest()
        {
            const string originalString = "asdf werljk xov  .qw 120 xcv;z";
            var s = new StyledText(originalString, Color.Black);
            var s2 = s.Substring(5, 4);

            Assert.AreEqual(s.Text, originalString);
            Assert.AreEqual(Color.Black, s.Color);

            Assert.AreEqual(originalString.Substring(5, 4), s2.Text);
            Assert.AreEqual(Color.Black, s2.Color);
        }
Exemplo n.º 6
0
        public void ToStringTest()
        {
            var a = new StyledText("abc", Color.Red);
            var b = new StyledText("123", a);

            Assert.AreEqual("abc", a.ToString());
            Assert.AreEqual("123", b.ToString());
        }
Exemplo n.º 7
0
        public void IEnumerableToStringTest()
        {
            var a = new StyledText("abc");
            var b = new StyledText("123");
            var c = new StyledText("xyz");
            var v = new StyledText[] { a, b, c };

            Assert.AreEqual("abc123xyz", StyledText.ToString(v));
        }
Exemplo n.º 8
0
        public static List<StyledText> ToMultiline(StyledText text)
        {
            var ret = new List<StyledText>();

            var split = text.Split('\n');
            foreach (var s in split)
            {
                var newS = new StyledText(s.Text.Replace('\r'.ToString(), string.Empty), s);
                ret.Add(newS);
            }

            return ret;
        }
Exemplo n.º 9
0
        /// <summary>
        /// Splits the <see cref="StyledText"/> at the given index into two parts. The character at the given
        /// <paramref name="charIndex"/> will end up in the <paramref name="right"/> side.
        /// </summary>
        /// <param name="charIndex">The 0-based character index to split at.</param>
        /// <param name="left">The left side of the split.</param>
        /// <param name="right">The right side of the split.</param>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="charIndex"/> is less than 0 or greater
        /// than the length of the <see cref="Text"/>.</exception>
        public void SplitAt(int charIndex, out StyledText left, out StyledText right)
        {
            if (charIndex < 0 || charIndex > Text.Length)
                throw new ArgumentOutOfRangeException("charIndex");

            if (charIndex == 0)
            {
                left = new StyledText(string.Empty, this);
                right = this;
            }
            else if (charIndex == Text.Length)
            {
                left = this;
                right = new StyledText(string.Empty, this);
            }
            else
            {
                left = Substring(0, charIndex);
                right = Substring(charIndex);
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Splits the <see cref="StyledText"/> into multiple pieces using the given <paramref name="separator"/>.
        /// </summary>
        /// <param name="separator">An array of Unicode characters that delimit the substrings in this instance,
        /// an empty array that contains no delimiters, or null.</param>
        /// <param name="count">The maximum number of substrings to return.</param>
        /// <returns>An array containing the pieces of the <see cref="StyledText"/> split using the given
        /// <paramref name="separator"/>.</returns>
        public StyledText[] Split(char[] separator, int count)
        {
            var strings = Text.Split(separator, count);

            var ret = new StyledText[strings.Length];
            for (var i = 0; i < ret.Length; i++)
            {
                if (strings[i] == Text)
                    ret[i] = this;
                else
                    ret[i] = new StyledText(strings[i], Color);
            }

            return ret;
        }
Exemplo n.º 11
0
        public static List<StyledText> Concat(StyledText[] input)
        {
            var ret = new List<StyledText>(input.Length);

            for (var i = 0; i < input.Length; i++)
            {
                var current = input[i];
                while (i + 1 < input.Length && current.HasSameStyle(input[i + 1]))
                {
                    current += input[i + 1].Text;
                    i++;
                }
                ret.Add(current);
            }

            return ret;
        }
Exemplo n.º 12
0
 /// <summary>
 /// Inserts the <paramref name="text"/> into the <see cref="TextBox"/> at the current cursor position.
 /// </summary>
 /// <param name="text">The text to insert.</param>
 public void Insert(StyledText text)
 {
     _lines.CurrentLine.Insert(text, CursorLinePosition);
     ((IEditableText)this).MoveCursor(MoveCursorDirection.Right);
     _numCharsToDraw.Invalidate();
     TruncateIfNeeded();
 }
Exemplo n.º 13
0
        /// <summary>
        /// Appends the <paramref name="text"/> to the <see cref="TextBox"/> at the end.
        /// </summary>
        /// <param name="text">The text to append.</param>
        public void Append(StyledText text)
        {
            if (text.Text.Length == 0)
                return;

            if (!IsMultiLine)
                _lines.LastLine.Append(text.ToSingleline());
            else
            {
                var textLines = StyledText.ToMultiline(text);
                _lines.LastLine.Append(textLines[0]);

                for (var i = 1; i < textLines.Count; i++)
                {
                    var newLine = new TextBoxLine(_lines);
                    _lines.Insert(_lines.Count, newLine);
                    newLine.Append(textLines[i]);
                }

                TruncateIfNeeded();
            }

            _numCharsToDraw.Invalidate();

            _hasTextChanged = true;
        }
Exemplo n.º 14
0
        public void SplitStringsWithOptionsTest()
        {
            const string originalString = "asdf werljk xov  .qw 120 xcv;z";
            var s = new StyledText(originalString, Color.Black);
            var split = s.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
            var expected = originalString.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);

            for (var i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(s.Color, split[i].Color);
                Assert.AreEqual(expected[i], split[i].Text);
            }
        }
Exemplo n.º 15
0
        public void SplitCharsWithCountTest()
        {
            const string originalString = "asdf werljk xov  .qw 120 xcv;z";
            var s = new StyledText(originalString, Color.Black);
            var split = s.Split(new char[] { ' ' }, 2);
            var expected = originalString.Split(new char[] { ' ' }, 2);

            for (var i = 0; i < expected.Length; i++)
            {
                Assert.AreEqual(s.Color, split[i].Color);
                Assert.AreEqual(expected[i], split[i].Text);
            }
        }
Exemplo n.º 16
0
        public void SplitAtTest()
        {
            var s = new StyledText("abcd", Color.Black);
            StyledText l;
            StyledText r;

            s.SplitAt(0, out l, out r);
            Assert.AreEqual("", l.Text);
            Assert.AreEqual("abcd", r.Text);
            Assert.AreEqual(Color.Black, l.Color);
            Assert.AreEqual(Color.Black, r.Color);

            s.SplitAt(1, out l, out r);
            Assert.AreEqual("a", l.Text);
            Assert.AreEqual("bcd", r.Text);
            Assert.AreEqual(Color.Black, l.Color);
            Assert.AreEqual(Color.Black, r.Color);

            s.SplitAt(2, out l, out r);
            Assert.AreEqual("ab", l.Text);
            Assert.AreEqual("cd", r.Text);
            Assert.AreEqual(Color.Black, l.Color);
            Assert.AreEqual(Color.Black, r.Color);

            s.SplitAt(3, out l, out r);
            Assert.AreEqual("abc", l.Text);
            Assert.AreEqual("d", r.Text);
            Assert.AreEqual(Color.Black, l.Color);
            Assert.AreEqual(Color.Black, r.Color);

            s.SplitAt(4, out l, out r);
            Assert.AreEqual("abcd", l.Text);
            Assert.AreEqual("", r.Text);
            Assert.AreEqual(Color.Black, l.Color);
            Assert.AreEqual(Color.Black, r.Color);

            Assert.Throws<ArgumentOutOfRangeException>(() => s.SplitAt(-1, out l, out r));
            Assert.Throws<ArgumentOutOfRangeException>(() => s.SplitAt(5, out l, out r));
        }
Exemplo n.º 17
0
        public void OperatorAddTest2()
        {
            var s = new StyledText("asdf", Color.Black);
            var s2 = s + "ff";

            Assert.AreEqual("asdf", s.Text);
            Assert.AreEqual(Color.Black, s.Color);

            Assert.AreEqual("asdfff", s2.Text);
            Assert.AreEqual(Color.Black, s2.Color);
        }
Exemplo n.º 18
0
        public void IEnumerableToStringWithDelimiterTest()
        {
            var a = new StyledText("abc");
            var b = new StyledText("123");
            var c = new StyledText("xyz");
            var v = new StyledText[] { a, b, c };

            Assert.AreEqual("abcWWW123WWWxyz", StyledText.ToString(v, "WWW"));
        }
Exemplo n.º 19
0
        /// <summary>
        /// Appends the <paramref name="text"/> to the <see cref="TextBox"/> at the end, then inserts a line break.
        /// </summary>
        /// <param name="text">The text to append.</param>
        public void AppendLine(StyledText text)
        {
            Append(text);

            if (IsMultiLine)
            {
                _lines.Insert(_lines.Count, new TextBoxLine(_lines));
                TruncateIfNeeded();
            }
        }
Exemplo n.º 20
0
        public void ToMultiLineTest()
        {
            // Test removed since Font.DefaultFont in the testing framework seems to cause issues
            return;

#pragma warning disable 162
            var s1 = new StyledText("abcd", Color.Black);
            var s2 = new StyledText("\r123", Color.Black);
            var s3 = new StyledText("\nxyz", Color.Black);
            var s4 = new StyledText("\r\nqwe", Color.Black);

            var lines = new List<StyledText> { s1, s2, s3, s4 };

            var r = StyledText.ToMultiline(lines, false, TestHelper.DefaultFont, 400);

            Assert.AreEqual("abcd", r[0][0].Text);
            Assert.AreEqual("123", r[1][0].Text);
            Assert.AreEqual("xyz", r[2][0].Text);
            Assert.AreEqual("qwe", r[3][0].Text);
#pragma warning restore 162
        }
Exemplo n.º 21
0
 static IEnumerable<StyledText> CreateChatText(string name, string message)
 {
     var left = new StyledText(name + ": ", Color.Red);
     var right = new StyledText(message, Color.Black);
     return new List<StyledText> { left, right };
 }
Exemplo n.º 22
0
        public void ToMultilineMultiInputDifferentLineTest()
        {
            const string originalString1 = "one \ntwo";
            const string originalString2 = "three fou\nr";
            const string originalString3 = "fi\r\nve";
            var s1 = new StyledText(originalString1, Color.Black);
            var s2 = new StyledText(originalString2, Color.Black);
            var s3 = new StyledText(originalString3, Color.Black);
            var lines = StyledText.ToMultiline(new StyledText[] { s1, s2, s3 }, true);

            Assert.AreEqual(6, lines.Count);
            Assert.AreEqual("one ", lines[0][0].Text);
            Assert.AreEqual("two", lines[1][0].Text);
            Assert.AreEqual("three fou", lines[2][0].Text);
            Assert.AreEqual("r", lines[3][0].Text);
            Assert.AreEqual("fi", lines[4][0].Text);
            Assert.AreEqual("ve", lines[5][0].Text);

            foreach (var l in lines)
            {
                Assert.AreEqual(s1.Color, l[0].Color);
                Assert.AreEqual(s2.Color, l[0].Color);
            }
        }
Exemplo n.º 23
0
 /// <summary>
 /// Gets if this <see cref="StyledText"/> has the same style as another <see cref="StyledText"/>.
 /// </summary>
 /// <param name="other">The other <see cref="StyledText"/>.</param>
 /// <returns>True if this StyledText has same style as the other <see cref="StyledText"/>; otherwise false.</returns>
 public bool HasSameStyle(StyledText other)
 {
     return Color == other.Color;
 }
Exemplo n.º 24
0
        public void ToMultilineMultiInputSameLineTest()
        {
            const string originalString1 = "one \ntwo";
            const string originalString2 = " three fou\nr";
            var s1 = new StyledText(originalString1, Color.Black);
            var s2 = new StyledText(originalString2, Color.Black);
            var lines = StyledText.ToMultiline(new StyledText[] { s1, s2 }, false);

            Assert.AreEqual(3, lines.Count);

            Assert.AreEqual("one ", lines[0][0].Text);

            Assert.AreEqual("two", lines[1][0].Text);
            Assert.AreEqual(" three fou", lines[1][1].Text);

            Assert.AreEqual("r", lines[2][0].Text);

            foreach (var l in lines)
            {
                Assert.AreEqual(s1.Color, l[0].Color);
                Assert.AreEqual(s2.Color, l[0].Color);
            }
        }
Exemplo n.º 25
0
        /// <summary>
        /// Splits the <see cref="StyledText"/> into multiple pieces using the given <paramref name="separator"/>.
        /// </summary>
        /// <param name="separator">An array of strings that delimit the substrings in this string,
        /// an empty array that contains no delimiters, or null.</param>
        /// <param name="options">Specify <see cref="System.StringSplitOptions.RemoveEmptyEntries"/> to omit empty array
        /// elements from the array returned, or <see cref="System.StringSplitOptions.None"/> to include
        /// empty array elements in the array returned.</param>
        /// <returns>An array containing the pieces of the StyledText split using the given
        /// <paramref name="separator"/>.</returns>
        public StyledText[] Split(string[] separator, StringSplitOptions options)
        {
            var strings = Text.Split(separator, options);

            var ret = new StyledText[strings.Length];
            for (var i = 0; i < ret.Length; i++)
            {
                if (strings[i] == Text)
                    ret[i] = this;
                else
                    ret[i] = new StyledText(strings[i], Color);
            }

            return ret;
        }
Exemplo n.º 26
0
        public void ToMultilineOneInputTest()
        {
            const string originalString = "one two\n three fou\nr";
            var s = new StyledText(originalString, Color.Black);
            var lines = StyledText.ToMultiline(s);

            Assert.AreEqual(3, lines.Count);
            Assert.AreEqual("one two", lines[0].Text);
            Assert.AreEqual(" three fou", lines[1].Text);
            Assert.AreEqual("r", lines[2].Text);

            foreach (var l in lines)
            {
                Assert.AreEqual(s.Color, l.Color);
            }
        }
Exemplo n.º 27
0
 public void ToSingleLineTest()
 {
     var a = new StyledText("abc\rdefg\r\nhij\r\nklm\nn");
     Assert.AreEqual("abcdefghijklmn", a.ToSingleline().Text);
 }
Exemplo n.º 28
0
 public void ToSingleLineWithReplacementTest()
 {
     var a = new StyledText("abc\rdefg\r\nhij\r\nklm\nn");
     Assert.AreEqual("abcXdefgXhijXklmXn", a.ToSingleline("X").Text);
 }
Exemplo n.º 29
0
        /// <summary>
        /// Handles when the <see cref="ScreenManager"/> updates.
        /// </summary>
        /// <param name="screenManager">The screen manager.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        void ScreenManager_Updated(IScreenManager screenManager, EventArgs e)
        {
            // The logger we use to grab log messages and output to the console will continue to queue messages
            // indefinitely until it is cleared. Because of this, we can't just flush the log by overriding
            // the screen's Update method. Instead, we have this event hook for when the ScreenManager
            // updates so we can clear out the log buffer every tick.

            // Get the latest events
            LoggingEvent[] events;
            try
            {
                events = _logger.GetEvents();
            }
            catch (ArgumentException)
            {
                // There is some bug in the _logger.GetEvents() that can throw this exception...
                return;
            }

            _logger.Clear();

            // Ensure there are events
            if (events != null && events.Length > 0)
            {
                foreach (var ev in events)
                {
                    var styledText = new StyledText(ev.RenderedMessage, ev.Level.GetColor());
                    _txtOutput.AppendLine(styledText);
                }
            }

            // Move to the last line in the log textbox
            _txtOutput.LineBufferOffset = Math.Max(0, _txtOutput.LineCount - _txtOutput.MaxVisibleLines);
        }
Exemplo n.º 30
0
        public void HasSameStyleTrueTest()
        {
            var a = new StyledText("abc", Color.Red);
            var b = new StyledText("123", a);

            Assert.IsTrue(a.HasSameStyle(b));
            Assert.IsTrue(b.HasSameStyle(a));
        }