Skip to content

Commit 5c49f70

Browse files
author
Staffan Gustafsson
committed
Initial commit of CDPath module
CDPath is a module to help speed navigaition of directories in powershell.
1 parent 0b0879b commit 5c49f70

4 files changed

Lines changed: 324 additions & 1 deletion

File tree

CDPath/CDCompletion.ps1

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#
2+
# .SYNOPSIS
3+
#
4+
# Description of added completer
5+
#
6+
function Complete-CDPath
7+
{
8+
[ArgumentCompleter(
9+
Parameter = 'Path',
10+
Command = ('Set-CDPathLocation'),
11+
Description = 'Completes the cdPath parameter: cd <pattern> <TAB>'
12+
)]
13+
param($commandName, $parameterName, $wordToComplete, $commandAst, $fakeBoundParameter)
14+
$cdpath = @('.')
15+
if (Test-Path '~\documents\WindowsPowershell\cdpath.txt')
16+
{
17+
$cdpath += Get-CDPath
18+
}
19+
else
20+
{
21+
$cdpath += $env:USERPROFILE
22+
}
23+
24+
if ($commandAst.CommandElements.Count -ne 2)
25+
{
26+
return
27+
}
28+
$ce = $commandAst.CommandElements[1]
29+
if ($ce -isnot [System.Management.Automation.Language.StringConstantExpressionAst])
30+
{
31+
return
32+
}
33+
$pattern = $commandAst.CommandElements[1].Value
34+
35+
Join-Path $cdpath "$pattern*" -Resolve -ea:SilentlyContinue | foreach {
36+
if (Test-Path -PathType Container $_)
37+
{
38+
if ($_.Contains(' '))
39+
{
40+
$_ = "'$_'"
41+
}
42+
New-CompletionResult -CompletionResultType ProviderContainer $_
43+
}
44+
}
45+
}

CDPath/CDPathLocation.ps1

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
$script:Options = [pscustomobject] @{
2+
SetWindowTitle = $true
3+
}
4+
5+
$script:PreviousWorkingDir = '~'
6+
7+
<#
8+
.SYNOPSIS
9+
Changing location using by resolving a pattern agains a set of paths
10+
11+
.EXAMPLE
12+
Set-CDPathLocation ....
13+
14+
The example changes the current location to the parent three levels up.
15+
.. (first parent)
16+
... (second parent)
17+
.... (third parent)
18+
19+
.EXAMPLE
20+
Set-CDLocation 'C:\Program Files (X86)\Microsoft Visual Studio 12'
21+
Set-CDLocation ~
22+
# Go back to 'C:\Program Files (X86)\Microsoft Visual Studio 12'
23+
Set-CDPathLocation -
24+
25+
.EXAMPLE
26+
27+
Get-CDPath
28+
~/Documents/GitHub
29+
~/Documents
30+
~/corpsrc
31+
32+
# go to ~/documents/WindowsPowerShell/Modules/CDPath
33+
Set-CDLocation win mod cdp
34+
35+
36+
#>
37+
function Set-CDPathLocation
38+
{
39+
param(
40+
[Parameter(Position=0)]
41+
[string] $Path,
42+
[Parameter(ValueFromRemainingArguments)]
43+
[string[]] $Remaining=$null,
44+
[switch] $Exact
45+
)
46+
47+
$CurrentWorkingDir = $Pwd.Path
48+
$NewPath = $null
49+
50+
# If there are extra arguments, create a globbing expression
51+
# so that cd a b c => cd a*\b*\c*
52+
if ($Remaining)
53+
{
54+
$ofs='*\'
55+
$path= "$path*\$Remaining*"
56+
}
57+
58+
# Resolve Path
59+
if (!$Path)
60+
{
61+
$Path = '~'
62+
}
63+
64+
# .'ing shortcuts for ... and ....
65+
if ($Path -match "^\.{3,}$")
66+
{
67+
$Path = $path.Substring(1) -replace '.','..\'
68+
}
69+
70+
71+
if ($Path -eq '-')
72+
{
73+
# pop back to your old Path
74+
Set-Location $script:PreviousWorkingDir
75+
$script:PreviousWorkingDir = $CurrentWorkingDir
76+
return
77+
}
78+
# See if the location exists. If it does not, use $CDPath to
79+
# resolve to a location that exists
80+
if (Test-Path $Path -ea SilentlyContinue)
81+
{
82+
$NewPath = $Path
83+
}
84+
else
85+
{
86+
if ($Exact)
87+
{
88+
$paths = Join-Path $cdpath "$path"
89+
}
90+
else
91+
{
92+
$paths = Join-Path $cdpath "$path*" -Resolve -ea SilentlyContinue
93+
}
94+
if ($paths) {
95+
if ($paths -is [string])
96+
{
97+
$NewPath=$paths
98+
}
99+
else
100+
{
101+
# find the first match that is a container
102+
$res=Test-Path -PathType:Container $paths
103+
for($i=0; $i -lt $res.Length;++$i)
104+
{
105+
if ($res[$i])
106+
{
107+
$NewPath=$paths[$i]
108+
break
109+
} #if
110+
} #for
111+
}
112+
} #if $paths
113+
# clear $newpath if it is not a directory
114+
if($NewPath -and !(Test-Path $NewPath -PathType:Container))
115+
{
116+
$NewPath=$null
117+
}
118+
119+
}#else
120+
121+
if ($NewPath )
122+
{
123+
$script:PreviousWorkingDir = $Pwd.Path
124+
Set-Location $NewPath
125+
if($script:Options.SetWindowTitle)
126+
{
127+
$Host.UI.RawUI.WindowTitle=$NewPath
128+
}
129+
}
130+
else
131+
{
132+
if (Test-Path $path -ea SilentlyContinue)
133+
{
134+
Set-Location $path
135+
}
136+
else {
137+
Write-Error "Directory not found: $path"
138+
}
139+
}
140+
}
141+
142+
143+
function Update-Cdpath
144+
{
145+
$script:cdpath=Get-Content '~\documents\WindowsPowershell\cdpath.txt' |
146+
Where-Object {$_.Trim()} |
147+
ForEach-Object { $ExecutionContext.InvokeCommand.ExpandString($_) }
148+
}
149+
150+
function Add-CDPath
151+
{
152+
param(
153+
[string[]] $NewPath
154+
,
155+
[Switch] $Prepend
156+
)
157+
158+
if ($Prepend)
159+
{
160+
Set-CDPath @($NewPath + $script:cdpath)
161+
}
162+
else
163+
{
164+
Set-CDPath @($script:cdpath + $NewPath)
165+
}
166+
}
167+
168+
function Get-CDPath
169+
{
170+
$script:cdpath
171+
}
172+
173+
function Get-CDPathOption
174+
{
175+
$script:Options
176+
}
177+
178+
function Set-CDPath
179+
{
180+
param(
181+
[Parameter(Mandatory)]
182+
[string[]] $Path
183+
)
184+
$script:CDPath = $Path
185+
Set-Content '~\documents\WindowsPowershell\cdpath.txt' $Path
186+
}
187+
188+
<#
189+
.SYNOPSIS
190+
Customizes the behavior of Set-CDPathLocation in CDPath
191+
#>
192+
function Set-CDPathOption
193+
{
194+
param(
195+
# Indicates if the the window title should be changed when changing location
196+
[Switch] $SetWindowTitle
197+
)
198+
199+
$script:Options.SetWindowTitle = [bool] $SetWindowTitle
200+
201+
}

CDPath/cdpath.psm1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
Set-StrictMode -Version 4
3+
4+
. $PSScriptRoot\CDPathLocation.ps1
5+
6+
# read cdpath from file
7+
Update-Cdpath
8+
9+
if (-not $script:cdpath)
10+
{
11+
$script:cdpath=$env:USERPROFILE,$env:programfiles,$env:windir
12+
}
13+
14+
Export-ModuleMember Set-CDPathLocation, Update-CdPath, Get-CDPath, Add-CDPath, Set-CDPathOption, Get-CDPathOption -alias cd
15+

README.md

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,66 @@
11
CDPath
22
======
33

4-
PowerShell module to make navigating your system a breeze
4+
PowerShell module to make navigating your system a breeze.
5+
6+
The main function of the module is Set-CDPathLocation (below referenced by 'cd') and supports the following:
7+
8+
* Go to previous location without pushd/popd (cd -)
9+
* Go upwards multiple levels (cd ....)
10+
* Go to a directory in a predefined path
11+
* Integration with TabExpansion++
12+
13+
14+
Usage
15+
-----
16+
Assuming you've installed the module somewhere in your module path, just import the module in your profile, e.g.:
17+
```powershell
18+
Import-Module CDPath
19+
# if you've imported PSCX and want to use cd as an alias for Set-CDPathLocation, you must first remove the alias set by PSCX
20+
Remove-Item alias:cd
21+
Set-Alias cd Set-CDPathLocation
22+
```
23+
24+
To setup the CDPath, i.e. the parent directories of the directories you most often navigate to, call
25+
```powershell
26+
# a user who has github projects and a directory where corporate source code is stored may set up the path like this
27+
Set-CDPath -Path ~\Documents\GitHub,d:\corpsrc,~\documents
28+
```
29+
The CDPath is persisted at ~/Documents/WindowsPowerShell/cdpath.txt
30+
31+
Imagine the following directory structure
32+
33+
~/Documents/
34+
GitHub
35+
PSReadLine
36+
TabExpansion++
37+
CDPath
38+
WindowsPowerShell
39+
ArgumentCompleters
40+
Modules
41+
D:\CorpSrc
42+
PSApi
43+
ToolingApi
44+
Frontend
45+
Backend
46+
47+
Navigation can then be done like this:
48+
49+
```powershell
50+
# goto PSReadline
51+
cd psr
52+
53+
# goto ~/Documents/WindowsPowerShell/Modules
54+
cd win mod
55+
56+
# goto ToolingApi
57+
cd too
58+
59+
#goto TabExpansion++
60+
cd tab
61+
# go up three levels
62+
cd ....
63+
# go back to previous directory
64+
cd -
65+
66+
```

0 commit comments

Comments
 (0)