Beispiel #1
0
        private void WriteOrStash(string s, bool indent = true)
        {
            if (indent)
            {
                var rs = new CuttingStringReader(s);
                var sb = new StringBuilder();

                while (rs.RemainingLength > 0)
                {
                    // Detect line breaks
                    if (rs.Eat("\r\n") || rs.Eat("\n"))
                    {
                        sb.Append("\n");
                        _isAtLineStart = true;
                        continue;
                    }

                    // Indentation
                    if (_isAtLineStart)
                    {
                        for (int j = 0; j < _Indent; j++)
                        {
                            sb.Append('\t');
                        }
                        _isAtLineStart = false;
                    }

                    var c = rs.Read();
                    sb.Append(c);
                }
                s = sb.ToString();
            }

            if (_Stash != null)
            {
                _Stash.Append(s);
            }
            else
            {
                var task = new WriteTask(s);
                _WriteQueue.Add(task);
                if (!AllowAsynchronousWriting)
                {
                    task.Done.WaitOne();
                }
            }
        }
Beispiel #2
0
        private static void DoWrite(string s, bool flush)
        {
            var targets = _Targets;             // _Targets may be replaced (copy-on-write), so grab a single reference to be used throughout this method
            var rs      = new CuttingStringReader(s);
            var sb      = new StringBuilder();

            while (rs.RemainingLength > 0)
            {
                // Case "\~ "
                if (rs.Eat("\\~ "))
                {
                    foreach (var t in targets)
                    {
                        t.Write(sb.ToString());
                        t.ResetColors();
                    }
                    sb.Clear();

                    continue;
                }

                // Case "\FX"
                if (rs.Eat("\\F"))
                {
                    var c = Int32.Parse(new string (rs.Read(), 1), NumberStyles.HexNumber);

                    foreach (var t in targets)
                    {
                        t.Write(sb.ToString());
                        t.SetForegroundColor((ConsoleColor)c);
                    }
                    sb.Clear();

                    continue;
                }

                // Case "\BX"
                if (rs.Eat("\\B"))
                {
                    var c = Int32.Parse(new string (rs.Read(), 1), NumberStyles.HexNumber);

                    foreach (var t in targets)
                    {
                        t.Write(sb.ToString());
                        t.SetBackgroundColor((ConsoleColor)c);
                    }
                    sb.Clear();

                    continue;
                }

                // Regular
                {
                    var c = rs.Read();
                    sb.Append(c);
                }
            }

            foreach (var t in targets)
            {
                t.Write(sb.ToString());
            }
            sb.Clear();

            if (flush)
            {
                foreach (var t in targets)
                {
                    t.Flush();
                }
            }
        }