-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.ps1
More file actions
171 lines (124 loc) · 4.79 KB
/
Copy pathprofile.ps1
File metadata and controls
171 lines (124 loc) · 4.79 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"output"
# write-output "output"
# write-host "Initing prompt..."
$PSVersionTable
$global:CurrentUser = [System.Security.Principal.WindowsIdentity]::GetCurrent();
$global:ComputerName = [System.Net.Dns]::GetHostName();
"1"
# extend path correctly...
# $env:path = "c:\cygwin\bin;$($env:path)"
$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\cmd"
# $env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"
"2"
# set term so "less" does not complain
$env:TERM="msys"
"3"
# idiotic shared home drive rubish, must make the directory trusted
Set-Alias CasPol "$([Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory())CasPol.exe"
CasPol -polchgprompt off -machine -addgroup 1.2 -url file://P:\WindowsPowerShell\Modules\* FullTrust | out-null
"4"
# hook up PsGet and PoshGit
# Import-Module PsGet # not needed, use chocolaty
# Import-Module Pscx # after installing with choco it's on the path and always available
# Import-Module Posh-Git
# custom aliases
new-alias -force which get-command
new-alias -force help get-help
new-alias -force gh get-help
# Set-Location c:\
function prompt() {
#colours
$gbhlColour3 = 'darkgreen';
$hlColour1 = 'blue';
$hlColour2 = 'magenta';
$hlColour3 = 'green';
$location = get-location;
$windowWidth = $host.UI.RawUI.WindowSize.Width;
# username
$indexOfSlash = $CurrentUser.Name.IndexOf('\');
if($indexOfSlash -gt 0)
{
$userName = $CurrentUser.Name.Substring($indexOfSlash + 1);
}
else
{
$userName = $CurrentUser.Name;
}
$userAtHost = [string]::Format("{0}@{1}", $userName, $ComputerName);
$path = $location.Path;
write-host -nonewline -f $gbhlColour3 "┌─┤";
write-host -nonewline -f $hlColour3 $userAtHost;
if((get-module Posh-Git)) {
# git status
Write-VcsStatus;
}
write-host -nonewline -f $gbhlColour3 "├";
$cusorXPos = $host.UI.RawUI.CursorPosition.x;
# write-host $windowWidth $cusorXPos
$modifiedPath = truncatePath $location.Path (($windowWidth - $cusorXPos) - 4);
#write-host $location.Path;
#write-host $modifiedPath;
$pathStringLen = $modifiedPath.Length + 3; # extra 3 for the line art
$paddingLineLength = $windowWidth - ($cusorXPos + $pathStringLen);
# There might not be enough space to put the path in here, check and shorten in
# bits until the path is not to long to fit, stick that in the path location in
# the prompt.
# Keep shortening, and stick the fully shorted location with last directory in the
# window title.
# $path = truncatePath($location.Path, )
# a bar to fill the gap
$bar = "─" * ($paddingLineLength - 1);
write-host -nonewline -f $gbhlColour3 $bar;
write-host -nonewline -f $gbhlColour3 "┤";
write-host -nonewline -f $hlColour2 ($modifiedPath);
write-host -f $gbhlColour3 "├┐";
write-host -nonewline -f $gbhlColour3 "└ ";
write-host -nonewline -f $hlColour1 "PS";
write-host -nonewline -f $gbhlColour3 ">";
return " "
}
function truncatePath([string]$pathString, [int]$length) {
$processedPath = "";
if($pathString.Length -gt $length) {
$chunks = $pathString.Trim(@("\")).Split("\");
# assuming first value is the drive, skip it
# don't ever process the last item in the list
for($i = 1; $i -lt $chunks.Length - 1; $i++) { # -or (($chunks -join "\").Length -le $length
$newValue = "";
$selection = select-string "^.|\.." -input $chunks[$i] -allmatches;
foreach($m in $selection.Matches) {
$newValue += $m.Value;
}
$chunks[$i] = $newValue;
if(($chunks -join "\").Length -le $length) { break };
}
# write-host $length;
# write-host ($chunks -join "\");
return ($chunks -join "\");
} else {
$pathString;
}
}
function Invoke-SshAgent([switch]$Quiet) {
$oldPath = $env:path;
$env:path += ";" + (Get-Item "Env:ProgramFiles(x86)").Value + "\Git\bin"
Start-SshAgent($Quiet);
$env:path = $oldPath;
}
function Invoke-WMSettingChange {
#requires -version 2
if (-not ("win32.nativemethods" -as [type])) {
# import sendmessagetimeout from win32
add-type -Namespace Win32 -Name NativeMethods -MemberDefinition '[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam, uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);'
}
$HWND_BROADCAST = [intptr]0xffff;
$WM_SETTINGCHANGE = 0x1a;
$result = [uintptr]::zero
# notify all windows of environment block change
[win32.nativemethods]::SendMessageTimeout($HWND_BROADCAST, $WM_SETTINGCHANGE, [uintptr]::Zero, "Environment", 2, 5000, [ref]$result);
}
# Import-Module Posh-Git
function ImportPoshGit(){
Import-Module Posh-Git
Invoke-SshAgent
}