-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpowershellmarks.psm1
More file actions
109 lines (95 loc) · 3.08 KB
/
powershellmarks.psm1
File metadata and controls
109 lines (95 loc) · 3.08 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
<#
Module powershellmarks
Powershellmarks is a powershell module that allows you to save and jump to commonly
used directories. Supports tab completion.
The bookmarks file is stored in the user's home folder (~/.sdirs).
It is simply a text file, meaning it may be edited by the user.
USAGE:
s bookmarkname - saves the curr dir as bookmarkname
g bookmarkname - jumps to the that bookmark
g b[TAB] - tab completion is available
p bookmarkname - prints the bookmark
p b[TAB] - tab completion is available
d bookmarkname - deletes the bookmark
d [TAB] - tab completion is available
l - list all bookmarks
Version history
---------------
v1.0.0 - Initial version
GNU GENERAL PUBLIC LICENSE Version 2
https://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
#>
$SDIRS = "~/.sdirs"
if (!(Test-Path -Path $SDIRS)) {
New-Item -Path $SDIRS -ItemType File -Force
}
function Get-Bookmark {
param(
[string]$name
)
return Get-Content -Path $SDIRS | Where-Object { $_ -match "$name=" } | ForEach-Object { $_ -replace "$name=|`"", "" }
}
function Add-Bookmark {
param(
[string]$name
)
if ([string]::IsNullOrEmpty($name)) {
Write-Error "Bookmark name is required"
}
elseif ($name -ne ($name -creplace '[^\w]')) {
Write-Error "The bookmark name is not valid"
}
$target = Get-Bookmark $name
if (!$target) {
"$name=`"$(Convert-Path -LiteralPath $PWD)`"" | Out-File -FilePath $SDIRS -Append
}
}
function Remove-Bookmark {
param(
[string]$name
)
$tempFile = "~/.sdirs.bak"
Move-Item -Path $SDIRS -Destination $tempFile -Force
Get-Content -Path $tempFile | Where-Object { $_ -notmatch "$name=" } | Set-Content -Path $SDIRS
if (!(Test-Path -Path $SDIRS)) {
New-Item -Path $SDIRS -ItemType File -Force
}
Remove-Item -Path $tempFile
}
function Open-Bookmark {
param(
[string]$name
)
$target = Get-Bookmark $name
if (Test-Path -Path $target) {
Set-Location -Path $target
}
else {
Write-Error "'$name' bookmark does not exist"
}
}
function Show-Bookmark {
param(
[string]$name
)
$target = Get-Bookmark $name
Write-Host $target -ForegroundColor "Yellow"
}
function Show-Bookmarks {
Get-Content -Path $SDIRS | ForEach-Object {
$parts = $_ -split "="
Write-Host "$($parts[0]) $($parts[1])" -ForegroundColor "Yellow"
}
}
$completionBlock = {
param(
$commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameters
)
Get-Content -Path $SDIRS | Where-Object { $_ -like "$wordToComplete*" } | ForEach-Object { $($_ -split "=")[0] }
}
Register-ArgumentCompleter -CommandName Open-Bookmark -ParameterName name -ScriptBlock $completionBlock
New-Alias -Name s -Value Add-Bookmark
New-Alias -Name d -Value Remove-Bookmark
New-Alias -Name g -Value Open-Bookmark
New-Alias -Name p -Value Show-Bookmark
New-Alias -Name l -Value Show-Bookmarks