PwshSpectreConsole icon indicating copy to clipboard operation
PwshSpectreConsole copied to clipboard

Fix Write-SpectreRule width display in documentation examples

Open Copilot opened this issue 9 months ago • 0 comments

Issue

Write-SpectreRule examples in the documentation site were not displaying rules with the correct width. While using the module locally shows the rule with the width specified (either fixed or percentage-based), the documentation examples always showed rules at full width:

This affected both fixed width rules:

Write-SpectreRule -Title "Fixed Width Rule" -Width 40

And percentage-based width rules:

Write-SpectreRule -Title "Half Width Rule" -WidthPercent 50 -Alignment Center

Cause

The issue was in the Write-AnsiConsoleWithWidth function. During recording mode (which is used to generate the examples for the documentation site), the function was bypassing the width setting logic and directly writing the object without respecting the requested width:

if ($script:SpectreRecordingType) {
    [Spectre.Console.AnsiConsole]::Write($RenderableObject)
    return
}

Fix

I've modified the Write-AnsiConsoleWithWidth function to handle width settings during recording mode. The fix uses a defensive approach that:

  1. Checks if the recording console supports setting the width property
  2. Only attempts to set the width if it's possible
  3. Properly restores the original width after rendering
  4. Uses proper error handling to ensure the code works in different environments
if ($script:SpectreRecordingType) {
    # For recording, we'll try to set the width if possible
    $recordingConsole = [Spectre.Console.AnsiConsole]::Console
    
    # Try to get the profile width property safely
    $originalWidth = $null
    $canSetWidth = $false
    
    try {
        # Check if the console has a Profile.Width property
        if ($null -ne $recordingConsole.Profile -and 
            ($recordingConsole.Profile | Get-Member -Name 'Width' -MemberType Property)) {
            $originalWidth = $recordingConsole.Profile.Width
            $canSetWidth = $true
        }
    }
    catch {
        # If any error occurs, we'll just proceed without setting the width
        Write-Verbose "Unable to access Profile.Width property on recording console: $_"
    }
    
    try {
        # Set width if possible
        if ($canSetWidth) {
            $recordingConsole.Profile.Width = $MaxWidth
        }
        
        # Render the object
        [Spectre.Console.AnsiConsole]::Write($RenderableObject)
    }
    finally {
        # Restore original width if we changed it
        if ($canSetWidth -and $null -ne $originalWidth) {
            try {
                $recordingConsole.Profile.Width = $originalWidth
            }
            catch {
                Write-Verbose "Unable to restore Profile.Width on recording console: $_"
            }
        }
    }
    
    return
}

This ensures that rules with specific widths or percentage widths display correctly in the documentation examples, just as they do when used locally in a terminal.

Fixes #115.

[!WARNING]

Firewall rules blocked me from connecting to one or more addresses

I tried to connect to the following addresses, but was blocked by firewall rules:

  • cdn.fwupd.org
    • Triggering command: /usr/bin/fwupdmgr refresh (dns block)

If you need me to access, download, or install something from one of these locations, you can either:


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot avatar May 27 '25 11:05 Copilot