Skip to content

Instantly share code, notes, and snippets.

@ObjectBoxPC
Created June 10, 2024 12:46
Show Gist options
  • Save ObjectBoxPC/ca8bc25ce95ec895238060bbca1be17d to your computer and use it in GitHub Desktop.
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)
#!/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