-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprompt.ps1
More file actions
154 lines (137 loc) · 4.41 KB
/
prompt.ps1
File metadata and controls
154 lines (137 loc) · 4.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
$Global:__poshGitInstalled = [bool](Get-Module -Name posh-git -ListAvailable -Verbose:$false)
function Global:prompt
{
$wd = (Get-Location).Path
$user = (whoami).Split('\')[-1]
$hostname = hostname
$ver = ([string]$PSVersionTable.PSVersion).Split('.')[0..1] -join '.'
$jobs = Get-Job
if ($ver -ge 7)
{$lastTime = Get-LastHistoryDuration}
# Extra versiony goodness:
if ($null -ne $PSVersionTable.PSVersion.Patch)
{
$ver += ".$($PSVersionTable.PSVersion.Patch)"
}
$admin = $false
if ($IsLinux -and $user -eq 'root')
{
$admin = $true
}
elseif (!$IsLinux)
{
$admin = [bool](([System.Security.Principal.WindowsIdentity]::GetCurrent()).groups -match "S-1-5-32-544")
}
$gitStatus = $Global:__poshGitInstalled -and (Get-GitStatus)
if ($wd -eq $HOME)
{
$wd = '~'
}
elseif ($wd.Split('\')[-1] -ne '' -and !$gitStatus)
{
$wd = $wd.Split('\')[-1]
}
elseif ($gitStatus -and $wd -like '*::*')
{
$wd = $wd.Split(':')[-1]
}
# History count:
Write-Host "$(((Get-History).Count + 1).ToString().PadLeft(3, '0')) " -NoNewline
# PS Version:
if ($PSVersionTable.PSVersion.PreReleaseLabel)
{
Write-Host "($($ver)-$($PSVersionTable.PSVersion.PreReleaseLabel)) " -ForegroundColor Red -NoNewline
}
else
{
Write-Host "($ver) " -ForegroundColor Magenta -NoNewline
}
# Timestamp:
Write-Host "[$(([string](Get-Date)).Split()[1])] " -ForegroundColor Yellow -NoNewline
# Last Execution Time:
if ($ver -ge 7 -and $lastTime)
{
Write-Host "<$($lastTime)> " -ForegroundColor Blue -NoNewline
}
# Job control:
if ($jobs)
{
$r = $jobs.where({$_.State -eq 'Running'}).count
$d = $jobs.where({$_.State -eq 'Completed' -and $_.HasMoreData}).count
$c = $jobs.where({$_.State -eq 'Completed' -and -not $_.HasMoreData}).count
$jobCounter = "jobs($($c)/$($jobs.Count))"
Write-Host '[' -NoNewline -ForegroundColor Yellow
if ($d) {Write-Host $jobCounter -NoNewline -ForegroundColor Red}
elseif ($c -eq $jobs.Count)
{Write-Host $jobCounter -NoNewline -ForegroundColor Green}
else {Write-Host $jobCounter -NoNewline -ForegroundColor Cyan}
Write-Host ' : ' -NoNewline -ForegroundColor Gray
# Running
Write-Host ">$r " -NoNewline -ForegroundColor Cyan
# HasData
Write-Host "!$d " -NoNewline -ForegroundColor Red
# Complete
Write-Host "`$$c" -NoNewline -ForegroundColor Green
Write-Host '] ' -NoNewline -ForegroundColor Yellow
}
if ($gitStatus)
{
Write-Host '[GIT] ' -ForegroundColor Green -NoNewline
}
else
{
if ($admin)
{
Write-Host "$($user)" -ForegroundColor Red -NoNewLine
}
else
{
Write-Host "$($user)" -ForegroundColor Green -NoNewLine
}
Write-Host " @ " -ForegroundColor Yellow -NoNewLine
Write-Host "$($hostname) " -ForegroundColor Magenta -NoNewLine
}
if ($admin)
{
$sym = '#'
}
else
{
$sym = '$'
}
Write-Host "$($wd) " -ForegroundColor Cyan -NoNewLine
if ($gitStatus)
{
Write-Host "`b" -NoNewline
Write-Host (Write-VcsStatus) -NoNewline
}
#>
Write-Host ''
Write-Host "$($sym)" -ForegroundColor Cyan -NoNewLine
return ' '
}
function Get-LastHistoryDuration
{
if ((Get-History).Count -gt 0)
{
$duration = (Get-History)[-1].Duration
# Use the int portion of TotalHours because we're not doing Days
$hours = ([decimal]$duration.TotalHours).ToString().Split('.')[0]
$hours = $hours.PadLeft(2, '0')
$minutes = $duration.Minutes.ToString().PadLeft(2, '0')
$seconds = $duration.Seconds.ToString().PadLeft(2, '0')
$milliseconds = $duration.Milliseconds.ToString().PadLeft(3, '0')
$output = "$($seconds).$($milliseconds)"
if ($minutes -ne '00')
{
$output = "$($minutes):$($output)"
}
if ($hours -ne '00')
{
$output = "$($hours):$($output)"
}
return $output
}
}
Invoke-Expression "function Reset-Prompt { . '$($MyInvocation.MyCommand.Path)' }"
New-Alias -Name 'rsp' -Value 'Reset-Prompt' -Force