-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMicrosoft.PowerShell_profile.ps1
More file actions
288 lines (259 loc) · 9.74 KB
/
Microsoft.PowerShell_profile.ps1
File metadata and controls
288 lines (259 loc) · 9.74 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
277
278
279
280
281
282
283
284
285
286
287
288
function global:Backup-Environment($Verbose = $null) {
$ProfilePath = Split-Path $($PROFILE.CurrentUserCurrentHost) -Parent
Copy-Item "$env:p7settingDir\Microsoft.PowerShell_profile.ps1" $ProfilePath -Force
Copy-Item "$env:p7settingDir\Microsoft.WindowsPowerShell_profile.ps1" $ProfilePath -Force
Write-Host "[$(Get-Date)] Move Profile. CurrentUserCurrentHost" -ForegroundColor Green
}
function P7() {
Invoke-Expression (&starship init powershell)
# function prompt {
# prmt --code $LASTEXITCODE '{path:cyan} {git:purple} {python:yellow:m: 🐍} {time:dim}\n{ok:green}{fail:red} '
# }
# tv init power-shell
Invoke-Expression (& { (zoxide init powershell | Out-String) })
Get-ChildItem Alias:/rd | Out-Null && Remove-Item Alias:rd -ErrorAction SilentlyContinue
Set-Alias -Name cd -Value z -Scope Global -Option AllScope
Set-Alias -Name cdi -Value zi -Scope Global -Option AllScope
}
$global:initialModuleList = @(
"quickWebAction",
"quickVimAction",
"quickPSReadLine",
"quickPwshUtils.psm1",
"CLI-Basic"
)
$global:extraModuleList = @(
"Converter"
"GUI-Basic"
"CLI-Extra"
"quickMathAction"
"quickGitAction"
"quickTerminalAction"
"quickFilePathAction"
)
$global:personalModuleList = $global:initialModuleList + $global:extraModuleList
function initShellApp() {
foreach ($module in $global:initialModuleList) {
Import-Module -Name (Join-Path $env:p7settingDir $module) -Scope Global
}
}
function Restart-ModuleList() {
param (
[array]$ModuleList,
[string]$ModulePath = $pwd,
[Switch]$preferPsd1
)
foreach ($ModuleName in $ModuleList) {
$moduleFullPath = Join-Path $ModulePath $ModuleName
$psd1Module = (Test-Path -Path "$moduleFullPath.psd1") ? "$moduleFullPath.psd1" : $moduleFullPath
$psm1Module = (Test-Path -Path "$moduleFullPath.psm1") ? "$moduleFullPath.psm1" : $moduleFullPath
$finalPath = $preferPsd1 ? $psd1Module : $psm1Module
Remove-Module -Name $moduleFullPath -ErrorAction SilentlyContinue
Import-Module -Name $moduleFullPath -Force -ErrorAction Stop
Write-Output "$ModuleName reimported"
}
}
function global:Restart-Profile($option = "env") {
if ($option -match "^all") {
Restart-ModuleList -ModuleList $global:personalModuleList -ModulePath $env:p7settingDir
. $PROFILE
Write-Output "Restart profile and All module."
}
else {
. $PROFILE
Write-Output "Restart pwsh Profile."
}
}
Set-Alias -Name repro -Value Restart-Profile
function cdcb(
[Parameter(ValueFromPipeline = $true)]
$defaultDir = (Get-Clipboard)
) {
$copiedPath = ($defaultDir -replace '"')
$property = Get-Item $copiedPath
if ($property.PSIsContainer -eq $true) {
Set-Location $copiedPath
}
else {
Set-Location (Split-Path -Path $copiedPath -Parent)
}
}
function Set-LocationWhere(
[Parameter(
# Mandatory = $true,
ValueFromPipeline = $true
)]
$files = (Get-Clipboard)
) {
$whichBackend = "scoop which" # INFO: default is `which` that windows provide. but this return a list.
try {
$tryWhichCommand = Invoke-Expression "$whichBackend $files" -ErrorAction SilentlyContinue
# $initialInfo = Get-Command $files
$commandInfo = Get-Command $tryWhichCommand -ErrorAction SilentlyContinue
}
catch {
# $initialInfo = $null
$commandInfo = Get-Command $files -ErrorAction SilentlyContinue
}
# echo ($commandInfo).PSObject.TypeNames
if ($commandInfo.PSObject.TypeNames -notcontains "System.Object[]") {
switch -Exact ($commandInfo.CommandType) {
"Application" {
# INFO: We need something to detect executable here. Mostly exe files but there could also be other type as well.
if (($commandInfo.Extension -match "exe|cmd")) {
$listBinaries = Invoke-Expression "(Resolve-Path ($whichBackend $files)).ToString()"
try {
$fileType = (${listBinaries}?.PsObject.TypeNames[0])
}
catch {
Write-Host "From local dir not path." -ForegroundColor Blue
}
if ($fileType -match "String") {
$finalBinariesPath = $listBinaries
}
else {
$finalBinariesPath = $files
}
Set-Location (Split-Path $finalBinariesPath -Parent)
}
else {
echo "cdcb now."
# other extensions
cdcb $files
}
; break;
}
"Function" {
$definition = ($commandInfo).Source
$ModuleInfo = Get-Module $commandInfo.Source
$ModulePath = $ModuleInfo.Path
$linkInfo = Format-Hyperlink $commandInfo.Source $ModulePath
Write-Host "function from $linkInfo module." -ForegroundColor Yellow -BackgroundColor DarkBlue
Write-Host $commandInfo.Definition
Set-Location (Split-Path $ModulePath -Parent)
}
"Alias" {
$definition = ($commandInfo).Definition
$ModuleInfo = Get-Module $commandInfo.Source
$ModulePath = $ModuleInfo.Path
$linkInfo = Format-Hyperlink $commandInfo.Source $ModulePath
Write-Host "alias of $definition , source: $linkInfo" -ForegroundColor Yellow -BackgroundColor Black
$definitionInfo = Get-Command $definition
Set-LocationWhere $definitionInfo.Name
}
"ExternalScript" {
$definition = ($commandInfo).Source
$scriptName = $commandInfo.Name
$linkInfo = Format-Hyperlink $scriptName $commandInfo.Source
Write-Host "Script from $linkInfo." -ForegroundColor Yellow -BackgroundColor DarkBlue
$fileName = ($files)
try {
$ScriptContent = Get-Content "$env:LOCALAPPDATA/shims/$fileName.ps1"
Write-Host $ScriptContent
# Try to extract path from variable assignments like $path = '...'
$pathLine = $ScriptContent | Where-Object { $_ -match '\$\w+\s*=\s*[''"](.+?)[''"]' }
if ($pathLine) {
$extractedPath = $Matches[1]
Write-Host "Extracted path: $extractedPath" -ForegroundColor Cyan
cdcb $extractedPath
}
else {
# Fallback to original method
Write-Output $ScriptContent |`
Select-Object -Index 0 |`
Get-PathFromFiles | cdcb
}
}
catch {
Write-Error "Had tried, still failed on shim."
Set-Location (Split-Path $definition -Parent)
}
}
default {
Write-Host "what... files?" -ForegroundColor Red -BackgroundColor Yellow
$fileName = ($files)
try {
Get-Content "$env:LOCALAPPDATA/shims/$fileName.ps1" |`
Select-Object -Index 0 |`
Get-PathFromFiles | cdcb
}
catch {
Write-Error "Had tried, still failed."
}
} # optional
}
}
else {
$finalBinariesPath = $commandInfo | % { $_.Source } | fzf
Set-Location (Split-Path ($finalBinariesPath) -Parent)
}
}
Set-Alias -Name cdw -Value Set-LocationWhere
Set-Alias -Name cdwhere -Value Set-LocationWhere
function addPath {
param (# Parameter help description
[Parameter(
# Mandatory = $true,
ValueFromPipeline = $true
)]
[Alias("d")]
$dirList = $pwd,
[Parameter(Mandatory = $false)]
[Alias("p")]
$parent = $null
)
foreach ($dir in $dirList) {
if ($null -ne $parent) {
$dir = Split-Path $dir -Parent
}
else {
$dir
}
$d = Resolve-Path $dir
$Env:Path += ";" + $d;
}
}
function global:initProfileEnv {
[Console]::InputEncoding = [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new()
$PSDefaultParameterValues['Out-File:Encoding'] = 'utf8'
# $Env:ProgramFilesD = "D:\Program Files"
$Env:ProgramDataD = "D:\ProgramDataD"
$Env:dotfilesRepo = "$Env:ProgramDataD\dotfiles"
$Env:p7settingDir = "D:\ProgramDataD\MiscLang\24.01-PowerShell\proj\powershellConfig"
$Env:pipxLocalDir = "~\.local\bin"
$Env:usrbinD = "D:\usr\bin"
$diradd = @(
$Env:usrbinD
, $Env:pipxLocalDir
)
foreach ($d in $diradd) {
$Env:Path += ";" + $d;
}
}
# INFO: cd- and cd--, same logic with cd+ and cd++
function cd-($rep = 1) {
if ($rep -le 0) { return } # Since I use that in scripts... it can underflow somehow.
foreach ($i in (1..$rep)) {
Set-Location -
}
}
function cd+($rep = 1) {
if ($rep -le 0) { return }
foreach ($i in (1..$rep)) {
Set-Location +
}
}
function ..($rep = 1) {
$furtherParent = $pwd
foreach ($i in (1..$rep)) {
$furtherParent = Split-Path -Path $furtherParent -Parent
}
Set-Location $furtherParent
}
Set-Alias -Name cd.. -Value .. -Scope Global -Option AllScope
# INFO: Rescue explorer function.
function Restart-Explorer {
Stop-Process -Name explorer
}
initProfileEnv
initShellApp