Skip to content

Commit a3f6e96

Browse files
authored
dotnet package search tests use default width (#7171)
1 parent c0c368a commit a3f6e96

16 files changed

Lines changed: 72 additions & 56 deletions

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageSearch/PackageSearchArgs.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ internal class PackageSearchArgs
1313
{
1414
private const int DefaultSkip = 0;
1515
private const int DefaultTake = 20;
16+
public int ConsoleWidth { get; set; }
1617
public List<string> Sources { get; set; }
1718
public int Skip { get; set; }
1819
public int Take { get; set; }

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageSearch/PackageSearchCommand.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public static void Register(Command rootCommand, Func<ILoggerWithColor> getLogge
118118
Interactive = parserResult.GetValue(interactive),
119119
Prerelease = parserResult.GetValue(prerelease),
120120
Logger = logger,
121+
ConsoleWidth = GetConsoleWidth(),
121122
};
122123

123124
return await setupSettingsAndRunSearchAsync(packageSearchArgs, parserResult.GetValue(configFile), cancelationToken);
@@ -132,6 +133,24 @@ public static void Register(Command rootCommand, Func<ILoggerWithColor> getLogge
132133
rootCommand.Subcommands.Add(searchCommand);
133134
}
134135

136+
internal static int GetConsoleWidth()
137+
{
138+
try
139+
{
140+
int width = Console.WindowWidth;
141+
if (width > 0)
142+
{
143+
return width;
144+
}
145+
}
146+
catch (IOException)
147+
{
148+
// Console.WindowWidth throws IOException when no console is attached
149+
}
150+
151+
return Table.DefaultWindowWidth;
152+
}
153+
135154
public static async Task<int> SetupSettingsAndRunSearchAsync(PackageSearchArgs packageSearchArgs, string configFile, CancellationToken cancellationToken)
136155
{
137156
DefaultCredentialServiceUtility.SetupDefaultCredentialService(packageSearchArgs.Logger, !packageSearchArgs.Interactive);

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageSearch/PackageSearchResultTableRenderer.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ internal class PackageSearchResultTableRenderer : IPackageSearchResultRenderer
2020
private static readonly string SourceSeparator = new('*', LineSeparatorLength);
2121
private PackageSearchVerbosity _verbosity;
2222
private bool _exactMatch;
23+
private int _consoleWidth;
2324

24-
public PackageSearchResultTableRenderer(string searchTerm, ILoggerWithColor loggerWithColor, PackageSearchVerbosity verbosity, bool exactMatch)
25+
public PackageSearchResultTableRenderer(string searchTerm, ILoggerWithColor loggerWithColor, PackageSearchVerbosity verbosity, bool exactMatch, int consoleWidth)
2526
{
2627
_searchTerm = searchTerm;
2728
_loggerWithColor = loggerWithColor;
2829
_verbosity = verbosity;
2930
_exactMatch = exactMatch;
31+
_consoleWidth = consoleWidth;
3032
}
3133

3234
public void Add(PackageSource source, IEnumerable<IPackageSearchMetadata> completedSearch)
@@ -35,7 +37,7 @@ public void Add(PackageSource source, IEnumerable<IPackageSearchMetadata> comple
3537
_loggerWithColor.LogMinimal($"Source: {source.Name} ({source.SourceUri})");
3638

3739
ITableFormatStrategy strategy = TableFormatStrategyFactory.GetStrategy(_verbosity, _exactMatch);
38-
Table table = strategy.CreateTable();
40+
Table table = strategy.CreateTable(_consoleWidth);
3941
PopulateTableWithResultsAsync(completedSearch, table, _verbosity);
4042
table.PrintResult(_searchTerm, _loggerWithColor);
4143
}

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageSearch/PackageSearchRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static async Task<int> RunAsync(
4040
}
4141
else
4242
{
43-
packageSearchResultRenderer = new PackageSearchResultTableRenderer(packageSearchArgs.SearchTerm, packageSearchArgs.Logger, packageSearchArgs.Verbosity, packageSearchArgs.ExactMatch);
43+
packageSearchResultRenderer = new PackageSearchResultTableRenderer(packageSearchArgs.SearchTerm, packageSearchArgs.Logger, packageSearchArgs.Verbosity, packageSearchArgs.ExactMatch, packageSearchArgs.ConsoleWidth);
4444
}
4545

4646
packageSearchResultRenderer.Start();

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageSearch/Table.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal class Column
2020
internal class Table
2121
{
2222
// This is the default window width if we cannot get the actual window width
23-
const int DefaultWindowWidth = 115;
23+
internal const int DefaultWindowWidth = 115;
2424
// This is the minimum number of characters in a column which includes "| c |" where c is a character
2525
const int MinimumCharactersInAColumn = 4;
2626
// This is the list of columns in the table
@@ -36,30 +36,16 @@ internal class Table
3636
// This is the default console color
3737
private readonly ConsoleColor _consoleColor = Console.ForegroundColor;
3838

39-
public Table(int[] columnsToHighlight, params string[] headers)
39+
public Table(int[] columnsToHighlight, string[] headers, int consoleWidth)
4040
{
41-
_columnsToHighlight = columnsToHighlight;
42-
int windowWidth = -1;
43-
44-
// Get the window width if possible
45-
try
41+
if (consoleWidth <= 0)
4642
{
47-
windowWidth = Console.WindowWidth;
48-
}
49-
catch (Exception)
50-
{
51-
// Ignore any exception
43+
throw new ArgumentOutOfRangeException(nameof(consoleWidth), consoleWidth, "Console width must be greater than zero.");
5244
}
5345

54-
// If the window width is not available, use the default window width
55-
if (windowWidth <= 0)
56-
{
57-
_maxColumnWidth = DefaultWindowWidth;
58-
}
59-
else
60-
{
61-
_maxColumnWidth = Math.Max(MinimumCharactersInAColumn, (windowWidth - MinimumCharactersInAColumn * headers.Length) / headers.Length);
62-
}
46+
_columnsToHighlight = columnsToHighlight;
47+
48+
_maxColumnWidth = Math.Max(MinimumCharactersInAColumn, (consoleWidth - MinimumCharactersInAColumn * headers.Length) / headers.Length);
6349

6450
// Add the headers
6551
foreach (var header in headers)

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageSearch/TableFormat/DetailedTableFormatStrategy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ internal class DetailedTableFormatStrategy : ITableFormatStrategy
1010
private readonly string[] _detailedVerbosityTableHeader = { "Package ID", "Latest Version", "Owners", "Total Downloads", "Vulnerable", "Deprecation", "Project URL", "Description" };
1111
private readonly int[] _detailedColumnsToHighlight = { 0, 2, 6, 7 };
1212

13-
public Table CreateTable()
13+
public Table CreateTable(int consoleWidth)
1414
{
15-
return new Table(_detailedColumnsToHighlight, _detailedVerbosityTableHeader);
15+
return new Table(_detailedColumnsToHighlight, _detailedVerbosityTableHeader, consoleWidth);
1616
}
1717
}
1818
}

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageSearch/TableFormat/ExactDetailedTableFormatStrategy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ internal class ExactDetailedTableFormatStrategy : ITableFormatStrategy
1010
private readonly string[] _detailedVerbosityTableHeaderForExactMatch = { "Package ID", "Version", "Owners", "Total Downloads", "Vulnerable", "Deprecation", "Project URL", "Description" };
1111
private readonly int[] _detailedColumnsToHighlight = { 0, 2, 6, 7 };
1212

13-
public Table CreateTable()
13+
public Table CreateTable(int consoleWidth)
1414
{
15-
return new Table(_detailedColumnsToHighlight, _detailedVerbosityTableHeaderForExactMatch);
15+
return new Table(_detailedColumnsToHighlight, _detailedVerbosityTableHeaderForExactMatch, consoleWidth);
1616
}
1717
}
1818
}

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageSearch/TableFormat/ExactMinimalTableFormatStrategy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ internal class ExactMinimalTableFormatStrategy : ITableFormatStrategy
1010
private readonly string[] _minimalVerbosityTableHeaderForExactMatch = { "Package ID", "Version" };
1111
private readonly int[] _minimalColumnsToHighlight = { 0 };
1212

13-
public Table CreateTable()
13+
public Table CreateTable(int consoleWidth)
1414
{
15-
return new Table(_minimalColumnsToHighlight, _minimalVerbosityTableHeaderForExactMatch);
15+
return new Table(_minimalColumnsToHighlight, _minimalVerbosityTableHeaderForExactMatch, consoleWidth);
1616
}
1717
}
1818
}

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageSearch/TableFormat/ExactNormalTableFormatStrategy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ internal class ExactNormalTableFormatStrategy : ITableFormatStrategy
1010
private readonly string[] _normalVerbosityTableHeaderForExactMatch = { "Package ID", "Version", "Owners", "Total Downloads" };
1111
private readonly int[] _normalColumnsToHighlight = { 0, 2 };
1212

13-
public Table CreateTable()
13+
public Table CreateTable(int consoleWidth)
1414
{
15-
return new Table(_normalColumnsToHighlight, _normalVerbosityTableHeaderForExactMatch);
15+
return new Table(_normalColumnsToHighlight, _normalVerbosityTableHeaderForExactMatch, consoleWidth);
1616
}
1717
}
1818
}

src/NuGet.Core/NuGet.CommandLine.XPlat/Commands/PackageSearch/TableFormat/ITableFormatStrategy.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@ namespace NuGet.CommandLine.XPlat
77
{
88
internal interface ITableFormatStrategy
99
{
10-
Table CreateTable();
10+
Table CreateTable(int consoleWidth);
1111
}
1212
}

0 commit comments

Comments
 (0)