Skip to content

Layout issue when using a BarChart in the grid #7

@juchom

Description

@juchom

Hello @xoofx,

When using a BarChart inside the grid, there are issues with the layout (2/3, 1/3) is not respected, the BarChart max value ends outside the screen

You can try this sample.

using System.Diagnostics;
using System.Text;
using XenoAtom.Terminal;
using XenoAtom.Terminal.UI;
using XenoAtom.Terminal.UI.Controls;

var rnd = new Random();

// LogControl (left pane)
var log = new LogControl { MaxCapacity = 5000 }.WrapText(true);
log.AppendMarkupLine("[dim]Log started. Press Ctrl+Q to quit.[/]");

// Progress bars (right pane) — initialized with random values
var progressValues = Enumerable.Range(0, 5)
    .Select(_ => new State<double>(rnd.NextDouble()))
    .ToArray();

var progressLabels = new[] { "Download", "Compile", "Test", "Package", "Deploy" };

var progressStack = new VStack(new BarChart().Minimum(0.0)
                    .Maximum(1.0)
                    .ShowValues(false)
                    .ShowPercentages(true).Items(
                        progressLabels.Select((label, i) => new BarChartItem(label, 0).Value(progressValues[i].Bind.Value)))        
        ).Spacing(1);

// Layout: Grid with 2 columns (2/3 + 1/3)
var grid = new Grid()
    .Columns(
        new ColumnDefinition { Width = GridLength.Star(2), MinWidth = 0 },
        new ColumnDefinition { Width = GridLength.Star(1), MinWidth = 0 })
    .Rows(new RowDefinition { Height = GridLength.Star(1) })
    .Cell(new Group().TopLeftText("Log").Padding(1).Content(log.HorizontalAlignment(Align.Stretch)).Stretch(), 0, 0)
    .Cell(new Group().TopLeftText("Progress").Padding(1).Content(progressStack).Stretch(), 0, 1)
    .HorizontalAlignment(Align.Stretch)
    .VerticalAlignment(Align.Stretch);

// Timing for background task
var lastLogTick = Stopwatch.GetTimestamp();
var lastProgressTick = lastLogTick;

string RandomText()
{
    var len = rnd.Next(80, 201);
    var sb = new StringBuilder(len);
    for (var i = 0; i < len; i++)
    {
        // Insert a space every 5-12 chars so word-wrap can break the line
        var wordLen = rnd.Next(5, 13);
        if (i > 0 && i % wordLen == 0) sb.Append(' ');
        else sb.Append((char)rnd.Next('a', 'z' + 1));
    }
    return sb.ToString();
}

Terminal.Run(grid, () =>
{
    var now = Stopwatch.GetTimestamp();

    // Append a random log line every ~1s
    if (Stopwatch.GetElapsedTime(lastLogTick, now) >= TimeSpan.FromSeconds(1))
    {
        lastLogTick = now;
        log.AppendLine(RandomText());
    }

    // Drift progress bars every ~500ms for visual interest
    if (Stopwatch.GetElapsedTime(lastProgressTick, now) >= TimeSpan.FromMilliseconds(500))
    {
        lastProgressTick = now;
        foreach (var p in progressValues)
            p.Value = Math.Clamp(p.Value + (rnd.NextDouble() - 0.45) * 0.05, 0, 1);
    }

    return TerminalLoopResult.Continue;
});

Here is a screenshot of how it renders.

Image

Let me know if I can do something for you

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions