Skip to content

Commit 1f5eb7e

Browse files
committed
An example showing a jobs to fetch background information
I actually use the WeatherJob as part of my regular prompt during the summer
1 parent b5b2af0 commit 1f5eb7e

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Source/Examples/BackgroundData.ps1

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#requires -module @{ModuleName='PowerLine';ModuleVersion='3.4.0'}
2+
3+
Get-Job -Name WeatherQuery -EA 0| Stop-Job -PassThru | Remove-Job
4+
$global:WeatherJob = Start-ThreadJob -Name WeatherQuery {
5+
while ($true) {
6+
Invoke-RestMethod "wttr.in?format=%c%t"
7+
Start-Sleep 300 # This job will update the weather every 5 minutes
8+
}
9+
}
10+
11+
Set-PowerLinePrompt -SetCurrentDirectory -PowerLineFont -Title {
12+
-join @(
13+
if (Test-Elevation) { "Admin: " }
14+
"PS" + $PSVersionTable.PSVersion.Major + " "
15+
Convert-Path $pwd
16+
)
17+
} -Colors @(
18+
"SteelBlue4", "DodgerBlue3", "DeepSkyBlue2", "SkyBlue2", "SteelBlue2", "LightSkyBlue1"
19+
) -Prompt @(
20+
# Consume the output of the job:
21+
# In this case, I only want the most recent output, so [-1]
22+
{ $WeatherJob.Output[-1] }
23+
{ "&Gear;" * $NestedPromptLevel }
24+
{ $pwd.Drive.Name }
25+
{ Split-Path $pwd -Leaf }
26+
{
27+
# In order for PSReadLine to work properly, it needs the $PromptText set to match the end of my prompt...
28+
$MyInvocation.HistoryId
29+
30+
# Because I don't have a "Write-PowerLineBlock" I am doing all this by hand:
31+
# Need to draw ">ID>" but the > each have to be FOREGROUND = the BACKGROUND of the previous block
32+
# AND the color changes depending on whether nestedPrompt rendered or not
33+
[string]$CS = [PoshCode.Pansies.Entities]::ExtendedCharacters["ColorSeparator"]
34+
$thisBg = $NestedPromptLevel ? $bg:SteelBlue2 : $bg:SkyBlue2
35+
$previousFg = $NestedPromptLevel ? $fg:SkyBlue2 : $fg:DeepSkyBlue2
36+
$thisFg = $NestedPromptLevel ? $fg:SteelBlue2 : $fg:SkyBlue2
37+
38+
Set-PSReadlineOption -PromptText @(
39+
($thisBg + $previousFg + $CS + $fg:white + $MyInvocation.HistoryId + $thisFg + $bg:clear + $CS)
40+
($bg:Gray44 + $previousFg + $CS + $fg:white + $MyInvocation.HistoryId + $fg:Gray44 + $bg:clear + $CS)
41+
)
42+
}
43+
{ "`t" }
44+
{ Get-Elapsed -Trim }
45+
{ Get-Date -Format "T" }
46+
)

0 commit comments

Comments
 (0)