JanDeDobbeleer / oh-my-posh

The most customisable and low-latency cross platform/shell prompt renderer
https://ohmyposh.dev
MIT License
15.73k stars 2.28k forks source link

Refresh Git widgets periodically #5036

Open Darker21 opened 2 weeks ago

Darker21 commented 2 weeks ago

Code of Conduct

What would you like to see changed/added?

Was wondering if it would be possible to update the git widgets details automatically rather than having to enter a new line to refresh the state shown on the widgets (i.e. file changes, incoming changes, outgoing commits).

Not sure if this is 100% possible but would be a nice feature to have for people like myself using the terminal in IDE's such as VS Code who prefer the terminal to the extensions for git

JanDeDobbeleer commented 2 weeks ago

@Darker21 by automatically, do you mean on an interval? There's no way to reload only the git part, as the prompt is a string. We could however reload the prompt on an interval in case it wasn't reloaded on for a predefined timeout. This would only work for shells that have a way to reload the prompt, which is a few I believe. What shell do you use? I might be able to craft something that work together with you before adding that to our shell scripts and configuration.

Darker21 commented 2 weeks ago

An interval was what I had in mind, I'm using PowerShell within Windows Terminal. I could be missing some config if it already exists

JanDeDobbeleer commented 2 weeks ago

@Darker21 you could try adding this logic AFTER initialising oh-my-posh, this wasn't tested yet as I'm not on my laptop ATM.

# Create a timer object with a specific interval and a starttime
$timer = New-Object -Type Timers.Timer
$timer.Interval  = 3000 # milliseconds, in this case 3 seconds

# Register an event for every passed interval
Register-ObjectEvent -InputObject $timer  -EventName Elapsed  -SourceIdentifier "Timer.Elapsed" -Action {

    # reload the prompt using PSReadLine
    [Microsoft.PowerShell.PSConsoleReadLine]::InvokePrompt()

} | Out-Null

function Start-Timer {
    # stop the timer when it's running
    if ($timer.Enabled) {
        $timer.Stop()
    }

    # Start the timer
    $timer.Start()
}

New-Alias -Name 'Set-PoshContext' -Value 'Start-Timer' -Scope Global -Force
Darker21 commented 2 weeks ago

That's what I was looking for, it's working as expected, thanks for that

JanDeDobbeleer commented 2 weeks ago

@Darker21 I might add this natively so I'm leaving this open in the meantime. Or maybe the snippet helps someone do a contribution. It's a cool feature to have. No stale prompts.

Darker21 commented 2 weeks ago

I think it would be a good addition and could open up possibilities for other prompt options such as the Spotify one currently (#4732) open to 'refresh' and provide 'real time' updates

Kyren223 commented 2 weeks ago

@Darker21 I might add this natively so I'm leaving this open in the meantime. Or maybe the snippet helps someone do a contribution. It's a cool feature to have. No stale prompts.

Hello, I need this exact feature (be able to set a reload interval in ms), for a different purpose I was able to export an env var with my shell's vi-mode, but it doesn't update it, unless I press enter for a new command.

I am using zsh as my shell, I am not sure how to reload oh-my-posh, I tried zle reset-prompt and zle && zle reset-prompt but they don't seem to work.

Would love this as a feature, but in the meantime, if you can please provide me with a script/command to reload oh-my-posh, that'd be great