Skip to content

Commit 3e1e7a1

Browse files
Merge pull request #326 from dracula/feature/cwd_max_length
feature: add max length feature to cwd
2 parents a97cf51 + 42b3179 commit 3e1e7a1

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

scripts/cwd.sh

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/usr/bin/env bash
22

3+
current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4+
source "$current_dir/utils.sh"
5+
36
# return current working directory of tmux pane
47
getPaneDir() {
58
nextone="false"
@@ -13,10 +16,34 @@ getPaneDir() {
1316
}
1417

1518
main() {
16-
path=$(getPaneDir)
19+
path="$(getPaneDir)"
20+
21+
if [[ "$path" == "$HOME" ]]; then
22+
echo "~"
23+
exit 0
24+
fi
1725

1826
# change '/home/user' to '~'
19-
cwd="${path/"$HOME"/'~'}"
27+
cwd="${path/"${HOME}/"/'~/'}"
28+
29+
# check max number of subdirs to display. 0 means unlimited
30+
cwd_max_dirs="$(get_tmux_option "@dracula-cwd-max-dirs" "0")"
31+
32+
if [[ "$cwd_max_dirs" -gt 0 ]]; then
33+
base_to_erase=$cwd
34+
for ((i = 0 ; i < cwd_max_dirs ; i++)); do
35+
base_to_erase="${base_to_erase%/*}"
36+
done
37+
# / would have #base_to_erase of 0 and ~/ has #base_to_erase of 1. we want to exclude both cases
38+
if [[ ${#base_to_erase} -gt 1 ]]; then
39+
cwd="…/${cwd:${#base_to_erase}+1}"
40+
fi
41+
fi
42+
43+
cwd_max_chars="$(get_tmux_option "@dracula-cwd-max-chars" "0")"
44+
if [[ "${cwd_max_chars}" -gt 0 && "${#cwd}" -gt "$cwd_max_chars" ]]; then
45+
cwd="…/…${cwd:(- cwd_max_chars)}"
46+
fi
2047

2148
echo "$cwd"
2249
}

0 commit comments

Comments
 (0)