-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprofile.ps1
91 lines (84 loc) · 3.31 KB
/
profile.ps1
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
function Prompt {
$CurrentUser = (Get-ChildItem Env:\USERNAME).Value
$Hostname = hostname
$CurrentPath = (Get-Location)
$CurrentDirName = "./" + (Split-Path -Path $CurrentPath -Leaf)
# ($CurrentPath.ToString() -Split {$_ -eq "/" -or $_ -eq "\"})[-1]
$GitFolder = Join-Path -Path $CurrentPath -ChildPath ".git"
$LastCommand = Get-History -Count 1
if ($lastCommand) { $RunTime = ($lastCommand.EndExecutionTime - $lastCommand.StartExecutionTime).TotalSeconds }
if ($RunTime -ge 60) {
$ts = [timespan]::fromseconds($RunTime)
$min, $sec = ($ts.ToString("mm\:ss")).Split(":")
$ElapsedTime = -join ($min, " min ", $sec, " sec")
}
else {
$ElapsedTime = [math]::Round(($RunTime), 2)
$ElapsedTime = -join (($ElapsedTime.ToString()), " sec")
}
if (Test-Path $GitFolder) {
$CurrentBranchExt = $((git branch) -match "\*");
if ($CurrentBranchExt) {
Try {
$Branch = git branch
# Holds the pattern for extracting the branch name
$CurrentBranchMatchPattern = "\w*";
# Executes the regular expression against the matched branch
$CurrentBranchNameMatches = [regex]::matches($Branch, $CurrentBranchMatchPattern);
# Gets the current branch from the matches
$CurrentBranchName = $CurrentBranchNameMatches.Captures[2].Value.Trim();
# Sets the Prompt which contains the Current git branch name
# Prompt format - current_directory [ current_branch ] >
$GitPrompt = "($CurrentBranchName)"
}
Catch {
# Default prompt
$GitPrompt = ""
}
} else {
# Default prompt
$GitPrompt = ""
}
}
else {$GitPrompt = ""}
if ($IsWindows -or $env:OS) {
$IsAdmin = (New-Object Security.Principal.WindowsPrincipal `
([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
Write-Host ($(if ($IsAdmin) { 'Elevated ' } else { '' })) `
-NoNewLine `
-ForegroundColor White `
-BackgroundColor DarkRed
}
elseif ($IsLinux -or $IsMacOS) {
Write-Host "Sudo " `
-NoNewLine `
-ForegroundColor White `
-BackgroundColor DarkRed
}
Write-Host " $CurrentUser@$Hostname " `
-NoNewLine `
-ForegroundColor White `
-BackgroundColor DarkBlue
Write-Host $CurrentDirName `
-NoNewLine `
-ForegroundColor White `
-BackgroundColor Black
Write-Host " $GitPrompt " `
-NoNewLine `
-ForegroundColor Yellow `
-BackgroundColor Black
Write-Host "[$ElapsedTime] " `
-NoNewLine `
-ForegroundColor Green `
-BackgroundColor Black
Write-Host (Get-History).Count `
-NoNewLine `
-ForegroundColor White `
-BackgroundColor Black
Write-Host " >>>" `
-NoNewLine `
-ForegroundColor Yellow `
-BackgroundColor Black
$host.UI.RawUI.WindowTitle = "Current Dir: $((Get-Location).Path)"
Return " "
}