Exemplo n.º 1
0
        /// <summary>
        /// Creates a new sub-context for writing, using a new scrawler.
        /// </summary>
        /// <param name="scrawler">The new scrawler to use for writing.</param>
        /// <param name="parentContext">The parent context, to birth this context from.</param>
        public SubWriteContext(Scrawler scrawler, ObjectWriteContext parentContext)
            : base(scrawler, parentContext.Stringifier)
        {
            ParentContext = parentContext;

            NullSymbol     = parentContext.NullSymbol;
            LineTerminator = parentContext.LineTerminator;
        }
Exemplo n.º 2
0
 public void Write(ObjectWriteContext context)
 {
     foreach (var item in Items)
     {
         context.Write("- ");
         context.Write(item);
         context.NewLine();
     }
 }
Exemplo n.º 3
0
        public void Write(ObjectWriteContext context)
        {
            var properties          = ModelType.GetProperties();
            var longestPropertyName = properties.Max(p => p.Name.Length);

            foreach (var property in properties)
            {
                context.Write($"{property.Name.PadRight(longestPropertyName)} : ");
                context.WriteObject(property.GetValue(Model));
                context.NewLine();
            }
        }
Exemplo n.º 4
0
        public void WriteObjectWithNullRef_WritesNullSymbol()
        {
            var scrawlerMock = new Mock <Scrawler>();
            var stringifier  = new NullStringifier();

            var context = new ObjectWriteContext(scrawlerMock.Object, stringifier);

            scrawlerMock.Setup(s => s.Write(context.NullSymbol))
            .Verifiable();

            context.WriteObject(null);

            scrawlerMock.Verify();
        }
Exemplo n.º 5
0
        public void WriteVerticalBorder(ObjectWriteContext context, int paddingLeft, int paddingRight)
        {
            if (paddingLeft > 0)
            {
                context.Write(new string(' ', paddingLeft));
            }

            if (Bordered)
            {
                context.Write("|");
            }

            if (paddingRight > 0)
            {
                context.Write(new string(' ', paddingRight));
            }
        }
Exemplo n.º 6
0
        private void WriteTopCap(ObjectWriteContext context, int longestLineLength)
        {
            for (var i = 0; i < Margin.Top; i++)
            {
                context.NewLine();
            }

            WriteHorizontalBorder(context, longestLineLength);

            for (var i = 0; i < Padding.Top; i++)
            {
                context.NewLine();
                WriteVerticalBorder(context, Margin.Left, Padding.Left);
                context.Write(new string(' ', longestLineLength));
                WriteVerticalBorder(context, Padding.Right, Margin.Right);
            }
        }
Exemplo n.º 7
0
        private void WriteBottomCap(ObjectWriteContext context, int contentLength)
        {
            for (var i = 0; i < Padding.Bottom; i++)
            {
                WriteVerticalBorder(context, Margin.Left, Padding.Left);
                context.Write(new string(' ', contentLength));
                WriteVerticalBorder(context, Padding.Right, Margin.Right);

                context.NewLine();
            }

            WriteHorizontalBorder(context, contentLength);

            for (var i = 0; i < Margin.Bottom; i++)
            {
                context.NewLine();
            }
        }
Exemplo n.º 8
0
        private void WriteHorizontalBorder(ObjectWriteContext context, int contentLength)
        {
            if (Bordered)
            {
                if (Margin.Left > 0)
                {
                    context.Write(new string(' ', Margin.Left));
                }

                context.Write("+");
                context.Write(new string('-', contentLength + Padding.Left + Padding.Right));
                context.Write("+");

                if (Margin.Right > 0)
                {
                    context.Write(new string(' ', Margin.Right));
                }
            }
        }
Exemplo n.º 9
0
        public void WriteObject_WritesToScrawler()
        {
            var scrawlerMock = new Mock <Scrawler>();
            var stringifier  = new NullStringifier();

            var context = new ObjectWriteContext(scrawlerMock.Object, stringifier);

            // We could check s.Write(string.Empty),
            // but that's not the point of this test.
            scrawlerMock.Setup(s => s.Write(It.IsAny <string>()))
            .Verifiable();

            var tester = new
            {
                Name       = "Earl",
                Occupation = "Tester",
                Age        = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).Days / 365,
                Assignment = "Getting written"
            };

            context.WriteObject(tester);

            scrawlerMock.Verify();
        }
        /// <summary>
        /// Writes a definition list from the provided model to the context.
        /// </summary>
        /// <typeparam name="TModel">The type of the model.</typeparam>
        /// <param name="writeContext">The context to write the list to.</param>
        /// <param name="model">The model to create a list from.</param>
        public static void WriteDefinitionList(this ObjectWriteContext writeContext, object model)
        {
            var definitionList = new DefinitionList(model);

            definitionList.Write(writeContext);
        }
Exemplo n.º 11
0
 /// <summary>
 /// Creates a new context with the same properties as the current context, but with the provided sub-scrawler.
 /// </summary>
 /// <param name="context">The context to create a sub context from.</param>
 /// <param name="subscrawler">The scrawler to use in the sub context.</param>
 public static SubWriteContext CreateSubContext(this ObjectWriteContext context, Scrawler subscrawler)
 => new SubWriteContext(subscrawler, context);
Exemplo n.º 12
0
        public void Write(ObjectWriteContext context)
        {
            var buildScrawler  = new ProxyScrawler();
            var proxiedContext = context.CreateSubContext(buildScrawler);

            var isTerminated     = false;
            var isLineTerminator = false;

            var terminatorChars = proxiedContext.LineTerminator.ToCharArray();

            var    lineList    = new LineList(32);
            string currentLine = string.Empty;

            // Setup on write method for the proxy.
            buildScrawler.OnWrite(s =>
            {
                var len = s.Length;

                if (len == 0)
                {
                    return;
                }

                isLineTerminator = s.Equals(proxiedContext.LineTerminator, StringComparison.Ordinal);

                if (isLineTerminator)
                {
                    lineList.AddLine(currentLine);
                    currentLine = string.Empty;

                    return;
                }

                isTerminated = isLineTerminator || s.EndsWith(proxiedContext.LineTerminator);

                if (isTerminated)
                {
                    lineList.AddLine(currentLine.TrimEnd(terminatorChars));
                    currentLine = string.Empty;

                    return;
                }

                currentLine += s;
            });

            // Write to the proxied context.
            InnerComponent.Write(proxiedContext);

            // Make all lines into a "block".
            // | abc abc abc |
            // | abc abc     |
            // | abc ab abc  |
            lineList.RightPad(lineList.LongestLineLength);

            // Write top border
            WriteTopCap(context, lineList.LongestLineLength);

            context.NewLine();

            foreach (var line in lineList)
            {
                WriteVerticalBorder(context, Margin.Left, Padding.Left);

                context.Write(line);

                WriteVerticalBorder(context, Padding.Right, Margin.Right);

                context.NewLine();
            }

            WriteBottomCap(context, lineList.LongestLineLength);

            context.NewLine();
        }