-
Notifications
You must be signed in to change notification settings - Fork 3
/
profile.ps1
117 lines (100 loc) · 3.53 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
# Settings
$RemoteServer = ""
# Window Title
$Title = "PowerShell"
if ([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5-32-544') {
$Title = "Administrator: " + $Title
}
$Host.UI.RawUI.WindowTitle = $Title
# Shell Prompt
function prompt {
$path = $pwd -replace [regex]::escape($HOME), "~"
$path = $path -replace "\\", "/"
Write-Host "[" -ForegroundColor White -NoNewline
Write-Host "$([char]0xf17a) " -NoNewline
Write-Host "$path" -ForegroundColor DarkCyan -NoNewline
Write-Host "]" -ForegroundColor White -NoNewline
Write-Host "$ `e[5 q" -NoNewline
# Git Support
if (Get-Command git) {
if ($status = Get-GitStatus -Force) {
$global:GitPromptSettings.ShowStatusWhenZero = 0
Write-Host "`r[" -ForegroundColor White -NoNewline
Write-Host "$([char]0xf17a) " -NoNewline
Write-Host "$path" -ForegroundColor DarkCyan -NoNewline
Write-Host "|" -NoNewline
Write-Host "$($status.Branch)" -ForegroundColor (Get-GitBranchStatusColor $status).ForegroundColor -NoNewline
if ($status.BehindBy -ne 0 || $status.AheadBy -ne 0) {
Write-Host "$(Write-GitBranchStatus $status)" -NoNewline
}
Write-Host "$(Write-GitWorkingDirStatus $status)" -NoNewline
Write-Host "$(Write-GitIndexStatus $status)" -NoNewline
Write-Host "]" -ForegroundColor White -NoNewline
Write-Host "$ " -NoNewline
}
}
return "`e[5 q"
}
# Custom Functions
function Connect-RemoteServer () {
if ($RemoteServer) {
ssh.exe $RemoteServer
}
else {
Write-Error "Specify the `$RemoteServer variable in your profile!`n`t($($PROFILE.CurrentUserAllHosts))"
}
}
function Open-NewWindow () {
Start-Process pwsh.exe
}
function Edit-InVim ($file) {
& "$env:ProgramFiles/Neovim/bin/nvim.exe" $file
}
function Get-AllChildItems {
return Get-ChildItem @args -Force
}
function Format-Size([int64] $size) {
if ($size -lt 1024) { return "{0} B" -f ($size) }
if ($size -lt 1Mb) { return "{0:0.0} KB" -f ($size / 1Kb) }
if ($size -lt 1Gb) { return "{0:0.0} MB" -f ($size / 1Mb) }
return "{0:0.0} GB" -f ($size / 1Gb)
}
function Get-ChildSize($path = ".") {
Get-ChildItem $path |
ForEach-Object {
$file = $_.Name
Get-ChildItem -File -Recurse $_.FullName | Measure-Object -Property length -Sum |
Select-Object -Property @{Name = "Name"; Expression = { $file } },
@{Name = "Size"; Expression = { Format-Size($_.Sum) } },
@{Name = "Length"; Expression = { $_.Sum } }
}
}
function Invoke-AsAdmin {
if ([Security.Principal.WindowsIdentity]::GetCurrent().Groups -contains 'S-1-5-32-544') {
Invoke-Expression -Command "$args"
}
else {
Start-Process -Verb RunAs pwsh.exe -Args "-executionpolicy bypass -command Set-Location \`"$PWD\`"; $args; pause"
}
}
function Update-Path($path = $null) {
if ($null -eq $path) {
$entries = @()
$ENV:Path -Split ";" | ForEach-Object {
$entries += [Pscustomobject]@{Entries = $_ }
}
return $entries
}
else {
$ENV:Path = $path
}
}
Remove-Item alias:nv -Force
Set-Alias -Name nv -Value Edit-InVim
Set-Alias -Name du -Value Get-ChildSize
Set-Alias -Name path -Value Update-Path
Set-Alias -Name sudo -Value Invoke-AsAdmin
Set-Alias -Name open -Value Open-NewWindow
Set-Alias -Name la -Value Get-AllChildItems
Set-Alias -Name cs -Value Connect-RemoteServer
Import-Module PSColor