ShaunLawrie / PwshSpectreConsole

👻 PwshSpectreConsole is a PowerShell wrapper for the awesome Spectre.Console library
https://pwshspectreconsole.com/
MIT License
117 stars 7 forks source link

Spectre.Console Live capabilities #41

Closed IvanDrag0 closed 3 months ago

IvanDrag0 commented 3 months ago

Does the library support the Spectre.Console Live capabilities?

Is it possible to display a table and add rows to it as you're performing a function/task?

ShaunLawrie commented 3 months ago

I'm in the middle of doing a big update to add the missing Spectre.Console features. Live rendering and a bunch of the other features like layouts and embedding widgets inside other widgets requires quite a bit of change in this module.

So I'm working on it at the moment but it's a couple of weeks away most likely. Live rendering in particular I think will be a bit tricky to get right.

If you're interested in trying something in the meantime you can put the data you want into a table and do a loop like:

function Write-TableData {
  param ($tableData)
  Clear-Host
  $tableData | Format-SpectreTable
}

$tableData = @(
  @{ Title = "Table Data"; Description = "Other data" }
)

# Simulate doing some work and updating the table
for ($i = 0; $i -lt 5; $i++) {
  Write-TableData $myTable
  Start-Sleep -Seconds 1
  $myTable += @{ Title = "Table Data $i"; Description = "Other data $i" }
}

Which will give you a basic "updating table" but it does flicker a bit.

https://github.com/user-attachments/assets/f1b1f398-7dcf-4eb6-8874-0c21e4b45868

If you really want to hide the flicker you can use something like this to move the cursor around to redraw with less flickering:

function Write-TableData {
  param ($tableData)
  [Console]::SetCursorPosition(0, 0) # <- move the cursor to the top left of the console
  $tableData | Format-SpectreTable
}

$myTable = @(
  @{ Title = "Table Data"; Description = "Other data" }
)

Clear-Host # <- clear the console host once to hide all this code

# Hide the cursor
[Console]::CursorVisible = $false

# Render the table in a loop
for ($i = 0; $i -lt 5; $i++) {
  Write-TableData $myTable
  Start-Sleep -Seconds 1
  $myTable += @{ Title = "Table Data $i"; Description = "Other data $i" }
}

# Show the cursor again
[Console]::CursorVisible = $true

https://github.com/user-attachments/assets/0cb45582-2864-417f-a949-14655b5ef2ee

ShaunLawrie commented 3 months ago

Actually it's not going to be too hard to add to the library 😄! So this will definitely be in the next release in the coming week.

If you can't wait, you can use the Spectre.Console live functionality by directly calling the [Spectre.Console.AnsiConsole]::Live() function like this in powershell:

$table = [Spectre.Console.Table]::new()

[Spectre.Console.AnsiConsole]::Live($table).Start({
  param($ctx)

  $table = $table.AddColumn("Column 1")
  $ctx.Refresh()
  Start-Sleep -Seconds 1

  $table = $table.AddColumn("Column 2")
  $ctx.Refresh()
  Start-Sleep -Seconds 1

  for ($i = 0; $i -lt 5; $i++) {
    Start-Sleep -Seconds 1
    $table = [Spectre.Console.TableExtensions]::AddRow($table, "Row $i", "Value $i")
    $ctx.Refresh()
  }
})

https://github.com/user-attachments/assets/1f5cd582-1dcf-4ee2-a5b6-354440b0f179

IvanDrag0 commented 3 months ago

That is awesome!! Thank you!!

ShaunLawrie commented 3 months ago

FYI this stuff is in prerelease now https://prerelease.pwshspectreconsole.com/guides/upgrading-to-2-0/