From 4692ae758ee86ad0c2b6fa240e0d5d46cd3cad57 Mon Sep 17 00:00:00 2001 From: Goodwill Hacking Date: Wed, 30 Oct 2024 20:03:33 -0500 Subject: [PATCH] feat: add current working directory feature (#20) --- scripts/cwd.sh | 55 +++++++++++++++++++++++++++++++++++++++++++++++ scripts/tmux2k.sh | 2 ++ 2 files changed, 57 insertions(+) create mode 100755 scripts/cwd.sh diff --git a/scripts/cwd.sh b/scripts/cwd.sh new file mode 100755 index 0000000..8e91928 --- /dev/null +++ b/scripts/cwd.sh @@ -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 diff --git a/scripts/tmux2k.sh b/scripts/tmux2k.sh index 0cdf1c7..e467989 100755 --- a/scripts/tmux2k.sh +++ b/scripts/tmux2k.sh @@ -39,6 +39,7 @@ light_purple=$(get_tmux_option "@tmux2k-light-purple" '#ff65c6') declare -A plugin_colors=( ["git"]="green text" ["cpu"]="blue text" + ["cwd"]="purple text" ["ram"]="light_yellow text" ["gpu"]="yellow text" ["battery"]="light_purple text" @@ -155,6 +156,7 @@ set_theme() { plugin_colors=( ["git"]="text green" ["cpu"]="text blue" + ["cwd"]="text purple" ["ram"]="text light_yellow" ["gpu"]="text yellow" ["battery"]="text light_purple"