Skip to content

Commit f2512fe

Browse files
apogeeoakjonsequitur
authored andcommitted
Made requested changes.
1 parent 96da131 commit f2512fe

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

src/System.CommandLine/Help/HelpBuilder.cs

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ private void AppendDescription(string description)
215215
protected virtual void AppendHelpItems(IReadOnlyCollection<HelpItem> helpItems)
216216
{
217217
if (helpItems is null)
218+
{
218219
throw new ArgumentNullException(nameof(helpItems));
220+
}
219221

220222
var table = CreateTable(helpItems, item => new[]
221223
{
@@ -228,42 +230,46 @@ protected virtual void AppendHelpItems(IReadOnlyCollection<HelpItem> helpItems)
228230
}
229231

230232
/// <summary>
231-
/// Create a read only table of strings using the projection of a collection.
233+
/// Create a table of strings using the projection of a collection.
232234
/// </summary>
233235
/// <typeparam name="T">The type of the elements of <paramref name="collection"/>.</typeparam>
234236
/// <param name="collection">The collection of values to create the table from.</param>
235237
/// <param name="selector">A transformation function to apply to each element of <paramref name="collection"/>.</param>
236238
/// <returns>
237-
/// A read only table of strings whose elements are the projection of the collection with whitespace formatting removed.
239+
/// A table of strings whose elements are the projection of the collection with whitespace formatting removed.
238240
/// </returns>
239-
protected virtual ReadOnlyCollection<ReadOnlyCollection<string>> CreateTable<T>(IEnumerable<T> collection, Func<T, IEnumerable<string>> selector)
241+
protected virtual IEnumerable<IReadOnlyList<string>> CreateTable<T>(IEnumerable<T> collection, Func<T, IEnumerable<string>> selector)
240242
{
241243
return collection.Select(selector)
242244
.Select(row => row
243-
.Select(element => RemoveFormatting(element))
244-
.ToList().AsReadOnly())
245-
.ToList().AsReadOnly();
245+
.Select(element => ShortenWhitespace(element))
246+
.ToArray());
246247
}
247248

248249
/// <summary>
249250
/// Allocate space for columns favoring minimal rows. Wider columns are allocated more space if it is available.
250251
/// </summary>
251252
/// <param name="table">The table of values to determine column widths for.</param>
252253
/// <returns>A collection of column widths.</returns>
253-
protected virtual ReadOnlyCollection<int> ColumnWidths(IEnumerable<IList<string>> table)
254+
private IReadOnlyList<int> ColumnWidths(IEnumerable<IReadOnlyList<string>> table)
254255
{
255256
if (!table.Any())
256-
return Array.AsReadOnly(Array.Empty<int>());
257+
{
258+
return Array.Empty<int>();
259+
}
257260

258261
var columns = table.First().Count;
259-
Debug.Assert(table.All(e => e.Count == columns), $"Every row in {nameof(table)} must have the same number of columns.");
260262
var unsetWidth = -1;
261263
var widths = new int[columns];
262264
for (int i = 0; i < columns; ++i)
265+
{
263266
widths[i] = unsetWidth;
267+
}
264268
var maxWidths = new int[columns];
265269
for (int i = 0; i < columns; ++i)
270+
{
266271
maxWidths[i] = table.Max(row => row[i].Length);
272+
}
267273

268274
var nonEmptyColumns = maxWidths.Count(width => width > 0);
269275

@@ -272,7 +278,9 @@ protected virtual ReadOnlyCollection<int> ColumnWidths(IEnumerable<IList<string>
272278
// If available space is not sufficent then do not wrap.
273279
// If all columns are empty then return array of zeros.
274280
if (available - nonEmptyColumns < 0 || nonEmptyColumns == 0)
275-
return Array.AsReadOnly(maxWidths);
281+
{
282+
return maxWidths;
283+
}
276284

277285
// Loop variables.
278286
var unset = nonEmptyColumns;
@@ -293,25 +301,29 @@ protected virtual ReadOnlyCollection<int> ColumnWidths(IEnumerable<IList<string>
293301
// Attempt to fit column to single line.
294302
var width = maxWidths[i];
295303
if (allocateRemaining)
304+
{
296305
width = Math.Min(width, equal);
306+
}
297307
if (width <= equal)
308+
{
298309
widths[i] = width;
310+
}
299311
}
300312
}
301313
previousUnset = unset;
302314
unset = widths.Count(width => width < 0);
303315
--loopLimit;
304316
}
305317

306-
return Array.AsReadOnly(widths);
318+
return widths;
307319
}
308320

309321
/// <summary>
310322
/// Writes a table of strings to the console.
311323
/// </summary>
312324
/// <param name="table">The table of values to write.</param>
313325
/// <param name="columnWidths">The width of each column of the table.</param>
314-
protected virtual void AppendTable(IEnumerable<IEnumerable<string>> table, IReadOnlyList<int> columnWidths)
326+
private void AppendTable(IEnumerable<IEnumerable<string>> table, IReadOnlyList<int> columnWidths)
315327
{
316328
foreach (var row in table)
317329
AppendRow(row, columnWidths);
@@ -322,7 +334,7 @@ protected virtual void AppendTable(IEnumerable<IEnumerable<string>> table, IRead
322334
/// </summary>
323335
/// <param name="row">The row of elements to write.</param>
324336
/// <param name="columnWidths">The width of each column of the table.</param>
325-
protected virtual void AppendRow(IEnumerable<string> row, IReadOnlyList<int> columnWidths)
337+
private void AppendRow(IEnumerable<string> row, IReadOnlyList<int> columnWidths)
326338
{
327339
var split = row.Select((element, index) => SplitText(element, columnWidths[index])).ToArray();
328340
var longest = split.Max(lines => lines.Count);
@@ -361,21 +373,21 @@ protected virtual void AppendRow(IEnumerable<string> row, IReadOnlyList<int> col
361373
/// Collection of lines of at most <paramref name="width"/> characters
362374
/// generated from the supplied <paramref name="text"/>.
363375
/// </returns>
364-
protected virtual ReadOnlyCollection<string> SplitText(string text, int width)
376+
protected virtual IReadOnlyList<string> SplitText(string text, int width)
365377
{
366378
if (text is null)
367379
throw new ArgumentNullException(nameof(text), $"{nameof(text)} cannot be null.");
368380
if (width < 0)
369381
throw new ArgumentOutOfRangeException(nameof(width), $"{nameof(width)} must be non-negative.");
370382

371383
if (width == 0)
372-
return Array.AsReadOnly(Array.Empty<string>());
384+
return Array.Empty<string>();
373385

374386
var separator = ' ';
375387

376388
var start = 0;
377389
var lines = new List<string>();
378-
text = RemoveFormatting(text);
390+
text = ShortenWhitespace(text);
379391

380392
while (start < text.Length - width)
381393
{
@@ -398,7 +410,7 @@ protected virtual ReadOnlyCollection<string> SplitText(string text, int width)
398410

399411
lines.Add(text.Substring(start, text.Length - start));
400412

401-
return lines.AsReadOnly();
413+
return lines;
402414
}
403415

404416
/// <summary>
@@ -724,20 +736,13 @@ private bool ShouldDisplayArgumentHelp(ICommand? command)
724736

725737
private int GetConsoleWindowWidth(IConsole console)
726738
{
727-
try
728-
{
729-
if (console is SystemConsole && System.Console.WindowWidth > 0)
730-
return System.Console.WindowWidth;
731-
else
732-
return int.MaxValue;
733-
}
734-
catch
735-
{
739+
if (console is SystemConsole systemConsole)
740+
return systemConsole.GetConsoleWindowWidth();
741+
else
736742
return int.MaxValue;
737-
}
738743
}
739744

740-
private string RemoveFormatting(string input)
745+
private string ShortenWhitespace(string input)
741746
{
742747
return Regex.Replace(input, @"\s+", " ");
743748
}

src/System.CommandLine/IO/SystemConsole.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,7 @@ public SystemConsole()
2020
public bool IsOutputRedirected => Console.IsOutputRedirected;
2121

2222
public bool IsInputRedirected => Console.IsInputRedirected;
23+
24+
public int GetConsoleWindowWidth() => IsOutputRedirected ? int.MaxValue : System.Console.WindowWidth;
2325
}
2426
}

0 commit comments

Comments
 (0)