Created
June 10, 2024 12:46
-
-
Save ObjectBoxPC/ca8bc25ce95ec895238060bbca1be17d to your computer and use it in GitHub Desktop.
Print the working directory in a compact form (originally created for tmux status bar customization)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# Print a compact version of the current working directory. | |
# The home directory is replaced with ~, and long paths are truncated to the final part with < as a marker. | |
# Examples: | |
# /home/example/Documents becomes ~/Documents | |
# /example/of/a/really/really/long/path becomes <ly/really/long/path | |
# The maximum length can be set using the LIMIT variable. | |
LIMIT=20 | |
HOME_REGEX=$(echo "$HOME" | sed 's/\//\\\//g') | |
TRANSLATED_PWD=$(echo "$PWD" | sed "s/^$HOME_REGEX/~/") | |
PWD_LENGTH=$(printf "%s" "$TRANSLATED_PWD" | wc -m) | |
if [ $PWD_LENGTH -lt $LIMIT ]; then | |
echo "$TRANSLATED_PWD" | |
else | |
CUT_LIMIT=$(($LIMIT - 1)) | |
echo "<$(echo "$TRANSLATED_PWD" | cut -c $(($PWD_LENGTH - $CUT_LIMIT + 1))-)" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment