-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprofile.ps1
218 lines (177 loc) · 7.12 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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using namespace System
using namespace System.IO
using namespace System.Security
using namespace System.Text
using namespace Microsoft.PowerShell
[CultureInfo]::CurrentCulture = [CultureInfo]::CreateSpecificCulture("en-US")
$PSDefaultParameterValues["*:Encoding"] = "utf8"
$PSStyle.Progress.View = "Classic"
$Host.PrivateData.ProgressBackgroundColor = "Cyan"
$Host.PrivateData.ProgressForegroundColor = "Yellow"
$ErrorView = "ConciseView"
$global:IsAdmin = if ($IsWindows) {
$CurrentUser = [Principal.WindowsPrincipal][Principal.WindowsIdentity]::GetCurrent()
$Administrator = [Principal.WindowsBuiltInRole]::Administrator
$CurrentUser.IsInRole($Administrator)
} elseif ($IsLinux) {
$(id -u) -eq 0
} elseif ($IsMacOS) {
$(sudo -n true 2>$null) -eq $true
} else {
$null
}
if ($IsWindows) {
$global:Natural = { [Regex]::Replace($_.Name, "\d+", { $Args[0].Value.PadLeft(20) }) }
}
#region Aliases
Set-Alias -Name ^ -Value Select-Object
if ($IsWindows) {
Set-Alias -Name man -Value Get-Help -Option AllScope
Set-Alias -Name touch -Value New-Item
Set-Alias -Name activate -Value ".\venv\Scripts\Activate.ps1"
Set-Alias -Name np -Value notepad.exe
Set-Alias -Name exp -Value explorer.exe
}
#endregion
#region PSReadLine Configuration
Set-PSReadLineOption -PredictionSource HistoryAndPlugin `
-PredictionViewStyle ListView `
-HistoryNoDuplicates `
-HistorySearchCursorMovesToEnd `
-ShowToolTips `
-EditMode Windows `
-BellStyle None
Set-PSReadLineKeyHandler -Key Tab -Function Complete
Set-PSReadLineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadLineKeyHandler -Key DownArrow -Function HistorySearchForward
Set-PSReadLineKeyHandler -Key Ctrl+u -Function RevertLine
Set-PSReadLineKeyHandler -Key Ctrl+s -BriefDescription SaveInHistory -LongDescription "Save current line in history without execution" -ScriptBlock {
param($Key, $Arg)
$Line = $null
$Cursor = $null
[PSConsoleReadLine]::GetBufferState([ref]$Line, [ref]$Cursor)
[PSConsoleReadLine]::AddToHistory($Line)
[PSConsoleReadLine]::RevertLine()
}
Set-PSReadLineKeyHandler -Key "(", "[", "{" -BriefDescription InsertPairedBraces -LongDescription "Insert matching braces" -ScriptBlock {
param($Key, $Arg)
$CloseChar = switch ($Key.KeyChar) {
<#case#> "(" { [char]")"; break }
<#case#> "[" { [char]"]"; break }
<#case#> "{" { [char]"}"; break }
}
$SelectionStart = $null
$SelectionLength = $null
[PSConsoleReadLine]::GetSelectionState([ref]$SelectionStart, [ref]$SelectionLength)
$Line = $null
$Cursor = $null
[PSConsoleReadLine]::GetBufferState([ref]$Line, [ref]$Cursor)
if ($SelectionStart -ne -1) {
[PSConsoleReadLine]::Replace($SelectionStart, $SelectionLength, $Key.KeyChar + $Line.SubString($SelectionStart, $SelectionLength) + $CloseChar)
[PSConsoleReadLine]::SetCursorPosition($SelectionStart + $SelectionLength + 2)
}
else {
[PSConsoleReadLine]::Insert("$($Key.KeyChar)$CloseChar")
[PSConsoleReadLine]::SetCursorPosition($Cursor + 1)
}
}
Set-PSReadLineKeyHandler -Key ")", "]", "}" -BriefDescription SmartClosingBraces -LongDescription "Insert closing brace or skip" -ScriptBlock {
param($Key, $Arg)
$Line = $null
$Cursor = $null
[PSConsoleReadLine]::GetBufferState([ref]$Line, [ref]$Cursor)
if ($Line[$Cursor] -and $Key.KeyChar) {
[PSConsoleReadLine]::SetCursorPosition($Cursor + 1)
}
else {
[PSConsoleReadLine]::Insert("$($Key.KeyChar)")
}
}
#endregion
#region Hook Scripts
if ($env:PROFILE_LOAD_CUSTOM_SCRIPTS -and $(Test-Path $env:PROFILE_LOAD_CUSTOM_SCRIPTS)) {
Get-ChildItem -Path $env:PROFILE_LOAD_CUSTOM_SCRIPTS -Filter "*.ps1" | ForEach-Object {
. $_.FullName
}
}
#endregion
#region Command Prompt
function Get-ExecutionTime {
[OutputType([TimeSpan])]
param()
process {
$History = Get-History
$ExecTime = $History ? ($History[-1].EndExecutionTime - $History[-1].StartExecutionTime) : (New-TimeSpan)
Write-Output $ExecTime
}
}
#region Automatically Executing Scripts
if (Get-Module PowerTools) {
Import-Module PowerTools
}
# Required by Python for custom virtual environment status indicator in prompt function
$env:VIRTUAL_ENV_DISABLE_PROMPT = 1
#endregion
function prompt {
$ExecTime = Get-ExecutionTime
$GitStatus = if ($(git rev-parse --is-inside-work-tree 2>&1) -eq $true) {
# tag branch detached head
$Head = (git tag --points-at HEAD) ?? (git branch --show-current) ?? (git rev-parse --short HEAD)
$DisplayUserName = $env:PROFILE_ENABLE_BRANCH_USERNAME -eq 1
# U @ H
[string]::Format(" {2}({0}{1}{2}{3}{4}{2}{5}){6}",
$PSStyle.Foreground.Cyan, # 0
$DisplayUserName ? (git config user.name) : [string]::Empty, # 1
$PSStyle.Foreground.Blue, # 2
$PSStyle.Foreground.BrightBlue, # 3
$DisplayUserName ? "@" : [string]::Empty, # 4
$Head, # 5
$PSStyle.Foreground.White # 6
)
}
$PythonVirtualEnvironment = if ($env:VIRTUAL_ENV) {
[string]::Format(" {0}({1}){2}",
$PSStyle.Foreground.Magenta,
[Path]::GetFileName($env:VIRTUAL_ENV),
$PSStyle.Foreground.White
)
}
$PsPrompt = [StringBuilder]::new()
$null = & {
# [username@hostname pwd]
$PsPrompt.Append("[")
$PsPrompt.Append($PSStyle.Foreground.BrightCyan)
$PsPrompt.Append([Environment]::UserName)
$PsPrompt.Append($PSStyle.Foreground.White)
$PsPrompt.Append("@")
$PsPrompt.Append([Environment]::MachineName)
$PsPrompt.Append(" ")
$PsPrompt.Append($PSStyle.Foreground.Green)
$PsPrompt.Append([DirectoryInfo]::new($ExecutionContext.SessionState.Path.CurrentLocation).BaseName)
$PsPrompt.Append($PSStyle.Foreground.White)
$PsPrompt.Append("]")
$PsPrompt.Append(" ")
# (HH:mm:ss:ms)
$PsPrompt.Append($PSStyle.Foreground.Yellow)
$PsPrompt.Append("(")
$PsPrompt.Append($ExecTime.Hours.ToString("D2"))
$PsPrompt.Append(":")
$PsPrompt.Append($ExecTime.Minutes.ToString("D2"))
$PsPrompt.Append(":")
$PsPrompt.Append($ExecTime.Seconds.ToString("D2"))
$PsPrompt.Append(":")
$PsPrompt.Append($ExecTime.Milliseconds.ToString("D2"))
$PsPrompt.Append(")")
$PsPrompt.Append($PSStyle.Foreground.White)
# (user@branch)
$PsPrompt.Append($GitStatus)
# (active)
$PsPrompt.Append($PythonVirtualEnvironment)
# #/>
$PsPrompt.Append([Environment]::NewLine)
$PsPrompt.Append([string]::new($global:IsAdmin ? "#" : ">", $NestedPromptLevel + 1))
$PsPrompt.Append(" ")
}
return $PsPrompt.ToString()
}
#endregion