private void WriteValueWithPadding(
            IColoredTextWriter textWriter,
            MessageTemplate.ArgumentToken arg,
            string formatedValue,
            Color?textForegroud,
            Color?textBackground,
            Color?valueForeground,
            Color?valueBackground)
        {
            if (arg.Alignment > 0)
            {
                int padLeft = arg.Alignment - formatedValue.Length;
                textWriter.Write(textForegroud, textBackground, ' ', padLeft);
            }

            textWriter.Write(
                valueForeground ?? textForegroud,
                valueBackground ?? textBackground,
                formatedValue);

            if (arg.Alignment < 0)
            {
                int padRight = -arg.Alignment - formatedValue.Length;
                textWriter.Write(textForegroud, textBackground, ' ', padRight);
            }
        }
        private void RenderLine()
        {
            int    written    = 0;
            string line       = ToString();
            var    highlights = _textHighlighter.Highlight(line);

            int lastUnchanged = IndexOfLastUnchanged(_prevHighlights, highlights);

            if (lastUnchanged >= 0)
            {
                written = highlights[lastUnchanged].Span.End;
            }

            _consoleCursor.Move(written - Position);

            int initialLeft = _console.CursorLeft - written;

            if (initialLeft < 0)
            {
                initialLeft = _console.BufferWidth + initialLeft % _console.BufferWidth;
            }

            for (int i = lastUnchanged + 1; i < highlights.Count; i++)
            {
                var highlight = highlights[i];
                var style     = highlight.Style;
                var span      = highlight.Span;

                _coloredTextWriter.Write(style?.Foreground, style?.Background, span.Text);
                written += span.Length;
            }

            if (written != line.Length)
            {
                throw new IndexOutOfRangeException(); // TODO: add error message
            }

            int clearSpace = WriteBlank(_prevLength - written);

            int currentLeft = (initialLeft + written + clearSpace) % _console.BufferWidth;

            if (currentLeft == 0 && _console.CursorLeft == _console.BufferWidth - 1)
            {
                _console.Write(' ');
                _console.Write('\b');
            }

            _consoleCursor.Move(Position - written - clearSpace);

            _prevLength     = written;
            _prevHighlights = highlights;
            if (written > _maxPosition)
            {
                _maxPosition = written;
            }
        }
 public static void Write(this IColoredTextWriter textWriter, Color?foreground, Color?background, char value, int count)
 {
     if (count <= 0)
     {
         return;
     }
     textWriter.SetColors(foreground, background);
     textWriter.Write(value, count);
     textWriter.ResetColors();
 }
        public void Write(
            Colored <MessageTemplate> messageTemplate,
            IReadOnlyList <Colored <object> > args,
            IColoredTextWriter textWriter,
            IFormatProvider formatProvider = null)
        {
            if (messageTemplate == null)
            {
                throw new ArgumentNullException(nameof(messageTemplate));
            }
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            if (textWriter == null)
            {
                throw new ArgumentNullException(nameof(textWriter));
            }

            var formatter = GetFormatter(formatProvider);

            var textForegroud  = messageTemplate.Foreground;
            var textBackground = messageTemplate.Background;

            foreach (var token in messageTemplate.Value.Tokens)
            {
                switch (token)
                {
                case MessageTemplate.TextToken text:
                    textWriter.Write(textForegroud, textBackground, text.Text);
                    break;

                case MessageTemplate.ArgumentToken arg:
                    var    coloredValue  = args[arg.Index];
                    string formatedValue = _valueFormatter.Format(coloredValue.Value, arg.Format, formatProvider, formatter);
                    WriteValueWithPadding(
                        textWriter, arg, formatedValue,
                        textForegroud, textBackground,
                        coloredValue.Foreground, coloredValue.Background);
                    break;

                default:
                    throw new FormatException($"Unknown format token `{token?.GetType().Name}`.");
                }
            }
        }
 public static void Write(this IColoredTextWriter textWriter, Color?foreground, Color?background, char value)
 {
     textWriter.SetColors(foreground, background);
     textWriter.Write(value);
     textWriter.ResetColors();
 }
Beispiel #6
0
 public void Write(IColoredTextWriter writer)
 {
     writer.Write(_options.Foreground, _options.Background, _options.Message);
 }
 public void Write(Color foreground, char value)
 {
     _lock.ThrowIfLocked();
     _coloredTextWriter.Write(foreground, null, value);
 }