Skip to content

Commit 13c05a2

Browse files
authored
Merge branch '2KAbhishek:main' into patch-2
2 parents b653ba9 + 59d7377 commit 13c05a2

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ Show CPU temperature
176176
Show current working directory
177177

178178
- `tmux2k-cwd-icon`: Icon for directory, default: ``
179+
- `tmux2k-cwd-length`: Maximum path length before truncation, default: `20`
180+
- `tmux2k-cwd-min-depth`: Minimum directory depth before mid-truncation, default: `4`
181+
- `tmux2k-cwd-prefix-chars`: Characters to keep from each directory name when truncating, default: `2`
179182

180183
#### 6. `git`
181184

plugins/cwd.sh

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
44
source "$current_dir/../lib/utils.sh"
55

6-
# return current working directory of tmux pane
76
get_pane_dir() {
87
nextone="false"
98
ret=""
@@ -15,37 +14,41 @@ get_pane_dir() {
1514
echo "${ret%?}"
1615
}
1716

18-
# truncate the path if it's longer than 30 characters
1917
truncate_path() {
20-
local path="$1" limit=20 truncated_path=""
18+
local path="$1"
19+
local max_length=$(get_tmux_option "@tmux2k-cwd-length" 40)
20+
local min_depth=$(get_tmux_option "@tmux2k-cwd-min-depth" 4)
21+
local prefix_chars=$(get_tmux_option "@tmux2k-cwd-prefix-chars" 2)
22+
23+
[[ ${#path} -le $max_length ]] && echo "$path" && return
2124

22-
# If path is greater than limit, then truncate parts to 2 characters
23-
[[ ${#path} -le $limit ]] && echo "$path" && return
2425
IFS='/' read -ra parts <<<"$path"
26+
local truncated=""
27+
2528
for ((i = 0; i < ${#parts[@]} - 1; i++)); do
26-
truncated_path+="${parts[i]:0:2}/"
29+
truncated+="${parts[i]:0:$prefix_chars}/"
2730
done
28-
truncated_path+="${parts[-1]}"
31+
truncated+="${parts[-1]}"
32+
33+
[[ ${#truncated} -le $max_length ]] && echo "$truncated" && return
2934

30-
# If there are more than 4 slashes, then we will truncate the middle part
31-
if [[ $(tr -cd '/' <<<"$truncated_path" | wc -c) -gt 4 ]]; then
32-
IFS='/' read -ra parts <<<"$truncated_path"
35+
local slash_count=$(tr -cd '/' <<<"$truncated" | wc -c)
36+
if [[ $slash_count -gt $min_depth ]]; then
37+
IFS='/' read -ra parts <<<"$truncated"
3338
echo "${parts[0]}/${parts[1]}/.../${parts[-2]}/${parts[-1]}"
3439
else
35-
echo "$truncated_path"
40+
echo "$truncated"
3641
fi
3742
}
3843

3944
main() {
4045
path=$(get_pane_dir)
4146

42-
# Change '/home/user' to '~'
4347
cwd="${path/"$HOME"/'~'}"
4448
truncated_cwd=$(truncate_path "$cwd")
4549
cwd_icon=$(get_tmux_option "@tmux2k-cwd-icon" "")
4650

4751
echo "$cwd_icon $truncated_cwd"
4852
}
4953

50-
#run main driver program
5154
main

0 commit comments

Comments
 (0)