33current_dir=" $( cd " $( dirname " ${BASH_SOURCE[0]} " ) " && pwd) "
44source " $current_dir /../lib/utils.sh"
55
6- # return current working directory of tmux pane
76get_pane_dir () {
87 nextone=" false"
98 ret=" "
@@ -15,37 +14,41 @@ get_pane_dir() {
1514 echo " ${ret% ?} "
1615}
1716
18- # truncate the path if it's longer than 30 characters
1917truncate_path () {
20- local path=" $1 " limit=20 truncated_path=" "
18+ local path=" $1 "
19+ local max_length=$( get_tmux_option " @tmux2k-cwd-length" 40)
20+ local min_depth=$( get_tmux_option " @tmux2k-cwd-min-depth" 4)
21+ local prefix_chars=$( get_tmux_option " @tmux2k-cwd-prefix-chars" 2)
22+
23+ [[ ${# path} -le $max_length ]] && echo " $path " && return
2124
22- # If path is greater than limit, then truncate parts to 2 characters
23- [[ ${# path} -le $limit ]] && echo " $path " && return
2425 IFS=' /' read -ra parts <<< " $path"
26+ local truncated=" "
27+
2528 for (( i = 0 ; i < ${# parts[@]} - 1 ; i++ )) ; do
26- truncated_path +=" ${parts[i]: 0: 2 } /"
29+ truncated +=" ${parts[i]: 0: $prefix_chars } /"
2730 done
28- truncated_path+=" ${parts[-1]} "
31+ truncated+=" ${parts[-1]} "
32+
33+ [[ ${# truncated} -le $max_length ]] && echo " $truncated " && return
2934
30- # If there are more than 4 slashes, then we will truncate the middle part
31- if [[ $( tr -cd ' / ' <<< " $truncated_path " | wc -c ) - gt 4 ]]; then
32- IFS=' /' read -ra parts <<< " $truncated_path "
35+ local slash_count= $( tr -cd ' / ' <<< " $truncated " | wc -c )
36+ if [[ $slash_count - gt $min_depth ]]; then
37+ IFS=' /' read -ra parts <<< " $truncated "
3338 echo " ${parts[0]} /${parts[1]} /.../${parts[-2]} /${parts[-1]} "
3439 else
35- echo " $truncated_path "
40+ echo " $truncated "
3641 fi
3742}
3843
3944main () {
4045 path=$( get_pane_dir)
4146
42- # Change '/home/user' to '~'
4347 cwd=" ${path/ " $HOME " / ' ~' } "
4448 truncated_cwd=$( truncate_path " $cwd " )
4549 cwd_icon=$( get_tmux_option " @tmux2k-cwd-icon" " " )
4650
4751 echo " $cwd_icon $truncated_cwd "
4852}
4953
50- # run main driver program
5154main
0 commit comments