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

<#
 .Synopsis
   Add folder to bookmarks list.
 
 .Description
  Add folder to bookmarks list.
 
 .Parameter Name
  The bookmark name.
 
 .Parameter Path
  The Path to folder.
 
 .Example
   # Add bookmark with name.
   ./Add-PSBookmark [ ba ] BookmarkName (Opt)Directory
 
 .Example
   # Add bookmark from pipeline.
   $pwd | Add-PSBookmark -Name "ThisDirectory"
#>

function Add-PSBookmark () {
    Param (
        [Parameter(Position = 0, Mandatory = $true)]
        [Alias("Bookmark")]
        $Name,
        [Parameter(Position = 1, ValueFromPipeline  = $True)]
        [Alias("Path")]
        [string]$dir = $null
    )
    if ( [string]::IsNullOrEmpty($dir) ) {
        $dir = (Get-Location).Path
    }

    Restore-PSBookmarks
    $marks["$Name"] = $dir
    Save-PSBookmarks
    Write-Output ("Location '{1}' saved to bookmark '{0}'" -f $Name, $dir)     
}


<#
 .Synopsis
  Delete bookmark from list.
 
 .Description
  Removes folder from bookmarks list.
 
 .Parameter Name
  The bookmark name.
 .Example
   # Add bookmark with name.
   ./Remove-PSBookmark [ br ] BookmarkName
#>

function Remove-PSBookmark () {
    Param (
        [Parameter(Position = 0, Mandatory = $true)]
        [Alias("Bookmark")]
        [ArgumentCompleter(
            {
                param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)
                return @($marks) -like "$WordToComplete*"
            }
        )]
        $Name
    )

    Restore-PSBookmarks
    $marks.Remove($Name)
    Save-PSBookmarks
    Write-Output ("Location '{0}' removed from bookmarks" -f $Name)     
}

<#
 .Synopsis
  Open saved bookmark.
 
 .Description
  Open saved bookmark.
 
 .Parameter Name
  The bookmark name.
 .Example
   # Add bookmark with name.
   ./Open-PSBookmark [ bo ] BookmarkName
#>

function Open-PSBookmark() {
    Param(
    [Parameter(Position = 0, ValueFromPipelineByPropertyName  = $True, Mandatory = $true)]
    [Alias("Bookmark")]
    [ArgumentCompleter(
        {
            param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)
            return @($marks) -like "$WordToComplete*"
        }
    )]
    $Name
    )
    Set-Location $marks["$Name"]
}

function Restore-PSBookmarks {
    if (test-path $marksPath) {
        Import-Csv $marksPath | ForEach-Object { $marks[$_.key] = $_.value }
    }
}
function Save-PSBookmarks {
    $marks.getenumerator() | export-csv $marksPath -notype
}

<#
 .Synopsis
  List saved bookmark.
 
 .Description
  List saved bookmark.
 
 .Example
   # Add bookmark with name.
   ./Get-PSBookmarks [ bl ] [bv]
#>

function Get-PSBookmarks {
    Restore-PSBookmarks
    $marks
}

$marks = @{ }
$marksPath = Join-Path (split-path -parent $profile) .bookmarks

Restore-PSBookmarks


Export-ModuleMember -Function Add-PSBookmark 
Export-ModuleMember -Function Remove-PSBookmarks
Export-ModuleMember -Function Open-PSBookmark
Export-ModuleMember -Function Get-PSBookmarks