From 20a2f727f724032880c7932ee5e6d7af3e162c81 Mon Sep 17 00:00:00 2001 From: BlazeFace Date: Tue, 9 Apr 2024 06:03:45 -0700 Subject: [PATCH] Updating test and Ratio calculation --- src/Spectre.Console/Internal/Ratio.cs | 15 +++++++-- src/Spectre.Console/Rendering/Segment.cs | 6 ++++ .../Unit/Widgets/LayoutTests.cs | 33 +++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/Spectre.Console/Internal/Ratio.cs b/src/Spectre.Console/Internal/Ratio.cs index fb38bdd..80059fc 100644 --- a/src/Spectre.Console/Internal/Ratio.cs +++ b/src/Spectre.Console/Internal/Ratio.cs @@ -9,7 +9,17 @@ internal static class Ratio { static (int Div, float Mod) DivMod(float x, float y) { - return ((int)(x / y), x % y); + var (div, mod) = ((int)(x / y), x % y); + + // If remainder is withing .0001 of 1 then we round up + if (!(mod > 0.9999)) + { + return (div, mod); + } + + div++; + mod = 0; + return (div, mod); } static int? GetEdgeWidth(IRatioResolvable edge) @@ -46,7 +56,8 @@ internal static class Ratio .ToList(); } - var portion = (float)remaining / flexibleEdges.Sum(x => Math.Max(1, x.Edge.Ratio)); + var r = flexibleEdges.Sum(x => Math.Max(1, x.Edge.Ratio)); + var portion = (float)remaining / r; var invalidate = false; foreach (var (index, size, edge) in flexibleEdges) diff --git a/src/Spectre.Console/Rendering/Segment.cs b/src/Spectre.Console/Rendering/Segment.cs index 9f36a77..6b11ca6 100644 --- a/src/Spectre.Console/Rendering/Segment.cs +++ b/src/Spectre.Console/Rendering/Segment.cs @@ -463,8 +463,14 @@ public class Segment var result = new List(); var segmentBuilder = (SegmentBuilder?)null; + var c = 0; foreach (var segment in segments) { + if (c == 163) + { + System.Console.Write("test"); + } + c++; if (segmentBuilder == null) { segmentBuilder = new SegmentBuilder(segment); diff --git a/test/Spectre.Console.Tests/Unit/Widgets/LayoutTests.cs b/test/Spectre.Console.Tests/Unit/Widgets/LayoutTests.cs index 550922a..b73cb67 100644 --- a/test/Spectre.Console.Tests/Unit/Widgets/LayoutTests.cs +++ b/test/Spectre.Console.Tests/Unit/Widgets/LayoutTests.cs @@ -264,4 +264,37 @@ public sealed class LayoutTests // Then return Verifier.Verify(console.Output); } + + [Fact] + [Expectation("Render_Layout_With_Three_And_One_Columns")] + public Task Should_Render_Layout_With_Three_And_One_Columns() + { + // Given + var console = new TestConsole().Size(new Size(40, 17)); + var layout = new Layout(); + var col1 = new Layout(); + var col1Row1 = new Layout(); + var col1Row2 = new Layout(); + var col1Row3 = new Layout(); + + col1.SplitRows(col1Row1, col1Row2, col1Row3); + + var col2 = new Layout(); + + layout.SplitColumns(col1, col2); + + var panel = new Panel("Hello, World!") { Expand = true }; + + List layouts = [col1Row1, col1Row2, col1Row3, col2]; + foreach (var l in layouts) + { + l.Update(panel); + } + + // When + console.Write(layout); + + // Then + return Verifier.Verify(console.Output); + } }