Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AnsiConsole.Live‘s working results are inconsistent. #1688

Open
NMSAzulX opened this issue Nov 19, 2024 · 2 comments
Open

AnsiConsole.Live‘s working results are inconsistent. #1688

NMSAzulX opened this issue Nov 19, 2024 · 2 comments
Labels
bug Something isn't working needs triage

Comments

@NMSAzulX
Copy link

NMSAzulX commented Nov 19, 2024

Information

  • OS: Windows
  • Version: 0.49.1
  • Terminal: Console

Describe the bug
Data is a result set generated by concurrent logic.

data = GetConcurrentData();

It will keep fetching data until the token is cancelled.

while(!cancel.IsCancellationRequested)
{
    tempData = GetConcurrentData();
    data = tempData.Order().ToList();
}

Every time new data is fetched, the contents in the table rows will be cleared and re-rendered.

table.Rows.Clear();
table.AddRows();
ctx.Refresh();

The problem is: When Rows is empty, ctx.Refresh() can still render data. Why?

table.Rows.Clear();
ctx.Refresh();

The result of the above code is that the table renders four rows of data.

To Reproduce

//make a table 
AnsiConsole
.Live(table)
.Start(ctx=>{
while (!cancel.IsCancellationRequested)
{
    table.Rows.Clear();
    for (int i = 0; i < data.Count; i++)
    {
        var rowData= data[i];
        table.AddRow(
            new Markup($"[{"white"}]{rowData.Id}[/]"),
            new Markup($"[khaki3]{rowData.Value}[/]"));
    }
    Thread.Sleep(100);
    ctx.Refresh();
    data = GetConcurrentData().Order().ToList();
}
table.Rows.Clear();
 for (int i = 0; i < data.Count; i++)
    {
        var rowData= data[i];
        table.AddRow(
            new Markup($"[{"white"}]{rowData.Id}[/]"),
            new Markup($"[khaki3]{rowData.Value}[/]"));
    }
ctx.Refresh();
});

Expected behavior

Screenshots
If applicable, add screenshots to help explain your problem.
1732022888275

Additional context


@NMSAzulX NMSAzulX added bug Something isn't working needs triage labels Nov 19, 2024
@github-project-automation github-project-automation bot moved this to Todo 🕑 in Spectre Console Nov 19, 2024
@patriksvensson
Copy link
Contributor

@NMSAzulX You will need to provide a fully reproducable example for us to be able to determine what the problem is.

@NMSAzulX
Copy link
Author

@NMSAzulX You will need to provide a fully reproducable example for us to be able to determine what the problem is.

Console.OutputEncoding = System.Text.Encoding.UTF8;
Console.InputEncoding = System.Text.Encoding.UTF8;
Console.SetWindowSize(100, 33);
var table = new Table
{
    Border = TableBorder.Simple
};
table.AddColumn(new TableColumn("Index").Centered());
table.AddColumn(new TableColumn("Name").Centered());
table.AddColumn(new TableColumn("Path").LeftAligned());
table.Centered();

ConcurrentDictionary<int, int> data = [];
CancellationTokenSource cancellation = new();

var result =Task.Run(()=>Parallel.For(0, 50, (i) =>
{
    Thread.Sleep(100);
    data[i] = i;
}));

Task.Run(() =>
{
    while (!result.IsCompleted)
    {
        Thread.Sleep(200);
    }
    cancellation.Cancel();
});

string path = "g:\\1111111111.1111111111\\11111111111112.2111111111111\\3.3\\4,4\\5\\6\\7\\8.csproj";
AnsiConsole
           .Live(table)
           .AutoClear(false)
           .Start((ctx) => {

               var tempList = data.OrderBy(item => item.Value).ToArray();
               while (!cancellation.IsCancellationRequested)
               {
                   table.Rows.Clear();
                   for (int i = 0; i < tempList.Length; i++)
                   {
                       var pathText = new TextPath(path)
                       {
                           RootStyle = new Style(foreground: Color.LightSteelBlue3),
                           StemStyle = new Style(foreground: Color.MediumPurple3_1),
                           LeafStyle = new Style(foreground: Color.LightSteelBlue3),
                           SeparatorStyle = new Style(foreground: Color.LightSteelBlue3)
                       };
                       table.AddRow(
                           new Markup($"[chartreuse3_1] {tempList[i].Value}[/]"),
                           new Markup($"[chartreuse3_1] NNNNNNNNNNNNNaaaaaaaaaaaaaNNNNNNNNNNNNNNNName [/]"),
                           pathText
                           );
                   }
                   Thread.Sleep(100);
                   ctx.Refresh();
                   tempList = data.OrderBy(item => item.Value).ToArray();
               }
               table.Rows.Clear();
               for (int i = 0; i < tempList.Length; i++)
               {
                   var pathText = new TextPath(path)
                   {
                       RootStyle = new Style(foreground: Color.LightSteelBlue3),
                       StemStyle = new Style(foreground: Color.MediumPurple3_1),
                       LeafStyle = new Style(foreground: Color.LightSteelBlue3),
                       SeparatorStyle = new Style(foreground: Color.LightSteelBlue3)
                   };
                   table.AddRow(
                           new Markup($"[chartreuse3_1] {tempList[i].Value}[/]"),
                           new Markup($"[chartreuse3_1] NNNNNNNNNNNNNaaaaaaaaaaaaaNNNNNNNNNNNNNNNName [/]"),
                           pathText
                           );
               }
               ctx.Refresh();

           });

I reproduced this bug. It is caused by the file path.
string path = "g:\\1111111111.1111111111\\11111111111112.2111111111111\\3.3\\4,4\\5\\6\\7\\8.csproj";
It is particularly long.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
Status: Todo 🕑
Development

No branches or pull requests

2 participants