Skip to content

Commit

Permalink
(maint) Add helper to split on max line lengths
Browse files Browse the repository at this point in the history
These changes introduces a new string helper that ensures
that each line in the output will not be any longer than the
configured maximum line length. And attempt to split these
lines at the last available non-letter and non-digit before the
length limitation.
  • Loading branch information
AdmiringWorm committed Jul 25, 2023
1 parent 367319a commit a143527
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
80 changes: 80 additions & 0 deletions src/chocolatey/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@
namespace chocolatey
{
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Web.UI;
using infrastructure.app;
using infrastructure.logging;

Expand Down Expand Up @@ -50,6 +55,81 @@ public static string FormatWith(this string input, params object[] formatting)
}
}

/// <summary>
/// Splits any Newline elements and ensures that each line is no longer than the configured <paramref name="maxLineLength"/>.
/// Lines longer than the specified line length will be split on the last non-letter or digit before the max length.
/// </summary>
/// <param name="input">The input to split any lines on.</param>
/// <param name="linePrefix">The line prefix used for all lines not being the first line.</param>
/// <param name="maxLineLength">Maximum length of the line.</param>
/// <returns>The splitted formatted lines.</returns>
/// <remarks>Not recommended to be used in hot paths.</remarks>
public static string SplitOnSpace(this string input, string linePrefix = "", int maxLineLength = 70)
{
if (string.IsNullOrWhiteSpace(input))
{
return string.Empty;
}

var sb = new StringBuilder(input.Length);
var firstLine = true;
var stack = new Stack<string>(input.Split('\n').Reverse());

while (stack.Count > 0)
{
var currentLine = stack.Pop();

if (currentLine.Length <= maxLineLength)
{
if (!firstLine && !string.IsNullOrEmpty(currentLine))
{
sb.Append(linePrefix);
}

sb.AppendLine(currentLine.TrimEnd());
}
else
{
var index = 70 - 1;

for (; index >= 0; index--)
{
if (char.IsWhiteSpace(currentLine[index]) || !char.IsLetterOrDigit(currentLine[index]))
{
break;
}
}

if (index <= 0)
{
index = maxLineLength;
}

if (!firstLine)
{
sb.Append(linePrefix);
}

var subLine = currentLine.Substring(0, index);
sb.AppendLine(subLine.TrimEnd());

if (stack.Count > 0)
{
var nextLine = currentLine.Substring(index + 1).TrimStart() + stack.Pop();
stack.Push(nextLine);
}
else
{
stack.Push(currentLine.Substring(index + 1).TrimStart());
}
}

firstLine = false;
}

return sb.ToString();
}

/// <summary>
/// Performs a trim only if the item is not null
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public ICollection<ValidationResult> Validate(ChocolateyConfiguration config)
result.Add(new ValidationResult
{
ExitCode = 0,
Message = "System Cache directory is not locked down to administrators. Remove the directory '{0}' to have Chocolatey CLI create it with the proper permissions.".FormatWith(cacheFolderPath),
Message = "System Cache directory is not locked down to administrators.\nRemove the directory '{0}' to have Chocolatey CLI create it with the proper permissions.".FormatWith(cacheFolderPath).SplitOnSpace(linePrefix: " "),
Status = ValidationStatus.Warning
});
}
Expand Down Expand Up @@ -106,7 +106,7 @@ public ICollection<ValidationResult> Validate(ChocolateyConfiguration config)
result.Add(new ValidationResult
{
ExitCode = 1, // Should we error?
Message = "System Cache directory was not created, or could not be locked down to administrators.",
Message = "System Cache directory was not created, or could not be locked down to administrators.".SplitOnSpace(linePrefix: " "),
Status = ValidationStatus.Error
});
}
Expand Down

0 comments on commit a143527

Please sign in to comment.