Skip to content

Commit df8aa87

Browse files
authored
feat: add scroll functionality for height-limited panels
Adds a new Scroll property to Panel widget that controls content behavior when exceeding height: - When true: shows most recent content (scrolling) - When false: truncates excess content (default behavior)
1 parent 11a320c commit df8aa87

1 file changed

Lines changed: 16 additions & 2 deletions

File tree

src/Spectre.Console/Widgets/Panel.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public sealed class Panel : Renderable, IHasBoxBorder, IHasBorder, IExpandable,
4545
/// </summary>
4646
public int? Height { get; set; }
4747

48+
/// <summary>
49+
/// Gets or sets a value indicating whether content should scroll when exceeding height.
50+
/// If true, shows most recent content; if false, truncates excess content.
51+
/// </summary>
52+
public bool Scroll { get; set; }
53+
4854
/// <summary>
4955
/// Gets or sets a value indicating whether or not the panel is inlined.
5056
/// </summary>
@@ -144,7 +150,15 @@ protected override IEnumerable<Segment> Render(RenderOptions options, int maxWid
144150

145151
// Split the child segments into lines.
146152
var childSegments = ((IRenderable)child).Render(options with { Height = height }, innerWidth);
147-
foreach (var (_, _, last, line) in Segment.SplitLines(childSegments, innerWidth, height).Enumerate())
153+
var allLines = Segment.SplitLines(childSegments, innerWidth, null).ToList();
154+
155+
// If scrolling is enabled and content exceeds height limit, keep only the last few lines
156+
if (Scroll && height.HasValue && allLines.Count > height.Value)
157+
{
158+
allLines = allLines.Skip(allLines.Count - height.Value).ToList();
159+
}
160+
161+
foreach (var (_, _, last, line) in allLines.Enumerate())
148162
{
149163
if (line.Count == 1 && line[0].IsWhiteSpace)
150164
{
@@ -231,4 +245,4 @@ private void AddTopBorder(
231245
result.Add(new Segment(border.GetPart(BoxBorderPart.TopRight), borderStyle));
232246
result.Add(Segment.LineBreak);
233247
}
234-
}
248+
}

0 commit comments

Comments
 (0)