docs: add current working dir plugin

This commit is contained in:
Abhishek Keshri 2024-10-31 06:58:06 +05:30
parent 4692ae758e
commit 52e3405b67
No known key found for this signature in database
2 changed files with 38 additions and 37 deletions

View file

@ -110,6 +110,7 @@ set -g @tmux2k-yellow '#f8c800' # change yellow color
- `battery`: Show battery stats and percentage - `battery`: Show battery stats and percentage
- `git`: Show Git branch and status information - `git`: Show Git branch and status information
- `cpu`: Show CPU usage information - `cpu`: Show CPU usage information
- `cwd`: Show current working directory
- `gpu`: Show GPU usage information - `gpu`: Show GPU usage information
- `ram`: Show RAM usage information - `ram`: Show RAM usage information
- `network`: Show network status and statistics - `network`: Show network status and statistics

View file

@ -1,54 +1,54 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# return current working directory of tmux pane # return current working directory of tmux pane
getPaneDir() { get_pane_dir() {
nextone="false" nextone="false"
ret="" ret=""
for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); do for i in $(tmux list-panes -F "#{pane_active} #{pane_current_path}"); do
[ "$i" == "1" ] && nextone="true" && continue [ "$i" == "1" ] && nextone="true" && continue
[ "$i" == "0" ] && nextone="false" [ "$i" == "0" ] && nextone="false"
[ "$nextone" == "true" ] && ret+="$i " [ "$nextone" == "true" ] && ret+="$i "
done done
echo "${ret%?}" echo "${ret%?}"
} }
# truncate the path if it's longer than 30 characters # truncate the path if it's longer than 30 characters
truncatePath() { truncate_path() {
local path="$1" local path="$1"
local limit=30 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} -gt $limit ]; then
if [ ${#path_array[i]} -gt 1 ]; then # Split the path into an array by '/'
truncated_path+="${path_array[i]:0:1}/" IFS='/' read -r -a path_array <<<"$path"
else truncated_path=""
truncated_path+="${path_array[i]}/"
fi
done
# Add the last component of the current directory path for ((i = 0; i < ${#path_array[@]} - 1; i++)); do
truncated_path+="${path_array[-1]}" if [ ${#path_array[i]} -gt 1 ]; then
truncated_path+="${path_array[i]:0:1}/"
else
truncated_path+="${path_array[i]}/"
fi
done
echo "$truncated_path" # Add the last component of the current directory path
else truncated_path+="${path_array[-1]}"
echo "$path"
fi echo "$truncated_path"
else
echo "$path"
fi
} }
main() { main() {
path=$(getPaneDir) path=$(get_pane_dir)
# change '/home/user' to '~' # change '/home/user' to '~'
cwd="${path/"$HOME"/'~'}" cwd="${path/"$HOME"/'~'}"
# Truncate path if it's too long
truncated_cwd=$(truncatePath "$cwd")
echo "$truncated_cwd" # Truncate path if it's too long
truncated_cwd=$(truncate_path "$cwd")
echo "$truncated_cwd"
} }
#run main driver program #run main driver program