Skip to content
Merged
Show file tree
Hide file tree
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
49 changes: 42 additions & 7 deletions bin/sw
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ and one level deeper. <project> is matched against folder names as a
case-insensitive extended regex; whole-name matches win over partial
matches, and direct children win over deeper folders.

If multiple folders match equally well, all matches are listed and nothing
is opened.
If multiple folders match equally well but one folder's name is a prefix
of all the others (nos-next among nos-next-copy and nos-next-2), that main
folder is opened. Genuinely different matches are listed and nothing is
opened.
EOF
}

Expand All @@ -35,21 +37,54 @@ if ! command -v code >/dev/null 2>&1; then
exit 1
fi

# Prints the match whose folder name is a prefix of all other matches'
# names (e.g. nos-next among nos-next-copy and nos-next-2). Prints nothing
# when the matches are genuinely different.
main_of() {
shortest=''
main=''
while IFS= read -r dir; do
base=$(basename "$dir" | tr '[:upper:]' '[:lower:]')
if [ -z "$shortest" ] || [ "${#base}" -lt "${#shortest}" ]; then
shortest=$base
main=$dir
fi
done <<EOF
$1
EOF
while IFS= read -r dir; do
[ "$dir" = "$main" ] && continue
base=$(basename "$dir" | tr '[:upper:]' '[:lower:]')
case "$base" in
"$shortest"?*) ;;
*) return 1 ;;
esac
done <<EOF
$1
EOF
printf '%s\n' "$main"
}

# Opens a single match in VSCode, or lists them all when ambiguous.
open_match() {
matches=$1

[ -n "$matches" ] || return 1

count=$(printf '%s\n' "$matches" | wc -l | tr -d ' ')
match=$matches
if [ "$count" -gt 1 ]; then
echo "'$name' matches multiple projects:" >&2
printf '%s\n' "$matches" | sed 's/^/ /' >&2
exit 1
match=$(main_of "$matches")
if [ -z "$match" ]; then
echo "'$name' matches multiple projects:" >&2
printf '%s\n' "$matches" | sed 's/^/ /' >&2
exit 1
fi
echo "The other matches are copies of $(basename "$match") with a suffix"
fi

echo "Opening $matches"
code "$matches"
echo "Opening $match"
code "$match"
exit $?
}

Expand Down
20 changes: 20 additions & 0 deletions profile/prompt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh

# Show the current git branch in the prompt, oh-my-zsh style:
# ➜ dir (branch)

git_branch() {
branch=$(git symbolic-ref --short -q HEAD 2>/dev/null) ||
branch=$(git rev-parse --short HEAD 2>/dev/null) ||
return 0
printf ' (%s)' "$branch"
}

if [ -n "$ZSH_VERSION" ]; then
setopt PROMPT_SUBST
# shellcheck disable=SC2016,SC2034
PROMPT='%(?.%F{green}.%F{red})➜%f %F{cyan}%c%f%F{blue}$(git_branch)%f '
elif [ -n "$BASH_VERSION" ]; then
# shellcheck disable=SC2016
PS1='➜ \[\e[36m\]\W\[\e[34m\]$(git_branch)\[\e[0m\] '
fi