-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMicrosoft.PowerShell_profile.ps1
More file actions
276 lines (243 loc) · 7.87 KB
/
Microsoft.PowerShell_profile.ps1
File metadata and controls
276 lines (243 loc) · 7.87 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
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
Set-PSReadlineKeyHandler -Key ctrl+d -Function ViExit
Set-Alias vim nvim
Set-Alias lg lazygit
function reload {
. $PROFILE
Write-Host "PowerShell profile reloaded!" -ForegroundColor Cyan
}
function push { git push $args }
function pull { git pull $args }
function path { $env:path -split ";" }
function which { (Get-Command $args).Definition }
function mkdocs { docker run --rm -it -p 8000:8000 -v ${PWD}:/docs gitlab.tenonespace.com:5001/devops/docker-images/mkdocs:0.1.7 $args }
# Unix-like commands
if (Get-Alias rm -ErrorAction SilentlyContinue) { Remove-Item Alias:rm -Force }
function rm {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, Position = 0, Mandatory = $true, ValueFromRemainingArguments = $true)]
[string[]]$Path,
[Alias("r")]
[switch]$Recursive,
[Alias("f")]
[switch]$Force,
[switch]$rf
)
process {
$rmArgs = @{}
if ($Recursive -or $rf) { $rmArgs["Recurse"] = $true }
if ($Force -or $rf) {
$rmArgs["Force"] = $true
$rmArgs["ErrorAction"] = "SilentlyContinue"
}
Remove-Item -Path $Path @rmArgs
}
}
function touch {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, Position = 0, Mandatory = $true, ValueFromRemainingArguments = $true)]
[string[]]$Path
)
process {
foreach ($p in $Path) {
if (Test-Path $p) {
(Get-Item $p).LastWriteTime = Get-Date
}
else {
New-Item -Path $p -ItemType File -Force | Out-Null
}
}
}
}
function mktemp {
$tempFileName = [System.IO.Path]::GetTempFileName()
Remove-Item -Path $tempFileName -Force
New-Item -Path $tempFileName -ItemType Directory | Out-Null
Set-Location $tempFileName
}
function time {
$Command = "$args"
Measure-Command {
Invoke-Expression $Command 2>&1 | Out-Default
}
}
function open {
param(
[Parameter(ValueFromPipeline = $true, Position = 0)]
[string]$Path = "."
)
# 1. Resolve the path to get the full system path
$resolvedPath = Resolve-Path $Path -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Path
# 2. Fallback if Resolve-Path fails (e.g., a file that hasn't been saved yet)
if (-not $resolvedPath) { $resolvedPath = $Path }
# 3. Check if the path exists
if (Test-Path $resolvedPath) {
if (Test-Path $resolvedPath -PathType Container) {
# It's a folder: Open in File Explorer
Start-Process "explorer.exe" -ArgumentList "`"$resolvedPath`""
}
else {
# It's a file: Open with the default associated program
Start-Process $resolvedPath
}
}
else {
Write-Error "The path '$Path' doesn't exist."
}
}
# Zoxide
if (Get-Command zoxide) {
Invoke-Expression (& { (zoxide init powershell --cmd cd | Out-String) })
}
# FzF
if (Get-Module -ListAvailable -Name PsFzf) {
Set-PsFzfOption -PSReadlineChordProvider 'Ctrl+t' -PSReadlineChordReverseHistory 'Ctrl+r'
}
# Python Venv
function activate {
[CmdletBinding()]
param(
# Root to search (defaults to current directory)
[Parameter(Position = 0)]
[string] $Path = ".",
# Override the list of venv directory names (search order matters)
[string[]] $Names = @(".venv", "venv", "env", ".env", ".python-venv", ".virtualenv"),
# Show what the function is doing
[switch] $VerboseOutput
)
# Resolve the search root to a full path
$root = Convert-Path -LiteralPath $Path -ErrorAction SilentlyContinue
if (-not $root) {
Write-Error "Path not found: $Path"
return
}
if ($VerboseOutput) { Write-Host "Searching for virtual environments in: $root" -ForegroundColor Cyan }
foreach ($name in $Names) {
$candidate = Join-Path $root $name
if (-not (Test-Path -LiteralPath $candidate -PathType Container)) {
if ($VerboseOutput) { Write-Host "Not found: $candidate" -ForegroundColor DarkGray }
continue
}
$winActivate = Join-Path $candidate "Scripts\Activate.ps1" # Windows
$activateScript = $null
if (Test-Path -LiteralPath $winActivate -PathType Leaf) {
$activateScript = $winActivate
}
if ($activateScript) {
if ($VerboseOutput) { Write-Host "Activating venv: $candidate" -ForegroundColor Green }
& $activateScript
return
}
else {
if ($VerboseOutput) {
Write-Host "Directory found but no PowerShell activation script: $candidate" -ForegroundColor Yellow
}
}
}
Write-Warning "No Python virtual environment found in '$root' (searched: $($Names -join ', '))."
}
function New-Venv {
[CmdletBinding(SupportsShouldProcess)]
param(
[Parameter(Position = 0)]
[ValidateNotNullOrEmpty()]
[string] $Name = ".venv",
[switch] $Force # If set, re-creates the venv by deleting the target dir if it already exists.
)
# Resolve target path in the current directory
$targetPath = Join-Path -Path (Get-Location) -ChildPath $Name
# Handle existing directory
if (Test-Path -LiteralPath $targetPath -PathType Container) {
if ($Force) {
if ($PSCmdlet.ShouldProcess($targetPath, "Remove existing venv directory")) {
try { Remove-Item -LiteralPath $targetPath -Recurse -Force -ErrorAction Stop }
catch {
Write-Error "Failed to remove existing directory '$targetPath': $($_.Exception.Message)"
return
}
}
}
else {
Write-Warning "Directory '$targetPath' already exists. Use -Force to recreate."
return
}
}
# Create the venv
try {
python -m venv "$targetPath"
if ($LASTEXITCODE -ne 0) {
Write-Error "Python reported a non-zero exit code ($LASTEXITCODE) while creating the venv."
return
}
}
catch {
Write-Error "Failed to create virtual environment: $($_.Exception.Message)"
return
}
activate
}
function New-Temp-Venv {
mktemp
new-venv
}
# git prompt
if (Get-Module -ListAvailable -Name posh-git) {
Import-Module posh-git
$GitPromptSettings.DefaultPromptEnableTiming = $true
$GitPromptSettings.DefaultPromptAbbreviateHomeDirectory = $true
$GitPromptSettings.BeforePath.Text = "$env:UserName@$env:ComputerName "
$GitPromptSettings.BeforeStatus.Text = '('
$GitPromptSettings.AfterStatus.Text = ')'
$GitPromptSettings.BeforeStatus.ForegroundColor = [ConsoleColor]::DarkYellow
$GitPromptSettings.AfterStatus.ForegroundColor = [ConsoleColor]::DarkYellow
$GitPromptSettings.BranchColor.ForegroundColor = [ConsoleColor]::DarkYellow
$GitPromptSettings.BeforePath.ForegroundColor = [ConsoleColor]::Green
function prompt {
$prompt = "PS "
$prompt += & $GitPromptScriptBlock
if ($prompt) { "$prompt" } else { " " }
}
}
else {
function Get-BranchName {
try {
$branch = git rev-parse --abbrev-ref HEAD
$str = ""
if ($branch -eq "HEAD") {
$str += " ("
$str += git rev-parse --short HEAD
$str += ")"
}
elseif ($branch) {
$str += " ($branch)"
}
$dirty = git status --porcelain
if ($dirty) {
$str += "*"
}
return $str;
}
catch { return "" }
}
function Get-Role {
$identity = [Security.Principal.WindowsIdentity]::GetCurrent()
$principal = [Security.Principal.WindowsPrincipal] $identity
$adminRole = [Security.Principal.WindowsBuiltInRole]::Administrator
$(if (Test-Path variable:/PSDebugContext) { '[DBG]: ' }
elseif ($principal.IsInRole($adminRole)) { '[ADMIN]: ' }
else { '' }
)
}
function prompt {
$path = "$($executionContext.SessionState.Path.CurrentLocation)"
$branch = $(Get-BranchName)
Write-Host -NoNewline $(Get-Role)
Write-Host -NoNewline "PS "
Write-Host -NoNewline "$env:UserName@$env:ComputerName" -ForegroundColor "green"
Write-Host -NoNewline ":" -ForegroundColor "white"
Write-Host -NoNewline $path -ForegroundColor "blue"
Write-Host -NoNewline $branch -ForegroundColor "DarkYellow"
return "$('>' * ($nestedPromptLevel + 1)) "
}
}