tmux2k/scripts/cwd.sh

48 lines
1.3 KiB
Bash
Raw Permalink Normal View History

#!/usr/bin/env bash
# return current working directory of tmux pane
2024-10-31 06:58:06 +05:30
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
2024-10-31 06:58:06 +05:30
truncate_path() {
2024-11-04 00:30:04 +05:30
local path="$1" limit=20 truncated_path=""
2024-10-31 06:58:06 +05:30
2024-11-04 00:30:04 +05:30
# 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]}"
2024-10-31 06:58:06 +05:30
2024-11-04 00:30:04 +05:30
# 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]}"
2024-10-31 06:58:06 +05:30
else
2024-11-04 00:30:04 +05:30
echo "$truncated_path"
2024-10-31 06:58:06 +05:30
fi
}
main() {
2024-10-31 06:58:06 +05:30
path=$(get_pane_dir)
2024-11-04 00:30:04 +05:30
# Change '/home/user' to '~'
2024-10-31 06:58:06 +05:30
cwd="${path/"$HOME"/'~'}"
truncated_cwd=$(truncate_path "$cwd")
2024-10-31 07:07:42 +05:30
echo "$truncated_cwd"
}
#run main driver program
main