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); }
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); }
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); }
private static StringBuilder CreateTableForMarkdownFormat(ConsoleTableBuilder builder) { var strBuilder = new StringBuilder(); BuildMetaRowsFormat(builder, strBuilder, MetaRowPositions.Top); // create the string format with padding var format = builder.Format('|'); 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, @"[^|]", '-'.ToString()); strBuilder.AppendLine(columnHeaders); strBuilder.AppendLine(divider); // add each row var results = builder.Rows.Skip(skipFirstRow ? 1 : 0).Select(row => string.Format(format, row.ToArray())).ToList(); results.ForEach(row => strBuilder.AppendLine(row)); BuildMetaRowsFormat(builder, strBuilder, MetaRowPositions.Bottom); return(strBuilder); }
private static StringBuilder CreateTableForCustomFormat(ConsoleTableBuilder builder, Dictionary <CharMapPositions, char> charMapDefinition, Dictionary <HeaderCharMapPositions, char> headerCharMapDefinition = null) { var filledMap = FillCharMap(charMapDefinition); var filledHeaderMap = FillHeaderCharMap(headerCharMapDefinition); var strBuilder = new StringBuilder(); BuildMetaRowsFormat(builder, strBuilder, MetaRowPositions.Top); // create the string format with padding char delim = 'x'; //for example: | {0,-14} | {1,-29} | {2,-13} | {3,-3} | {4,-22} | string format = builder.Format(delim); 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); string formatWithoutContent = builder.FormatWithoutContent(delim); var beginTableFormat = formatWithoutContent; beginTableFormat = filledMap[CharMapPositions.TopLeft] + beginTableFormat.Substring(1); beginTableFormat = beginTableFormat.Substring(0, beginTableFormat.Length - 1) + filledMap[CharMapPositions.TopRight]; beginTableFormat = beginTableFormat.Replace(' ', filledMap[CharMapPositions.BorderX]).Replace(delim, filledMap[CharMapPositions.TopCenter]); if (builder.TableTitle.Length > beginTableFormat.Length) { if (beginTableFormat.Length < 10) { builder.TableTitle = builder.TableTitle.Substring(0, beginTableFormat.Length - 4); } else { if (beginTableFormat.Length < 20) { builder.TableTitle = builder.TableTitle.Substring(0, beginTableFormat.Length - 7); } else { builder.TableTitle = builder.TableTitle.Substring(0, beginTableFormat.Length - 7) + "..."; } } } if (!string.IsNullOrEmpty(builder.TableTitle) && builder.TableTitle.Trim().Length > 0) // !IsNullOrWhiteSpace { var newBeginTableFormat = beginTableFormat.Substring(0, (beginTableFormat.Length - builder.TableTitle.Length) / 2 - 1) + ' '; newBeginTableFormat += builder.TableTitle + ' '; newBeginTableFormat += beginTableFormat.Substring(newBeginTableFormat.Length, beginTableFormat.Length - newBeginTableFormat.Length); beginTableFormat = newBeginTableFormat; } if (beginTableFormat.Trim('\0').Length == 0) { beginTableFormat = string.Empty; } var rowContentTableFormat = format; rowContentTableFormat = filledMap[CharMapPositions.BorderY] + rowContentTableFormat.Substring(1); rowContentTableFormat = rowContentTableFormat.Substring(0, rowContentTableFormat.Length - 1) + filledMap[CharMapPositions.BorderY]; rowContentTableFormat = rowContentTableFormat.Replace(delim, filledMap[CharMapPositions.DividerY]); var dividerTableFormat = formatWithoutContent; dividerTableFormat = filledMap[CharMapPositions.MiddleLeft] + dividerTableFormat.Substring(1); dividerTableFormat = dividerTableFormat.Substring(0, dividerTableFormat.Length - 1) + filledMap[CharMapPositions.MiddleRight]; dividerTableFormat = dividerTableFormat.Replace(' ', filledMap[CharMapPositions.DividerX]).Replace(delim, filledMap[CharMapPositions.MiddleCenter]); if (dividerTableFormat.Trim('\0').Length == 0) { dividerTableFormat = string.Empty; } var endTableFormat = formatWithoutContent; endTableFormat = filledMap[CharMapPositions.BottomLeft] + endTableFormat.Substring(1); endTableFormat = endTableFormat.Substring(0, endTableFormat.Length - 1) + filledMap[CharMapPositions.BottomRight]; endTableFormat = endTableFormat.Replace(' ', filledMap[CharMapPositions.BorderX]).Replace(delim, filledMap[CharMapPositions.BottomCenter]); if (endTableFormat.Trim('\0').Length == 0) { endTableFormat = string.Empty; } // header formats var beginHeaderFormat = string.Empty; var rowContentHeaderFormat = string.Empty; var endHeaderFormat = string.Empty; var hasHeader = builder.Column != null && builder.Column.Any() && builder.Column.Max(x => (x ?? string.Empty).ToString().Length) > 0; if (hasHeader) { if (filledHeaderMap != null) { beginHeaderFormat = formatWithoutContent; beginHeaderFormat = filledHeaderMap[HeaderCharMapPositions.TopLeft] + beginHeaderFormat.Substring(1); beginHeaderFormat = beginHeaderFormat.Substring(0, beginHeaderFormat.Length - 1) + filledHeaderMap[HeaderCharMapPositions.TopRight]; beginHeaderFormat = beginHeaderFormat.Replace(' ', filledHeaderMap[HeaderCharMapPositions.BorderXTop]).Replace(delim, filledHeaderMap[HeaderCharMapPositions.TopCenter]); if (builder.TableTitle.Length > beginHeaderFormat.Length) { if (beginHeaderFormat.Length < 10) { builder.TableTitle = builder.TableTitle.Substring(0, beginHeaderFormat.Length - 4); } else { if (beginHeaderFormat.Length < 20) { builder.TableTitle = builder.TableTitle.Substring(0, beginHeaderFormat.Length - 7); } else { builder.TableTitle = builder.TableTitle.Substring(0, beginHeaderFormat.Length - 7) + "..."; } } } if (!string.IsNullOrEmpty(builder.TableTitle) && builder.TableTitle.Trim().Length > 0) // !IsNullOrWhiteSpace { var newBeginHeaderFormat = beginHeaderFormat.Substring(0, (beginHeaderFormat.Length - builder.TableTitle.Length) / 2 - 1) + ' '; newBeginHeaderFormat += builder.TableTitle + ' '; newBeginHeaderFormat += beginHeaderFormat.Substring(newBeginHeaderFormat.Length, beginHeaderFormat.Length - newBeginHeaderFormat.Length); beginHeaderFormat = newBeginHeaderFormat; } if (beginHeaderFormat.Trim('\0').Length == 0) { beginHeaderFormat = string.Empty; } rowContentHeaderFormat = format; rowContentHeaderFormat = filledHeaderMap[HeaderCharMapPositions.BorderY] + rowContentHeaderFormat.Substring(1); rowContentHeaderFormat = rowContentHeaderFormat.Substring(0, rowContentHeaderFormat.Length - 1) + filledHeaderMap[HeaderCharMapPositions.BorderY]; rowContentHeaderFormat = rowContentHeaderFormat.Replace(delim, filledHeaderMap[HeaderCharMapPositions.Divider]); endHeaderFormat = formatWithoutContent; endHeaderFormat = filledHeaderMap[HeaderCharMapPositions.BottomLeft] + endHeaderFormat.Substring(1); endHeaderFormat = endHeaderFormat.Substring(0, endHeaderFormat.Length - 1) + filledHeaderMap[HeaderCharMapPositions.BottomRight]; endHeaderFormat = endHeaderFormat.Replace(' ', filledHeaderMap[HeaderCharMapPositions.BorderXBottom]).Replace(delim, filledHeaderMap[HeaderCharMapPositions.BottomCenter]); if (endHeaderFormat.Trim('\0').Length == 0) { endHeaderFormat = string.Empty; } } } // add each row var results = builder.Rows.Select(row => string.Format(rowContentTableFormat, row.ToArray())).ToList(); // header if (hasHeader) { if (filledHeaderMap != null) { if (beginHeaderFormat.Length > 0) { strBuilder.AppendLine(beginHeaderFormat); } strBuilder.AppendLine(string.Format(rowContentHeaderFormat, builder.Column.ToArray())); } else { if (beginTableFormat.Length > 0) { strBuilder.AppendLine(beginTableFormat); } strBuilder.AppendLine(string.Format(rowContentTableFormat, builder.Column.ToArray())); } } //else //{ // if (beginTableFormat.Length > 0) strBuilder.AppendLine(beginTableFormat); // strBuilder.AppendLine(string.Format(rowContentTableFormat, builder.Column.ToArray())); //} var isFirstRow = true; foreach (var row in results) { if (isFirstRow) { if (hasHeader) { if ((string.IsNullOrEmpty(endHeaderFormat) || endHeaderFormat.Length == 0) && dividerTableFormat.Length > 0) { strBuilder.AppendLine(dividerTableFormat); } else { if (endHeaderFormat.Length > 0) { strBuilder.AppendLine(endHeaderFormat); } } } else { if (beginTableFormat.Length > 0) { strBuilder.AppendLine(beginTableFormat); } } isFirstRow = false; } else { if (dividerTableFormat.Length > 0) { strBuilder.AppendLine(dividerTableFormat); } } strBuilder.AppendLine(row); } if (results.Any()) { if (endTableFormat.Length > 0) { strBuilder.AppendLine(endTableFormat); } } else { if ((string.IsNullOrEmpty(endHeaderFormat) || endHeaderFormat.Length == 0) && endTableFormat.Length > 0) { strBuilder.AppendLine(endTableFormat); } else { if (endHeaderFormat.Length > 0) { strBuilder.AppendLine(endHeaderFormat); } } } BuildMetaRowsFormat(builder, strBuilder, MetaRowPositions.Bottom); return(strBuilder); }