コード例 #1
0
        internal static string FormatTable(Table table, Hints hints = null)
        {
            hints = hints ?? new Hints();

            if (!table.Columns.Any())
            {
                return("");
            }

            if (hints.MaxTableWidth.HasValue)
            {
                EnforceMaxWidth(table, hints.MaxTableWidth.Value);
            }

            const char horizontalLineChar = '-';
            const char verticalLineChar   = '|';
            const char cornerChar         = '+';

            var skipHorizontalLines = hints.CollapseVerticallyWhenSingleLine &&
                                      !table.HasCellWith(c => c.Lines.Length > 1);

            var builder = new StringBuilder();

            BuildHorizontalLine(table, builder, horizontalLineChar, cornerChar);

            BuildColumnLabels(table, builder, verticalLineChar);

            if (skipHorizontalLines)
            {
                BuildHorizontalLine(table, builder, horizontalLineChar, cornerChar);
            }

            if (table.Rows.Any())
            {
                foreach (var row in table.Rows)
                {
                    if (!skipHorizontalLines)
                    {
                        BuildHorizontalLine(table, builder, horizontalLineChar, cornerChar);
                    }

                    BuildTableRow(row, table, builder, verticalLineChar);
                }
            }

            BuildHorizontalLine(table, builder, horizontalLineChar, cornerChar);

            return(builder.ToString());
        }
コード例 #2
0
 /// <summary>
 /// Creates the table formatter possibly using the given <see cref="Hints"/> to do its thing
 /// </summary>
 public TableFormatter(Hints hints = null)
 {
     _hints = hints ?? new Hints();
 }