mirror of
				https://github.com/nsnail/spectre.console.git
				synced 2025-10-31 09:09:25 +08:00 
			
		
		
		
	Add support for adding empty rows
This affects grids and tables.
This commit is contained in:
		 Patrik Svensson
					Patrik Svensson
				
			
				
					committed by
					
						 Patrik Svensson
						Patrik Svensson
					
				
			
			
				
	
			
			
			 Patrik Svensson
						Patrik Svensson
					
				
			
						parent
						
							5d132220ba
						
					
				
				
					commit
					1d74fb909c
				
			| @@ -6,6 +6,25 @@ namespace Spectre.Console.Tests.Unit.Composition | ||||
| { | ||||
|     public sealed class GridTests | ||||
|     { | ||||
|         public sealed class TheAddColumnMethod | ||||
|         { | ||||
|             [Fact] | ||||
|             public void Should_Throw_If_Rows_Are_Not_Empty() | ||||
|             { | ||||
|                 // Given | ||||
|                 var grid = new Grid(); | ||||
|                 grid.AddColumn(); | ||||
|                 grid.AddRow("Hello World!"); | ||||
|  | ||||
|                 // When | ||||
|                 var result = Record.Exception(() => grid.AddColumn()); | ||||
|  | ||||
|                 // Then | ||||
|                 result.ShouldBeOfType<InvalidOperationException>() | ||||
|                     .Message.ShouldBe("Cannot add new columns to grid with existing rows."); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public sealed class TheAddRowMethod | ||||
|         { | ||||
|             [Fact] | ||||
| @@ -54,6 +73,30 @@ namespace Spectre.Console.Tests.Unit.Composition | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public sealed class TheAddEmptyRowMethod | ||||
|         { | ||||
|             [Fact] | ||||
|             public void Should_Add_Empty_Row() | ||||
|             { | ||||
|                 // Given | ||||
|                 var console = new PlainConsole(width: 80); | ||||
|                 var grid = new Grid(); | ||||
|                 grid.AddColumns(2); | ||||
|                 grid.AddRow("Foo", "Bar"); | ||||
|                 grid.AddEmptyRow(); | ||||
|                 grid.AddRow("Qux", "Corgi"); | ||||
|  | ||||
|                 // When | ||||
|                 console.Render(grid); | ||||
|  | ||||
|                 // Then | ||||
|                 console.Lines.Count.ShouldBe(3); | ||||
|                 console.Lines[0].ShouldBe("Foo  Bar  "); | ||||
|                 console.Lines[1].ShouldBe("          "); | ||||
|                 console.Lines[2].ShouldBe("Qux  Corgi"); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         [Fact] | ||||
|         public void Should_Render_Grid_Correctly() | ||||
|         { | ||||
|   | ||||
| @@ -21,6 +21,22 @@ namespace Spectre.Console.Tests.Unit.Composition | ||||
|                 result.ShouldBeOfType<ArgumentNullException>() | ||||
|                     .ParamName.ShouldBe("column"); | ||||
|             } | ||||
|  | ||||
|             [Fact] | ||||
|             public void Should_Throw_If_Rows_Are_Not_Empty() | ||||
|             { | ||||
|                 // Given | ||||
|                 var grid = new Table(); | ||||
|                 grid.AddColumn("Foo"); | ||||
|                 grid.AddRow("Hello World"); | ||||
|  | ||||
|                 // When | ||||
|                 var result = Record.Exception(() => grid.AddColumn("Bar")); | ||||
|  | ||||
|                 // Then | ||||
|                 result.ShouldBeOfType<InvalidOperationException>() | ||||
|                     .Message.ShouldBe("Cannot add new columns to table with existing rows."); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public sealed class TheAddColumnsMethod | ||||
| @@ -88,6 +104,34 @@ namespace Spectre.Console.Tests.Unit.Composition | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public sealed class TheAddEmptyRowMethod | ||||
|         { | ||||
|             [Fact] | ||||
|             public void Should_Render_Table_Correctly() | ||||
|             { | ||||
|                 // Given | ||||
|                 var console = new PlainConsole(width: 80); | ||||
|                 var table = new Table(); | ||||
|                 table.AddColumns("Foo", "Bar", "Baz"); | ||||
|                 table.AddRow("Qux", "Corgi", "Waldo"); | ||||
|                 table.AddEmptyRow(); | ||||
|                 table.AddRow("Grault", "Garply", "Fred"); | ||||
|  | ||||
|                 // When | ||||
|                 console.Render(table); | ||||
|  | ||||
|                 // Then | ||||
|                 console.Lines.Count.ShouldBe(7); | ||||
|                 console.Lines[0].ShouldBe("┌────────┬────────┬───────┐"); | ||||
|                 console.Lines[1].ShouldBe("│ Foo    │ Bar    │ Baz   │"); | ||||
|                 console.Lines[2].ShouldBe("├────────┼────────┼───────┤"); | ||||
|                 console.Lines[3].ShouldBe("│ Qux    │ Corgi  │ Waldo │"); | ||||
|                 console.Lines[4].ShouldBe("│        │        │       │"); | ||||
|                 console.Lines[5].ShouldBe("│ Grault │ Garply │ Fred  │"); | ||||
|                 console.Lines[6].ShouldBe("└────────┴────────┴───────┘"); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         [Fact] | ||||
|         public void Should_Render_Table_Correctly() | ||||
|         { | ||||
|   | ||||
| @@ -1,6 +1,8 @@ | ||||
| using System; | ||||
| using System.Collections.Generic; | ||||
| using System.Linq; | ||||
| using Spectre.Console.Composition; | ||||
| using Spectre.Console.Internal; | ||||
|  | ||||
| namespace Spectre.Console | ||||
| { | ||||
| @@ -56,6 +58,11 @@ namespace Spectre.Console | ||||
|                 throw new ArgumentNullException(nameof(column)); | ||||
|             } | ||||
|  | ||||
|             if (_table.RowCount > 0) | ||||
|             { | ||||
|                 throw new InvalidOperationException("Cannot add new columns to grid with existing rows."); | ||||
|             } | ||||
|  | ||||
|             // Only pad the most right cell if we've explicitly set a padding. | ||||
|             _table.PadRightCell = column.Padding != null; | ||||
|  | ||||
| @@ -97,6 +104,16 @@ namespace Spectre.Console | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Adds an empty row to the grid. | ||||
|         /// </summary> | ||||
|         public void AddEmptyRow() | ||||
|         { | ||||
|             var columns = new string[_table.ColumnCount]; | ||||
|             Enumerable.Range(0, _table.ColumnCount).ForEach(index => columns[index] = string.Empty); | ||||
|             AddRow(columns); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Adds a new row to the grid. | ||||
|         /// </summary> | ||||
|   | ||||
| @@ -81,7 +81,7 @@ namespace Spectre.Console | ||||
|                 throw new ArgumentNullException(nameof(column)); | ||||
|             } | ||||
|  | ||||
|             _columns.Add(new TableColumn(column)); | ||||
|             AddColumn(new TableColumn(column)); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
| @@ -95,6 +95,11 @@ namespace Spectre.Console | ||||
|                 throw new ArgumentNullException(nameof(column)); | ||||
|             } | ||||
|  | ||||
|             if (_rows.Count > 0) | ||||
|             { | ||||
|                 throw new InvalidOperationException("Cannot add new columns to table with existing rows."); | ||||
|             } | ||||
|  | ||||
|             _columns.Add(column); | ||||
|         } | ||||
|  | ||||
| @@ -109,7 +114,10 @@ namespace Spectre.Console | ||||
|                 throw new ArgumentNullException(nameof(columns)); | ||||
|             } | ||||
|  | ||||
|             _columns.AddRange(columns.Select(column => new TableColumn(column))); | ||||
|             foreach (var column in columns) | ||||
|             { | ||||
|                 AddColumn(column); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
| @@ -123,7 +131,20 @@ namespace Spectre.Console | ||||
|                 throw new ArgumentNullException(nameof(columns)); | ||||
|             } | ||||
|  | ||||
|             _columns.AddRange(columns.Select(column => column)); | ||||
|             foreach (var column in columns) | ||||
|             { | ||||
|                 AddColumn(column); | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Adds an empty row to the table. | ||||
|         /// </summary> | ||||
|         public void AddEmptyRow() | ||||
|         { | ||||
|             var columns = new string[ColumnCount]; | ||||
|             Enumerable.Range(0, ColumnCount).ForEach(index => columns[index] = string.Empty); | ||||
|             AddRow(columns); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|   | ||||
		Reference in New Issue
	
	Block a user