PowerLine icon indicating copy to clipboard operation
PowerLine copied to clipboard

Is there a way to reference the current theme color within a prompt block?

Open TMA-2 opened this issue 1 year ago • 0 comments

For context, I may be over-thinking the use of PowerLine or using it in an odd way, as I have a long script called by my profile to set up my PowerLine configuration, as there are certain things I don't want called in every prompt, but which I know aren't saved to the configuration psd1 as they're outside of $Prompt or any Set-PowerLinePrompt parameter. For instance, it optionally checks for and installs the ThreadJobs module, then creates jobs to get the current process CPU and RAM usage every 5 minutes, only referencing the job output within the prompt block.

The one thing that has really tripped me up is combining a color palette with -Colors, but certain blocks have manually-created parts (i.e. adding a start/end cap character or a different section separator) which necessitate — so far as I've found — manual color referencing. For instance, the right side of my prompt has optional error info, the last command duration, and the date/time. There is a function _PLColor in the first prompt block that, when called, increments a variable each time it's called by a visible block, which is then used to reference the right color in the array... or at least that's the idea. Clearly it has issues, as seen here, where the end cap is a different color than it ought to be when the error block is shown:

PowerLine-PromptRight-malformed

# $script:PLFG is the current guess as to the block's theme color, and obviously `&ColorSeparatorRound;` is the round end cap.
New-PromptText "$ClockIcon$Spacing$(Get-Date -Format "HH:mm:ss")$script:PLFG${bg:Clear}&ColorSeparatorRound;"

Otherwise, it mostly displays normally with a few small oddities: PowerLine-PromptRight PowerLine-PromptLeft

There may be something I'm overlooking (likely, tbh), but if there was something like ${fg:CurrentForeground}, that would be amazing and I could delete this ridiculous function.

$global:Prompt = @(
{ # _PLColor definition
    $script:Index = 0 # Tracks the color by incrementing in visible script blocks
    $script:PLFG = $null # Explicit foreground color
    $script:PLBG = $null # Explicit background color
    $script:PLAuto = [RgbColor]::new() # The current color by $Index

    Function script:_PLColor {
        # Increments $script:Index when called, and returns the relevant color from the array passed to PowerLine ($ColorMap)
        # By default it sets $PLFG and $PLBG to `[RgbColor]`, with -RGB it sets them to the VT escape sequence.
        [CmdletBinding()]
        Param(
            [byte]$Idx = $script:Index,
            [switch]$RGB
        )
    
        $Idx++
        if($Idx -gt $script:ColorMap.GetUpperBound(0)) {
            $Idx = $script:Index = 0
        } else {
            $script:Index++
        }
    
        $script:PLAuto = $script:ColorMap[$script:Index] -as [RgbColor]
        if($RGB) {
            $script:PLFG = [RgbColor]::ConvertFrom($script:PLAuto)
            $script:PLBG = [RgbColor]::ConvertFrom($script:PLAuto)
        } else {
            $script:PLFG = ($script:PLAuto).ToVtEscapeSequence($false)
            $script:PLBG = ($script:PLAuto).ToVtEscapeSequence($true)
        }
    }
},
# ...other prompt blocks...
{ "`t" },
{ # Last error info
        if((Get-ErrorCount) -gt 0) {
            script:_PLColor -RGB
            $Err = $Error[0]
            $HResult = $Err.Exception.HResult
            New-PromptText ("&Error;$Spacing$HResult") -EBg 'DarkRed' -Bg $script:PLAuto
        }
},
{ # Last command duration
    script:_PLColor
    New-PromptText (Get-LastCommandDuration -Trim)
},
{ # Date/time
    script:_PLColor

    $ClockIcon = "&nf-weather-time_"+[datetime]::Now.ToString('%h')+";"
    New-PromptText "$ClockIcon$Spacing$(Get-Date -Format "HH:mm:ss")$script:PLFG${bg:Clear}&ColorSeparatorRound;"
}
# ...other prompt blocks...
)

TMA-2 avatar Jan 15 '25 20:01 TMA-2