forked from kelleyma49/PSFzf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PSFzf.Functions.ps1
226 lines (198 loc) · 6.37 KB
/
PSFzf.Functions.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
#.ExternalHelp PSFzf.psm1-help.xml
$addedAliases = @()
function script:SetPsFzfAlias {
param($Name,$Function)
New-Alias -Name $Name -Scope Global -Value $Function -ErrorAction Ignore
$addedAliases += $Name
}
function script:SetPsFzfAliasCheck {
param($Name,$Function)
# prevent Get-Command from loading PSFzf
$script:PSModuleAutoLoadingPreferencePrev=$PSModuleAutoLoadingPreference
$PSModuleAutoLoadingPreference='None'
if (-not (Get-Command -Name $Name -ErrorAction Ignore)) {
SetPsFzfAlias $Name $Function
}
# restore module auto loading
$PSModuleAutoLoadingPreference=$script:PSModuleAutoLoadingPreferencePrev
}
function script:RemovePsFzfAliases {
$addedAliases | ForEach-Object {
Remove-Item -Path Alias:$_
}
}
function Invoke-FuzzyEdit()
{
param($Directory=$null)
$files = @()
try {
if ($Directory) {
$prevDir = $PWD.Path
cd $Directory
}
Invoke-Expression (Get-FileSystemCmd .) | Invoke-Fzf -Multi | ForEach-Object { $files += """$_""" }
} catch {
}
finally {
if ($prevDir) {
cd $prevDir
}
}
# HACK to check to see if we're running under Visual Studio Code.
# If so, reuse Visual Studio Code currently open windows:
$editorOptions = ''
if ($null -ne $env:VSCODE_PID) {
$editor = 'code'
$editorOptions += '--reuse-window'
} else {
$editor = $env:EDITOR
if ($null -eq $editor) {
if (!$IsWindows) {
$editor = 'vim'
} else {
$editor = 'code'
}
}
}
if ($null -ne $files) {
$fileList = ''
$files | ForEach-Object {
if ($Directory) {
$fileList += (Join-Path $Directory $_) + ' '
} else {
$fileList += $_ + ' '
}
}
Invoke-Expression -Command ("$editor $editorOptions $fileList")
}
}
#.ExternalHelp PSFzf.psm1-help.xml
function Invoke-FuzzyFasd() {
$result = $null
try {
if (Get-Command Get-Frecents -ErrorAction Ignore) {
Get-Frecents | ForEach-Object { $_.FullPath } | Invoke-Fzf -ReverseInput -NoSort | ForEach-Object { $result = $_ }
} elseif (Get-Command fasd -ErrorAction Ignore) {
fasd -l | Invoke-Fzf -ReverseInput -NoSort | ForEach-Object { $result = $_ }
}
} catch {
}
if ($null -ne $result) {
# use cd in case it's aliased to something else:
cd $result
}
}
#.ExternalHelp PSFzf.psm1-help.xml
function Invoke-FuzzyHistory() {
if (Get-Command Get-PSReadLineOption -ErrorAction SilentlyContinue) {
$result = Get-Content (Get-PSReadLineOption).HistorySavePath | Invoke-Fzf -Reverse -NoSort
} else {
$result = Get-History | ForEach-Object { $_.CommandLine } | Invoke-Fzf -Reverse -NoSort
}
if ($null -ne $result) {
Write-Output "Invoking '$result'`n"
Invoke-Expression "$result" -Verbose
}
}
#.ExternalHelp PSFzf.psm1-help.xml
function GetProcessSelection() {
param(
[scriptblock]
$ResultAction
)
$header = "`n" + $("{0,-8} PROCESS NAME" -f "ID") + "`n"
$result = Get-Process | Where-Object { ![string]::IsNullOrEmpty($_.ProcessName) } | ForEach-Object { "{0,-8} {1}" -f $_.Id,$_.ProcessName } | Invoke-Fzf -Multi -Header $header
$result | ForEach-Object {
&$ResultAction $_
}
}
function Invoke-FuzzyKillProcess() {
GetProcessSelection -ResultAction {
param($result)
$id = $result -replace "([0-9]+)(.*)",'$1'
Stop-Process $id -Verbose
}
}
#.ExternalHelp PSFzf.psm1-help.xml
function Invoke-FuzzySetLocation() {
param($Directory=$null)
if ($null -eq $Directory) { $Directory = $PWD.Path }
$result = $null
try {
if ([string]::IsNullOrWhiteSpace($env:FZF_DEFAULT_COMMAND)) {
Get-ChildItem $Directory -Recurse -ErrorAction Ignore | Where-Object{ $_.PSIsContainer } | Invoke-Fzf | ForEach-Object { $result = $_ }
} else {
Invoke-Fzf | ForEach-Object { $result = $_ }
}
} catch {
}
if ($null -ne $result) {
Set-Location $result
}
}
if ((-not $IsLinux) -and (-not $IsMacOS)) {
#.ExternalHelp PSFzf.psm1-help.xml
function Set-LocationFuzzyEverything() {
param($Directory=$null)
if ($null -eq $Directory) {
$Directory = $PWD.Path
$Global = $False
} else {
$Global = $True
}
$result = $null
try {
Search-Everything -Global:$Global -PathInclude $Directory -FolderInclude @('') | Invoke-Fzf | ForEach-Object { $result = $_ }
} catch {
}
if ($null -ne $result) {
# use cd in case it's aliased to something else:
cd $result
}
}
}
#.ExternalHelp PSFzf.psm1-help.xml
function Invoke-FuzzyZLocation() {
$result = $null
try {
(Get-ZLocation).GetEnumerator() | Sort-Object { $_.Value } -Descending | ForEach-Object{ $_.Key } | Invoke-Fzf -NoSort | ForEach-Object { $result = $_ }
} catch {
}
if ($null -ne $result) {
# use cd in case it's aliased to something else:
cd $result
}
}
#.ExternalHelp PSFzf.psm1-help.xml
function Invoke-FuzzyGitStatus() {
$result = @()
try {
$headerStrings = Get-HeaderStrings
$gitRoot = git rev-parse --show-toplevel
git status --porcelain |
Invoke-Fzf -Multi -Bind $headerStrings[1] -Header $headerStrings[0] | ForEach-Object {
$result += Join-Path $gitRoot $('{0}' -f $_.Substring('?? '.Length))
}
} catch {
# do nothing
}
if ($null -ne $result) {
$result
}
}
function Enable-PsFzfAliases()
{
# set aliases:
if (-not $DisableAliases) {
SetPsFzfAliasCheck "fe" Invoke-FuzzyEdit
SetPsFzfAliasCheck "fh" Invoke-FuzzyHistory
SetPsFzfAliasCheck "ff" Invoke-FuzzyFasd
SetPsFzfAliasCheck "fkill" Invoke-FuzzyKillProcess
SetPsFzfAliasCheck "fd" Invoke-FuzzySetLocation
if (${function:Set-LocationFuzzyEverything}) {
SetPsFzfAliasCheck "cde" Set-LocationFuzzyEverything
}
SetPsFzfAliasCheck "fz" Invoke-FuzzyZLocation
SetPsFzfAliasCheck "fgs" Invoke-FuzzyGitStatus
}
}