Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 4 additions & 30 deletions upto.sh
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
#!/bin/bash
function upto() {
local EXPRESSION="$1"
if [ -z "$EXPRESSION" ]; then
echo "A folder expression must be provided." >&2
return 1
fi
if [ "$EXPRESSION" = "/" ]; then
cd "/"
return 0
fi
local CURRENT_FOLDER="$(pwd)"
local MATCHED_DIR=""
local MATCHING=true

while [ "$MATCHING" = true ]; do
if [[ "$CURRENT_FOLDER" =~ "$EXPRESSION" ]]; then
MATCHED_DIR="$CURRENT_FOLDER"
CURRENT_FOLDER=$(dirname "$CURRENT_FOLDER")
else
MATCHING=false
fi
done
if [ -n "$MATCHED_DIR" ]; then
cd "$MATCHED_DIR"
return 0
else
echo "No Match." >&2
return 1
fi
function upto() {
local D="${PWD%"${PWD#*/*([^/])$1*/}"}"
[[ -z "$D" ]] || cd "$D"
}

# complete upto
Expand All @@ -36,6 +10,6 @@ _upto () {
local cur prev words cword
_init_completion || return

COMPREPLY+=( $( compgen -W "$( echo ${PWD//\// } )" -- $cur ) )
COMPREPLY+=( $( compgen -W "${PWD//\// }" -- $cur ) )
}
complete -F _upto upto