generated from 2KAbhishek/bare-minimum
-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
cwd.sh
executable file
·47 lines (39 loc) · 1.28 KB
/
cwd.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env bash
# return current working directory of tmux pane
get_pane_dir() {
nextone="false"
ret=""
for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); do
[ "$i" == "1" ] && nextone="true" && continue
[ "$i" == "0" ] && nextone="false"
[ "$nextone" == "true" ] && ret+="$i "
done
echo "${ret%?}"
}
# truncate the path if it's longer than 30 characters
truncate_path() {
local path="$1" limit=20 truncated_path=""
# If path is greater than limit, then truncate parts to 2 characters
[[ ${#path} -le $limit ]] && echo "$path" && return
IFS='/' read -ra parts <<< "$path"
for ((i=0; i<${#parts[@]}-1; i++)); do
truncated_path+="${parts[i]:0:2}/"
done
truncated_path+="${parts[-1]}"
# If there are more than 4 slashes, then we will truncate the middle part
if [[ $(tr -cd '/' <<< "$truncated_path" | wc -c) -gt 4 ]]; then
IFS='/' read -ra parts <<< "$truncated_path"
echo "${parts[0]}/${parts[1]}/.../${parts[-2]}/${parts[-1]}"
else
echo "$truncated_path"
fi
}
main() {
path=$(get_pane_dir)
# Change '/home/user' to '~'
cwd="${path/"$HOME"/'~'}"
truncated_cwd=$(truncate_path "$cwd")
echo " $truncated_cwd"
}
#run main driver program
main