From c4199d5c7161889e45b1ca46c92e1fabaf548c89 Mon Sep 17 00:00:00 2001 From: Dterrell Date: Wed, 4 May 2016 11:06:25 -0400 Subject: [PATCH 1/2] Added support for PowerShell --- README.md | 8 +++++++- up-to.psm1 | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 up-to.psm1 diff --git a/README.md b/README.md index 1c53916..61cb3a9 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ upto ==== -Bash function that allows to go up to a certain directory. +Bash function and PowerShell script that allows to go up to a certain directory. Stop counting how many levels you have to go up in a folder tree and just type where you want to go! No more `cd ../../../..`. @@ -17,4 +17,10 @@ upto will go to the highest folder matched, in the previous example, if you writ Configuration ------------- +Bash: Just add `upto.sh` to your `/etc/profile.d/` folder. + +PowerShell: +Create a `Up-To` directory in one of yoru PowerShell Module Paths (typically `~\WindowsPowerShell\Modules`, but it can be any path returned by `$env:PSModulePath`) and add `up-to.psm1` there. + + diff --git a/up-to.psm1 b/up-to.psm1 new file mode 100644 index 0000000..1247771 --- /dev/null +++ b/up-to.psm1 @@ -0,0 +1,41 @@ +<# + .Synopsis + Quickly navigate up the directory hierarchy to a matching directory + + .Description + Searches up the directory hierarchy for a matching directory and sets the path + to the highest matching location + + .Parameter target + The subpath to search for + + .Example + # In `C:\Users\MyUser\Documents\foo\bar\baz` go to the `foo` directory + Up-To foo + + .Example + # From any directory on the `C:\` drive, quickly go to the top level directory + Up-To c +#> +Function Up-To { + Param ( + [Parameter(Mandatory=$true)] + [string]$target + ) + + $target = $target.ToLower() + + $path = (Get-Item -Path ".\").FullName.ToLower() + $index = $path.IndexOf($target) + If ($index -gt -1){ + $nextSlash = $path.IndexOf("\", $index) + If ($nextSlash -eq -1) { + $nextSlash = $path.Length + } + Set-Location -Path ($path[0..$nextSlash] -join "") + } Else { + Write-Host -foregroundcolor Red $target 'not found' + } +} +Set-Alias upto Up-To +Export-ModuleMember -Function Up-To -Alias upto \ No newline at end of file From ec04603abe8bc00a8b2a8b6a8174f73f9756986e Mon Sep 17 00:00:00 2001 From: Dterrell Date: Wed, 4 May 2016 12:57:22 -0400 Subject: [PATCH 2/2] Simplify via regex --- up-to.psm1 | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/up-to.psm1 b/up-to.psm1 index 1247771..1018fb8 100644 --- a/up-to.psm1 +++ b/up-to.psm1 @@ -23,19 +23,8 @@ Function Up-To { [string]$target ) - $target = $target.ToLower() - - $path = (Get-Item -Path ".\").FullName.ToLower() - $index = $path.IndexOf($target) - If ($index -gt -1){ - $nextSlash = $path.IndexOf("\", $index) - If ($nextSlash -eq -1) { - $nextSlash = $path.Length - } - Set-Location -Path ($path[0..$nextSlash] -join "") - } Else { - Write-Host -foregroundcolor Red $target 'not found' - } + Set-Location -Path ( + [regex]::match((Get-Item -Path ".\").FullName, "(.*?" + $target + ".*?(\\|$))|(^.*$)").Value) } Set-Alias upto Up-To Export-ModuleMember -Function Up-To -Alias upto \ No newline at end of file