Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add max length feature to cwd #326

Merged
merged 4 commits into from
Feb 16, 2025
Merged
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
31 changes: 29 additions & 2 deletions scripts/cwd.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#!/usr/bin/env bash

current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "$current_dir/utils.sh"

# return current working directory of tmux pane
getPaneDir() {
nextone="false"
Expand All @@ -13,10 +16,34 @@ getPaneDir() {
}

main() {
path=$(getPaneDir)
path="$(getPaneDir)"

if [[ "$path" == "$HOME" ]]; then
echo "~"
exit 0
fi

# change '/home/user' to '~'
cwd="${path/"$HOME"/'~'}"
cwd="${path/"${HOME}/"/'~/'}"

# check max number of subdirs to display. 0 means unlimited
cwd_max_dirs="$(get_tmux_option "@dracula-cwd-max-dirs" "0")"

if [[ "$cwd_max_dirs" -gt 0 ]]; then
base_to_erase=$cwd
for ((i = 0 ; i < cwd_max_dirs ; i++)); do
base_to_erase="${base_to_erase%/*}"
done
# / would have #base_to_erase of 0 and ~/ has #base_to_erase of 1. we want to exclude both cases
if [[ ${#base_to_erase} -gt 1 ]]; then
cwd="…/${cwd:${#base_to_erase}+1}"
fi
fi

cwd_max_chars="$(get_tmux_option "@dracula-cwd-max-chars" "0")"
if [[ "${cwd_max_chars}" -gt 0 && "${#cwd}" -gt "$cwd_max_chars" ]]; then
cwd="…/…${cwd:(- cwd_max_chars)}"
fi

echo "$cwd"
}
Expand Down