-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPowershellrc.ps1
354 lines (290 loc) · 12 KB
/
Powershellrc.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# Tim Holt's Powershell profile.
# Created in 2011 and heavily modified continuosly since then.
#
# This file is full of ‘*’ command ‘links’ __in_this_format. Maybe.
#
# Free to use for anyone
#│ ▼1 │ To-Do's
#└────┴─────────
# TODO Protect the Wide-Use Variables from editing!
# TODO Put machine-dependant variables into the local machine's profile,
# including paths to installed programs (with 'if' checks on whether
# something is defined before messing with). Don't forget to put template
# code in this file's comments!
#│ ▼1 │ Set-Up
#└─┬──┴─┬──────
#│ ▼2 │ Prompt Options
#└─┬──┴─┬──────────────
#│ ▼3 │ Various
#└────┴─────────
Set-PSReadlineOption -EditMode Vi
#│ ▼3 │ posh-git
#└────┴──────────
# NEVER install posh-git with chocolatey! It's fundamentally broken, and
# the posh-git devs see absolutely nothing wrong with this.
# Instead, use:
# PowerShellGet\Install-Module posh-git -Scope AllUsers -AllowPrerelease -Force
Import-Module posh-git
#│ ▼2 │ Code Page-ness
#└────┴────────────────
# From https://www.reddit.com/r/PowerShell/comments/3lym4q/cvalfcx
function Set-CodePage
{
[CmdletBinding()]
param( [ValidateSet("UTF8", "Default")] [string]$CodePage )
$codePageToNum = @{ UTF8 = 65001; Default = 437; }
chcp $codePageToNum[$CodePage] | Out-Null
}
if(Test-Path Env:ConEmuBuild)
{
Set-CodePage UTF8
}
#│ ▼2 │ Wide-Use Variables
#└────┴────────────────────
$progfilesx86 = ${env:ProgramFiles(x86)}
$progfiles_manual = "C:\Program Files (Manually Installed)\"
$all_progfiles = @($env:ProgramW6432, $progfilesx86, $progfiles_manual)
$zzd = "~/local_code/zigzagdungeon/"
$dropbox = "~/Dropbox/"
$tempmine = "~/temp/"
$inh = "~/Dropbox/timwrite/Book2/2016/inhuman_universe/"
$minecraft_hidden = "C:\Users\Tim\AppData\Roaming\.minecraft"
$my_scripts = "~\Dropbox\PowerShellScripts\"
# Utility variables:
$cpp_files = @("*.h", "*.cpp", "*.hpp")
$vim_runtime = "~/Dropbox/Vim/GlobalRuntimePath/"
# Resultant files of the "hg merge [branch] -t internal:dump" command.
$dump_files = @("*.base", "*.local", "*.orig", "*.other")
# cmdlet default parameters
$ls_x = @{"Exclude" = ".*"}
# small, shallow waf project automatic setup.
$qproj = "C:/Users/Tim/Dropbox/PythonScripts/qproj.py"
#│ ▼2 │ Path Augmentation
#└────┴───────────────────
# Note, this method of path augmentation goes away when powershell closes!
# TODO: Put these in a list, log the ones that don’t exist! (and check & log already present)
#if ($on_desktop)
#{
# #$env:path = $env:path + ";C:\Program Files (x86)\Vim\vim74"
# $env:path = $env:path + ";C:\Program Files (Manually Installed)\doxys_1_15_win_bin"
# $env:path = $env:path + ";C:\MinGW\bin"
# $env:path = $env:path + ";C:\Program Files\MiKTeX 2.9\miktex\bin\x64"
#}
#elseif ($on_laptop)
#{
# $env:path = $env:path + ";C:\Program Files\Vim\vim73\"
# $env:path = $env:path + ";C:\Program Files\TortoiseHg"
# $env:path = $env:path + ";C:\Program Files\SlikSvn\bin\"
# $env:path = $env:path + ";C:\MinGW\bin"
#}
#if ( $on_any_machine )
#{
$env:path = $env:path + ";" + $HOME + "\Dropbox\PowerShellScripts\"
$env:path = $env:path + ";C:\Program Files\7-Zip\"
# # $env:path = $env:path + ";" + $progfiles_manual + "doxys\"
#}
#│ ▼1 │ Utility
#└─┬──┴─┬───────
#│ ▼2 │ Mappings (Key Bindings)
#└────┴─────────────────────────
# I was *wondering* why my leave-term-mode mapping in NeoVim wasn't working.
# ...except this solved nothing. Powershell is a piece of crap.
Remove-PSReadLineKeyHandler -Chord ctrl+spacebar
Remove-PSReadLineKeyHandler -Chord ctrl+spacebar -ViMode Insert
Remove-PSReadLineKeyHandler -Chord ctrl+spacebar -ViMode Command
Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete
#│ ▼2 │ Aliases & Utility Functions
#└─┬──┴─┬───────────────────────────
#│ ▼3 │ Aliases
#└────┴─────────
Set-Alias v gvim
Set-Alias p. Push-Location
Set-Alias p Pop-Location
# (Defined in next section:)
Set-Alias p.s getlocationstack
Set-Alias b battery
Set-Alias _7z 7z
Set-Alias python2 C:\Python27\python.exe
# TODO Update to python 3.4 (Python33 was for the Vim installation I got)
Set-Alias python3 C:\Program` Files\Python37\python.exe
# Set-Alias c++ x86_64-w64-mingw32-c++
# Set-Alias g++ x86_64-w64-mingw32-g++
set-alias say out-speech
set-alias vs "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.exe"
#│ ▼3 │ Utility Functions
#└────┴───────────────────
function list_path
{
$a = $env:path; $a.Split(";")
}
function getlocationstack
{
Get-Location -Stack
}
function lds
{
if ( $on_any_machine )
{
C:\Program` Files\Intellectual` Reserve\LDS` View` 7.1\wcUView.exe
}
}
# Run vim without a swap file.
function vn
{
invoke-expression $("gvim -n")
}
function minecraft
{
if ( $on_desktop )
{
invoke-expression $("javaw -Xmx2048m -Xms1024m -jar `"C:\Program Files, manually installed\MineCraft\Minecraft.exe`"")
}
else
{
echo "You haven't set this up for this machine."
}
}
function word
{
if ( $on_any_machine )
{
& ($office + "WINWORD.EXE")
}
}
function hp([string]$topic)
{
# TODO: Find a Vim filetype spec for powershell help output
invoke-expression $("get-help -full {0} | v -R -" -f $topic)
}
function hpf([string]$topic)
{
# TODO: Find a Vim filetype spec for powershell help output
invoke-expression $("get-help -full {0} > ~/temp/cached_ps_help/{0}_help.txt" -f $topic)
}
function hghp([string]$topic)
{
# TODO: Find a Vim filetype spec for Mercurial help output
invoke-expression $("hg -v help {0} | v -R -" -f $topic)
}
function battery
{
if ( $on_laptop )
{
write-output "Estimated Battery Remaining: $((get-WmiObject win32_battery).EstimatedChargeRemaining)%"
}
elseif ( $on_desktop )
{
write-output "You're on the desktop, you silly!"
}
else
{
write-output "lolwut?"
}
}
# TODO perhaps multi-machine this
function waf-light
{
python C:/development_resources/waf/waf-light $args
}
function waf
{
python C:/development_resources/waf/waf $args
}
#function lsx([string]$other_params)
#{
# # TODO: This won't actually properly work yet.
# invoke-expression $("Get-ChildItem -Exclude .* {0}" -f $other_params)
# Get-ChildItem -Exclude .*
#}
function bash
{
ubuntu run
}
function sh([Parameter(ValueFromRemainingArguments)][string]$command)
{
invoke-expression $("ubuntu run {0}" -f $command)
}
function glog {Invoke-Expression "git log --all --graph --decorate --date=short-local --format=`"format:%Cblue%ad %h %Cred%d%Creset %s`""}
$Shlwapi = Add-Type -MemberDefinition '
[DllImport("Shlwapi.dll", CharSet=CharSet.Auto)]public static extern int StrFormatByteSize(long fileSize, System.Text.StringBuilder pwszBuff, int cchBuff);
' -Name "ShlwapiFunctions" -namespace ShlwapiFunctions -PassThru
function Format-ByteSize([Long]$Size)
{
$Bytes = New-Object Text.StringBuilder 20
$Return = $Shlwapi::StrFormatByteSize($Size, $Bytes, $Bytes.Capacity)
If ($Return) {$Bytes.ToString()}
}
function ll()
{
gci $args `
| Format-Table `
-AutoSize `
@{ `
Name='Mode '; `
Expression={$_.Mode} `
}, `
@{ `
Name='Last Write Time '; `
Expression={ `
$_.LastWriteTime.ToString('yyyy-MM-dd HH:mm') `
} `
}, `
@{ `
Name='Size '; `
Expression={ `
Format-ByteSize($_.Length) `
} `
}, `
Name
}
#│ ▼1 │ Prompt Line
#└────┴─────────────
function prompt
{
# Default contents:
#"PS $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) "
## .Link
## http://go.microsoft.com/fwlink/?LinkID=225750
## .ExternalHelp System.Management.Automation.dll-help.xml
# Refer to these sites for info:
# http://technet.microsoft.com/en-us/library/hh847739.aspx
#$saved_last_exit_code = $LastExitCode
$the_history = $(get-history)
$command_number = 1
if ( $the_history.Length -ne 0 )
{
$command_number = $(get-history)[-1].id + 1
Write-Host "`n" -NoNewLine
#if ( $saved_last_exit_code -eq $null -or $saved_last_exit_code -eq 0 )
#{
#Write-Host "OK`n" -ForegroundColor "Green"
#}
#else
#{
#Write-Host "X $($saved_last_exit_code)`n" -ForegroundColor "Red"
#}
}
# Abbreviate the home directory
$prompt_path = $($executionContext.SessionState.Path.CurrentLocation)
$prompt_path = $prompt_path -replace $($home -replace "\\", "\\"), "~"
$prompt_path += "\"
Write-Host $($prompt_path) -NoNewLine -ForegroundColor "Blue"
# repo branch and status by color
#▼
#▲
Write-Host "`n[" -ForegroundColor "darkgreen" -NoNewLine
Write-Host "r $($command_number)" -ForegroundColor "blue" -NoNewLine
Write-Host "] $('>' * ($nestedPromptLevel + 1))" -ForegroundColor "darkgreen" -NoNewLine
# Must return an object
" "
}
#│ ▼1 │ Some automatic garbage I didn’t put here.
#└────┴───────────────────────────────────────────
# Chocolatey profile
$ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1"
if (Test-Path($ChocolateyProfile)) {
Import-Module "$ChocolateyProfile"
}
#┌────┬───────────────────────────────────────────
#│ ▲1 │ End
# vim: set fmr=▼,▲ fdm=marker cms=#%s et ts=2 sw=0 sts=0 :