feat: add current working directory feature (#20)

This commit is contained in:
Goodwill Hacking 2024-10-30 20:03:33 -05:00 committed by GitHub
parent 4646757c9e
commit 4692ae758e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 57 additions and 0 deletions

55
scripts/cwd.sh Executable file
View file

@ -0,0 +1,55 @@
#!/usr/bin/env bash
# return current working directory of tmux pane
getPaneDir() {
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
truncatePath() {
local path="$1"
local limit=30
if [ ${#path} -gt $limit ]; then
# Split the path into an array by '/'
IFS='/' read -r -a path_array <<< "$path"
truncated_path=""
for ((i=0; i<${#path_array[@]}-1; i++)); do
if [ ${#path_array[i]} -gt 1 ]; then
truncated_path+="${path_array[i]:0:1}/"
else
truncated_path+="${path_array[i]}/"
fi
done
# Add the last component of the current directory path
truncated_path+="${path_array[-1]}"
echo "$truncated_path"
else
echo "$path"
fi
}
main() {
path=$(getPaneDir)
# change '/home/user' to '~'
cwd="${path/"$HOME"/'~'}"
# Truncate path if it's too long
truncated_cwd=$(truncatePath "$cwd")
echo "$truncated_cwd"
}
#run main driver program
main