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

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

Open
TMA-2 opened this issue Jan 15, 2025 · 0 comments
Open

Comments

@TMA-2
Copy link

TMA-2 commented Jan 15, 2025

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...
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant