Skip to content

Commit

Permalink
Handle dynamic increase of column width, change defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
elsand committed Oct 7, 2024
1 parent a25cd20 commit feaf015
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Digdir.Tool.Dialogporten.SlackNotifier.Common;

public static class AsciiTableFormatter
{
public static string ToAsciiTable(this IEnumerable<IEnumerable<object>> rows, int maxColumnWidth = 30) =>
public static string ToAsciiTable(this IEnumerable<IEnumerable<object>> rows, int maxColumnWidth = 90) =>
rows.Select(x => x.ToList())
.ToList()
.ToAsciiTable(maxColumnWidth);
Expand Down Expand Up @@ -92,6 +92,12 @@ private static void AddLineBreaks(List<List<object>> rows, int maxColumnWidth)
if ((currentLine + word).Length > maxColumnWidth)
{
lines.Add(currentLine.TrimEnd());

if (word.Length > maxColumnWidth)
{
maxColumnWidth = word.Length;
}

currentLine = "";
}
currentLine += word + " ";
Expand All @@ -101,7 +107,7 @@ private static void AddLineBreaks(List<List<object>> rows, int maxColumnWidth)
lines.Add(currentLine.TrimEnd());
}

row[colIndex] = string.Join("\n", lines);
row[colIndex] = string.Join("\n", lines).Trim();
}
else if (row[colIndex].ToString()?.Length > maxColumnWidth)
{
Expand All @@ -110,6 +116,10 @@ private static void AddLineBreaks(List<List<object>> rows, int maxColumnWidth)
for (var i = 0; i < strValue?.Length; i += maxColumnWidth)
{
brokenLines.Add(strValue.Substring(i, Math.Min(maxColumnWidth, strValue.Length - i)));
if (brokenLines.Last().Length > maxColumnWidth)
{
maxColumnWidth = brokenLines.Last().Length;
}
}
row[colIndex] = string.Join("\n", brokenLines);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static string ToAsciiTableExceptionReport(this IEnumerable<AppInsightsQue
.Select(table => Enumerable.Empty<List<object>>()
.Append(table.Columns.Select(x => (object)x.Name))
.Concat(table.Rows)
.ToAsciiTable(maxColumnWidth: 72));
.ToAsciiTable());
return string.Join(Environment.NewLine, asciiTables);
}

Expand Down

0 comments on commit feaf015

Please sign in to comment.