-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMicrosoft.PowerShell_profile.ps1
223 lines (171 loc) · 6.92 KB
/
Microsoft.PowerShell_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
219
220
221
222
223
$starttime = Get-Date
#region custom functions
#https://github.com/joonro/Get-ChildItem-Color.git
function Get-ChildItem-Color {
if ($Args[0] -eq $true) {
$ifwide = $true
if ($Args.Length -gt 1) {
$Args = $Args[1..($Args.length - 1)]
} else {
$Args = @()
}
} else {
$ifwide = $false
}
if (($Args[0] -eq "-a") -or ($Args[0] -eq "--all")) {
$Args[0] = "-Force"
}
$width = $host.UI.RawUI.WindowSize.Width
$items = Invoke-Expression "Get-ChildItem `"$Args`"";
$lnStr = $items | select-object Name | sort-object { "$_".length } -descending | select-object -first 1
$len = $lnStr.name.length
$cols = If ($len) {($width+1)/($len+2)} Else {1};
$cols = [math]::floor($cols);
if(!$cols){ $cols=1;}
$color_fore = $Host.UI.RawUI.ForegroundColor
$compressed_list = @(".7z", ".gz", ".rar", ".tar", ".zip")
$executable_list = @(".exe", ".bat", ".cmd", ".py", ".pl", ".ps1",
".psm1", ".vbs", ".rb", ".reg", ".fsx")
$dll_pdb_list = @(".dll", ".pdb")
$text_files_list = @(".csv", ".lg", "markdown", ".rst", ".txt")
$configs_list = @(".cfg", ".config", ".conf", ".ini")
$color_table = @{}
foreach ($Extension in $compressed_list) {
$color_table[$Extension] = "Yellow"
}
foreach ($Extension in $executable_list) {
$color_table[$Extension] = "Blue"
}
foreach ($Extension in $text_files_list) {
$color_table[$Extension] = "Cyan"
}
foreach ($Extension in $dll_pdb_list) {
$color_table[$Extension] = "Darkgreen"
}
foreach ($Extension in $configs_list) {
$color_table[$Extension] = "DarkYellow"
}
$i = 0
$pad = [math]::ceiling(($width+2) / $cols) - 3
$nnl = $false
$items |
%{
if ($_.GetType().Name -eq 'DirectoryInfo') {
$c = 'Green'
$length = ""
} else {
$c = $color_table[$_.Extension]
if ($c -eq $none) {
$c = $color_fore
}
$length = $_.length
}
# get the directory name
if ($_.GetType().Name -eq "FileInfo") {
$DirectoryName = $_.DirectoryName
} elseif ($_.GetType().Name -eq "DirectoryInfo") {
$DirectoryName = $_.Parent.FullName
}
if ($ifwide) { # Wide (ls)
if ($LastDirectoryName -ne $DirectoryName) { # change this to `$LastDirectoryName -ne $DirectoryName` to show DirectoryName
if($i -ne 0 -AND $host.ui.rawui.CursorPosition.X -ne 0){ # conditionally add an empty line
write-host ""
}
Write-Host -Fore $color_fore ("`n Directory: $DirectoryName`n")
}
$nnl = ++$i % $cols -ne 0
# truncate the item name
$towrite = $_.Name
if ($towrite.length -gt $pad) {
$towrite = $towrite.Substring(0, $pad - 3) + "..."
}
Write-Host ("{0,-$pad}" -f $towrite) -Fore $c -NoNewLine:$nnl
if($nnl){
write-host " " -NoNewLine
}
} else {
If ($LastDirectoryName -ne $DirectoryName) { # first item - print out the header
Write-Host "`n Directory: $DirectoryName`n"
Write-Host "Mode LastWriteTime Length Name"
Write-Host "---- ------------- ------ ----"
}
$Host.UI.RawUI.ForegroundColor = $c
Write-Host ("{0,-7} {1,25} {2,10} {3}" -f $_.mode,
([String]::Format("{0,10} {1,8}",
$_.LastWriteTime.ToString("d"),
$_.LastWriteTime.ToString("t"))),
$length, $_.name)
$Host.UI.RawUI.ForegroundColor = $color_fore
++$i # increase the counter
}
$LastDirectoryName = $DirectoryName
}
if ($nnl) { # conditionally add an empty line
Write-Host ""
}
}
function Get-ChildItem-Format-Wide {
$New_Args = @($true)
$New_Args += "$Args"
Invoke-Expression "Get-ChildItem-Color $New_Args"
}
#endregion custom functions
# Load posh-git example profile
#. 'C:\git-repos\posh-git\profile.example.ps1'
#Push-Location (Split-Path -Path $MyInvocation.MyCommand.Definition -Parent)
# Load posh-git module from current directory
Import-Module posh-git
# If module is installed in a default location ($env:PSModulePath),
# use this instead (see about_Modules for more information):
# Import-Module posh-git
function Test-Administrator {
$user = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $user).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}
# Set up a simple prompt, adding the git prompt parts inside git repos
function global:prompt {
$realLASTEXITCODE = $LASTEXITCODE
Write-Host
# Reset color, which can be messed up by Enable-GitColors
# $Host.UI.RawUI.ForegroundColor = $GitPromptSettings.DefaultForegroundColor
if (Test-Administrator) { # Use different username if elevated
Write-Host "(Elevated) " -NoNewline -ForegroundColor White
}
Write-Host "$ENV:USERNAME@" -NoNewline -ForegroundColor DarkYellow
Write-Host "$ENV:COMPUTERNAME" -NoNewline -ForegroundColor Magenta
if ($s -ne $null) { # color for PSSessions
Write-Host " (`$s: " -NoNewline -ForegroundColor DarkGray
Write-Host "$($s.Name)" -NoNewline -ForegroundColor Yellow
Write-Host ") " -NoNewline -ForegroundColor DarkGray
}
Write-Host " : " -NoNewline -ForegroundColor DarkGray
Write-Host $($(Get-Location) -replace ($env:USERPROFILE).Replace('\','\\'), "~") -NoNewline -ForegroundColor Blue
$global:LASTEXITCODE = $realLASTEXITCODE
Write-VcsStatus
return "> "
}
#Start ssh
Start-SshAgent -Quiet
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
#PSReadLine setup
#https://github.com/lzybkr/PSReadLine
Import-Module PSReadLine
Set-PSReadLineOption -HistoryNoDuplicates
Set-PSReadLineOption -HistorySearchCursorMovesToEnd
Set-PSReadLineOption -HistorySaveStyle SaveIncrementally
Set-PSReadLineOption -MaximumHistoryCount 4000
# history substring search
Set-PSReadlineKeyHandler -Key UpArrow -Function HistorySearchBackward
Set-PSReadlineKeyHandler -Key DownArrow -Function HistorySearchForward
# Tab completion
Set-PSReadlineKeyHandler -Chord 'Shift+Tab' -Function Complete
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
#Set aliases
Set-Alias ls Get-ChildItem-Color -option AllScope
Set-Alias l Get-ChildItem-Format-Wide -option AllScope
Set-Alias vi vim
Write-Host ("Loading personal profile took {0:N2} milliseconds" -f ((Get-Date) - $starttime).TotalMilliSeconds)