예제 #1
0
 /// <summary>
 /// Add title row on top of table
 /// </summary>
 /// <param name="builder"></param>
 /// <param name="title"></param>
 /// <param name="foregroundColor">text color</param>
 /// <param name="backgroundColor">background color</param>
 /// <returns></returns>
 public static ConsoleTableBuilder WithTitle(this ConsoleTableBuilder builder, string title, ConsoleColor foregroundColor, ConsoleColor backgroundColor, TextAligntment titleAligntment = TextAligntment.Center)
 {
     builder.TableTitle              = title;
     builder.TableTitleColor         = new ConsoleColorNullable(foregroundColor, backgroundColor);
     builder.TableTitleTextAlignment = titleAligntment;
     return(builder);
 }
예제 #2
0
        private static string[] GetVars(ConsoleTableBuilder builder, int maxRowLength)
        {
            string beginTable = null;
            string divider    = null;
            string endTable   = null;

            switch (builder.Options.FrameStyle)
            {
            case ConsoleTableBuilderOption.FrameStyles.None:
                beginTable = new string(builder.Options.DividerChar, maxRowLength);
                divider    = new string(builder.Options.DividerChar, maxRowLength);
                endTable   = new string(builder.Options.DividerChar, maxRowLength);
                break;

            case ConsoleTableBuilderOption.FrameStyles.Pipe:
                beginTable = FrameChars.BoxSE + new string(FrameChars.BoxHorizontal, maxRowLength - 2) + FrameChars.BoxSW;
                divider    = FrameChars.BoxVerticaltoRight + new string(FrameChars.BoxHorizontal, maxRowLength - 2) + FrameChars.BoxVerticaltoLeft;
                endTable   = FrameChars.BoxNE + new string(FrameChars.BoxHorizontal, maxRowLength - 2) + FrameChars.BoxNW;
                break;

            case ConsoleTableBuilderOption.FrameStyles.DoublePipe:
                beginTable = FrameChars.PipeSE + new string(FrameChars.PipeHorizontal, maxRowLength - 2) + FrameChars.PipeSW;
                divider    = FrameChars.PipeVerticaltoRight + new string(FrameChars.PipeHorizontal, maxRowLength - 2) + FrameChars.PipeVerticaltoLeft;
                endTable   = FrameChars.PipeNE + new string(FrameChars.PipeHorizontal, maxRowLength - 2) + FrameChars.PipeNW;
                break;
            }

            return(new string[] { beginTable, divider, endTable });
        }
        private static StringBuilder BuildMetaRowsFormat(ConsoleTableBuilder builder, StringBuilder stringBuilder, MetaRowPositions position)
        {
            switch (position)
            {
            case MetaRowPositions.Top:
                if (builder.TopMetadataRows.Any())
                {
                    foreach (var item in builder.TopMetadataRows)
                    {
                        stringBuilder.AppendLine(item.Value);
                    }
                }
                break;

            case MetaRowPositions.Bottom:
                if (builder.BottomMetadataRows.Any())
                {
                    foreach (var item in builder.BottomMetadataRows)
                    {
                        stringBuilder.AppendLine(item.Value);
                    }
                }
                break;

            default:
                break;
            }


            return(stringBuilder);
        }
        public static void ExportAndWriteLine(this ConsoleTableBuilder builder, TableAligntment alignment = TableAligntment.Left)
        {
            var strBuilder = builder.Export();

            if (alignment != TableAligntment.Left)
            {
                var lines = strBuilder.ToString().Split('\n');

                var linesCount = lines.Count();
                for (int i = 0; i < linesCount; i++)
                {
                    switch (alignment)
                    {
                    case TableAligntment.Center:
                        Console.WriteLine(String.Format("{0," + ((Console.WindowWidth / 2) + (lines[i].Length / 2)) + "}", lines[i]));
                        break;

                    case TableAligntment.Right:
                        Console.WriteLine(String.Format("{0," + Console.WindowWidth + "}", new string(' ', Console.WindowWidth - lines[i].Length) + lines[i]));
                        break;
                    }
                }
            }
            else
            {
                Console.WriteLine(strBuilder);
            }
        }
        public static ConsoleTableBuilder From(DataTable dt)
        {
            var builder = new ConsoleTableBuilder();

            if (dt == null)
            {
                return(builder);
            }

            var columnNames = dt.Columns.Cast <DataColumn>()
                              .Select(x => x.ColumnName)
                              .ToList();

#if NET35
            columnNames.ForEach(f => builder.Column.Add(f));
#else
            builder.Column = new List <object>(columnNames);
#endif
            foreach (DataRow row in dt.Rows)
            {
                builder.Rows.Add(new List <object>(row.ItemArray));
            }

            return(builder);
        }
예제 #6
0
        public static ConsoleTableBuilder WithMetadataRow(this ConsoleTableBuilder builder, MetaRowPositions position, Func <ConsoleTableBuilder, string> contentGenerator)
        {
            switch (position)
            {
            case MetaRowPositions.Top:
                if (builder.TopMetadataRows == null)
                {
                    builder.TopMetadataRows = new List <KeyValuePair <MetaRowPositions, Func <ConsoleTableBuilder, string> > >();
                }

                builder.TopMetadataRows.Add(new KeyValuePair <MetaRowPositions, Func <ConsoleTableBuilder, string> >(position, contentGenerator));
                break;

            case MetaRowPositions.Bottom:
                if (builder.BottomMetadataRows == null)
                {
                    builder.BottomMetadataRows = new List <KeyValuePair <MetaRowPositions, Func <ConsoleTableBuilder, string> > >();
                }

                builder.BottomMetadataRows.Add(new KeyValuePair <MetaRowPositions, Func <ConsoleTableBuilder, string> >(position, contentGenerator));
                break;

            default:
                break;
            }

            return(builder);
        }
예제 #7
0
        public static StringBuilder Export(this ConsoleTableBuilder builder)
        {
            var numberOfColumns = 0;

            if (builder.Rows.Any())
            {
                numberOfColumns = builder.Rows.Max(x => x.Count);
            }
            else
            {
                if (builder.Column != null)
                {
                    numberOfColumns = builder.Column.Count();
                }
            }

            if (numberOfColumns == 0)
            {
                return(new StringBuilder());
            }

            if (builder.Column == null)
            {
                numberOfColumns = 0;
            }
            else
            {
                if (numberOfColumns < builder.Column.Count)
                {
                    numberOfColumns = builder.Column.Count;
                }
            }

            for (int i = 0; i < 1; i++)
            {
                if (builder.Column != null && builder.Column.Count < numberOfColumns)
                {
                    var missCount = numberOfColumns - builder.Column.Count;
                    for (int j = 0; j < missCount; j++)
                    {
                        builder.Column.Add(null);
                    }
                }
            }

            for (int i = 0; i < builder.Rows.Count; i++)
            {
                if (builder.Rows[i].Count < numberOfColumns)
                {
                    var missCount = numberOfColumns - builder.Rows[i].Count;
                    for (int j = 0; j < missCount; j++)
                    {
                        builder.Rows[i].Add(null);
                    }
                }
            }

            return(CreateTableForCustomFormat(builder));
        }
예제 #8
0
        public static StringBuilder Export(this ConsoleTableBuilder builder)
        {
            if (!builder.Rows.Any())
            {
                throw new Exception("Table has no rows");
            }

            var numberOfColumns = builder.Rows.Max(x => x.Count);

            if (numberOfColumns < builder.Column.Count)
            {
                numberOfColumns = builder.Column.Count;
            }

            for (int i = 0; i < 1; i++)
            {
                if (builder.Column.Count < numberOfColumns)
                {
                    var missCount = numberOfColumns - builder.Column.Count;
                    for (int j = 0; j < missCount; j++)
                    {
                        builder.Column.Add(null);
                    }
                }
            }

            for (int i = 0; i < builder.Rows.Count; i++)
            {
                if (builder.Rows[i].Count < numberOfColumns)
                {
                    var missCount = numberOfColumns - builder.Rows[i].Count;
                    for (int j = 0; j < missCount; j++)
                    {
                        builder.Rows[i].Add(null);
                    }
                }
            }

            switch (builder.TableFormat)
            {
            case ConsoleTableBuilderFormat.Default:
                return(CreateTableForDefaultFormat(builder));

            case ConsoleTableBuilderFormat.Minimal:
                builder.Options.Delimiter  = '\0';
                builder.Options.FrameStyle = 0;
                return(CreateTableForMarkdownFormat(builder));

            case ConsoleTableBuilderFormat.Alternative:
                return(CreateTableForAlternativeFormat(builder));

            case ConsoleTableBuilderFormat.MarkDown:
                return(CreateTableForMarkdownFormat(builder));

            default:
                return(CreateTableForDefaultFormat(builder));
            }
        }
예제 #9
0
        private static StringBuilder CreateTableForDefaultFormat(ConsoleTableBuilder builder)
        {
            var strBuilder = new StringBuilder();

            if (builder.Options.MetaRowPosition == ConsoleTableBuilderOption.MetaRowPositions.Top)
            {
                strBuilder.AppendLine(BuildMetaRowFormat(builder));
            }

            // create the string format with padding
            char delim = GetDelimiter(builder);

            string format = builder.Format(delim);

            if (format == string.Empty)
            {
                return(strBuilder);
            }

            if (!builder.Options.FrameStyleInnerDelimiterEqualsOuter)
            {
                format = format.Substring(0, 1) + format.Substring(1, format.Length - 2).Replace(delim, '|') + format.Substring(format.Length - 1);
            }

            // find the longest formatted line
            var maxRowLength = Math.Max(0, builder.Rows.Any() ? builder.Rows.Max(row => string.Format(format, row.ToArray()).Length) : 0);

            string[] vars = GetVars(builder, maxRowLength);

            string beginTable = vars[0];
            string divider    = vars[1];
            string endTable   = vars[2];

            // add each row
            var results = builder.Rows.Select(row => string.Format(format, row.ToArray())).ToList();

            // header
            if (builder.Column != null && builder.Column.Any() && builder.Column.Max(x => (x ?? string.Empty).ToString().Length) > 0)
            {
                strBuilder.AppendLine(beginTable);
                strBuilder.AppendLine(string.Format(format, builder.Column.ToArray()));
            }

            foreach (var row in results)
            {
                strBuilder.AppendLine(divider);
                strBuilder.AppendLine(row);
            }

            strBuilder.AppendLine(endTable);

            if (builder.Options.MetaRowPosition == ConsoleTableBuilderOption.MetaRowPositions.Bottom)
            {
                strBuilder.AppendLine(BuildMetaRowFormat(builder));
            }
            return(strBuilder);
        }
예제 #10
0
        public static ConsoleTableBuilder AddColumn(this ConsoleTableBuilder builder, List <string> columnNames)
        {
#if NET35
            columnNames.ForEach(f => builder.Column.Add(f));
#else
            builder.Column.AddRange(columnNames);
#endif
            return(builder);
        }
예제 #11
0
        public static ConsoleTableBuilder WithHeaderTextAlignment(this ConsoleTableBuilder builder, Dictionary <int, TextAligntment> alignmentData)
        {
            if (alignmentData != null)
            {
                builder.HeaderTextAligmentData = alignmentData;
            }

            return(builder);
        }
예제 #12
0
        public static ConsoleTableBuilder WithMinLength(this ConsoleTableBuilder builder, Dictionary <int, int> minLengthData)
        {
            if (minLengthData != null)
            {
                builder.MinLengthData = minLengthData;
            }

            return(builder);
        }
예제 #13
0
        public static ConsoleTableBuilder AddRow(this ConsoleTableBuilder builder, DataRow row)
        {
            if (row == null)
            {
                throw new ArgumentNullException(nameof(row));
            }

            builder.Rows.Add(new List <object>(row.ItemArray));
            return(builder);
        }
예제 #14
0
        public static ConsoleTableBuilder AddRow(this ConsoleTableBuilder builder, List <List <object> > rows)
        {
            if (rows == null)
            {
                return(builder);
            }

            builder.Rows.AddRange(rows);
            return(builder);
        }
예제 #15
0
        public static ConsoleTableBuilder AddRow(this ConsoleTableBuilder builder, DataRow row)
        {
            if (row == null)
            {
                return(builder);
            }

            builder.Rows.Add(new List <object>(row.ItemArray));
            return(builder);
        }
예제 #16
0
        public static ConsoleTableBuilder AddRow(this ConsoleTableBuilder builder, List <List <object> > rows)
        {
            if (rows == null)
            {
                throw new ArgumentNullException(nameof(rows));
            }

            builder.Rows.AddRange(rows);
            return(builder);
        }
예제 #17
0
        private static StringBuilder CreateTableForAlternativeFormat(ConsoleTableBuilder builder)
        {
            var strBuilder = new StringBuilder();

            if (builder.Options.MetaRowPosition == ConsoleTableBuilderOption.MetaRowPositions.Top)
            {
                strBuilder.AppendLine(BuildMetaRowFormat(builder));
            }

            // create the string format with padding
            var format = builder.Format(builder.Options.Delimiter);

            if (format == string.Empty)
            {
                return(strBuilder);
            }

            var skipFirstRow  = false;
            var columnHeaders = string.Empty;

            if (builder.Column != null && builder.Column.Any() && builder.Column.Max(x => (x ?? string.Empty).ToString().Length) > 0)
            {
                skipFirstRow  = false;
                columnHeaders = string.Format(format, builder.Column.ToArray());
            }
            else
            {
                skipFirstRow  = true;
                columnHeaders = string.Format(format, builder.Rows.First().ToArray());
            }

            // create the divider
            var divider     = Regex.Replace(columnHeaders, @"[^|]", builder.Options.DividerChar.ToString());
            var dividerPlus = divider.Replace("|", "+");

            strBuilder.AppendLine(dividerPlus);
            strBuilder.AppendLine(columnHeaders);

            // add each row
            var results = builder.Rows.Skip(skipFirstRow ? 1 : 0).Select(row => string.Format(format, row.ToArray())).ToList();

            foreach (var row in results)
            {
                strBuilder.AppendLine(dividerPlus);
                strBuilder.AppendLine(row);
            }
            strBuilder.AppendLine(dividerPlus);

            if (builder.Options.MetaRowPosition == ConsoleTableBuilderOption.MetaRowPositions.Bottom)
            {
                strBuilder.AppendLine(BuildMetaRowFormat(builder));
            }
            return(strBuilder);
        }
예제 #18
0
        public static ConsoleTableBuilder From <T>(List <T> list)
        {
            if (list == null || !list.Any())
            {
                throw new ArgumentException($"{nameof(list)} cannot be null or empty");
            }

            var           builder = new ConsoleTableBuilder();
            var           isClass = typeof(T).IsClass;
            var           props   = list.First().GetType().GetProperties();
            List <object> columnNames;

            if (isClass)
            {
                columnNames = props.Select(p => p.Name as object).ToList();
            }
            else
            {
                columnNames = new List <object> {
                    "Value"
                };
            }


            builder.Column = columnNames;

            foreach (var item in list)
            {
                if (isClass == true)
                {
                    var itemPropValues = new List <object>();

                    foreach (var prop in props)
                    {
#if NET35
                        var objValue = prop.GetValue(item, new object[] { });
#else
                        var objValue = prop.GetValue(item);
#endif
                        itemPropValues.Add(objValue);
                    }

                    builder.Rows.Add(itemPropValues);
                }
                else
                {
                    builder.Rows.Add(new List <object> {
                        item
                    });
                }
            }

            return(builder);
        }
예제 #19
0
        public static ConsoleTableBuilder AddRow(this ConsoleTableBuilder builder, params object[] rowValues)
        {
            if (rowValues == null)
            {
                return(builder);
            }

            builder.Rows.Add(new List <object>(rowValues));

            return(builder);
        }
예제 #20
0
        public static ConsoleTableBuilder AddRow(this ConsoleTableBuilder builder, params object[] rowValues)
        {
            if (rowValues == null)
            {
                throw new ArgumentNullException(nameof(rowValues));
            }

            builder.Rows.Add(new List <object>(rowValues));

            return(builder);
        }
예제 #21
0
        public static ConsoleTableBuilder WithColumnFormatter(this ConsoleTableBuilder builder, int columnIndex, Func <string, string> formatter)
        {
            if (!builder.ColumnFormatterStore.ContainsKey(columnIndex))
            {
                builder.ColumnFormatterStore.Add(columnIndex, formatter);
            }
            else
            {
                builder.ColumnFormatterStore[columnIndex] = formatter;
            }

            return(builder);
        }
        public static ConsoleTableBuilder From(List <object> list)
        {
            var builder = new ConsoleTableBuilder();

            foreach (var value in list)
            {
                builder.Rows.Add(new List <object> {
                    value
                });
            }

            return(builder);
        }
        /// <summary>
        /// Text alignment definition
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="alignmentData"></param>
        /// <returns></returns>
        public static ConsoleTableBuilder WithTextAlignment(this ConsoleTableBuilder builder, Dictionary <int, TextAligntment> alignmentData)
        {
            if (alignmentData != null)
            {
                builder.TextAligmentData = new Dictionary <int, string>();

                foreach (var item in alignmentData)
                {
                    builder.TextAligmentData.Add(item.Key, item.Value == TextAligntment.Left ? "-" : string.Empty);
                }
            }

            return(builder);
        }
예제 #24
0
        private static string BuildMetaRowFormat(ConsoleTableBuilder builder)
        {
            var result = new StringBuilder().AppendFormat(builder.Options.MetaRowFormat, builder.Options.MetaRowParams).ToString();

            if (result.Contains(AppConstants.MetaRow.ROW_COUNT))
            {
                result = result.Replace(AppConstants.MetaRow.ROW_COUNT, builder.Rows.Count.ToString());
            }

            if (result.Contains(AppConstants.MetaRow.COLUMN_COUNT))
            {
                result = result.Replace(AppConstants.MetaRow.COLUMN_COUNT, builder.GetCadidateColumnLengths().Count.ToString());
            }

            return(result);
        }
예제 #25
0
        public static ConsoleTableBuilder From(List <List <object> > rows)
        {
            var builder = new ConsoleTableBuilder();

            if (rows == null)
            {
                return(builder);
            }

            foreach (var row in rows)
            {
                builder.Rows.Add(row);
            }

            return(builder);
        }
        public static ConsoleTableBuilder From(List <List <object> > rows)
        {
            if (rows == null || !rows.Any())
            {
                throw new Exception("Invail rows");
            }

            var builder = new ConsoleTableBuilder();

            foreach (var row in rows)
            {
                builder.Rows.Add(row);
            }

            return(builder);
        }
예제 #27
0
        public static ConsoleTableBuilder From(List <object[]> rows)
        {
            if (rows == null || !rows.Any())
            {
                throw new ArgumentException($"{nameof(rows)} cannot be null or empty");
            }

            var builder = new ConsoleTableBuilder();

            foreach (var row in rows)
            {
                builder.Rows.Add(new List <object>(row));
            }

            return(builder);
        }
예제 #28
0
        private static StringBuilder CreateTableForDefaultFormat(ConsoleTableBuilder builder)
        {
            var strBuilder = new StringBuilder();

            if (builder.Options.MetaRowPosition == MetaRowPosition.Top)
            {
                strBuilder.AppendLine(BuildMetaRowFormat(builder));
            }

            // create the string format with padding
            var format = builder.Format(builder.Options.Delimiter);

            if (format == string.Empty)
            {
                return(strBuilder);
            }

            // find the longest formatted line
            var maxRowLength = Math.Max(0, builder.Rows.Any() ? builder.Rows.Max(row => string.Format(format, row.ToArray()).Length) : 0);

            // add each row
            var results = builder.Rows.Select(row => string.Format(format, row.ToArray())).ToList();

            // create the divider
            var divider = string.Join("", Enumerable.Repeat(builder.Options.DividerString, maxRowLength).ToArray());

            // header
            if (builder.Column != null && builder.Column.Any() && builder.Column.Max(x => (x ?? string.Empty).ToString().Length) > 0)
            {
                strBuilder.AppendLine(divider);
                strBuilder.AppendLine(string.Format(format, builder.Column.ToArray()));
            }

            foreach (var row in results)
            {
                strBuilder.AppendLine(divider);
                strBuilder.AppendLine(row);
            }

            strBuilder.AppendLine(divider);

            if (builder.Options.MetaRowPosition == MetaRowPosition.Bottom)
            {
                strBuilder.AppendLine(BuildMetaRowFormat(builder));
            }
            return(strBuilder);
        }
예제 #29
0
        public static ConsoleTableBuilder From(DataTable dt)
        {
            var builder = new ConsoleTableBuilder();

            var columnNames = dt.Columns.Cast <DataColumn>()
                              .Select(x => x.ColumnName)
                              .ToList();

            builder.Column = new List <object>(columnNames);

            foreach (DataRow row in dt.Rows)
            {
                builder.Rows.Add(new List <object>(row.ItemArray));
            }

            return(builder);
        }
        public static ConsoleTableBuilder From(ConsoleTableBaseData baseData)
        {
            var builder = new ConsoleTableBuilder();

            if (baseData != null)
            {
                if (baseData.Rows != null)
                {
                    builder.Rows = baseData.Rows;
                }

                if (baseData.Column != null)
                {
                    builder.Column = baseData.Column;
                }
            }

            return(builder);
        }