This commit is contained in:
Shubham Tiwary 2025-03-25 19:04:45 +02:00 committed by GitHub
commit c2e499453b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 88 additions and 0 deletions

30
plugins/pathfmt/README.md Normal file
View File

@ -0,0 +1,30 @@
## Pathfmt plugin
This plugin adds current path information (ex: `~/Downloads/images/...`) into your terminal.
To use it, add `pathfmt` to the plugins array of your zshrc file:
```
plugins=(... pathfmt ...)
```
### Configuration (required)
Add these into your zshrc file:
```
#options = absolute/user/host
PATHFMT_MODE="user"
RPROMPT='$(path_format)'
```
<br>
### Things to Note:
`PATHFMT_MODE` has three modes:
```
1. absolute - full path (from /).
2. user - path from $USER/<path>
3. host - user@machine/<path>
```
Requires [`inetutils`]([url](https://launchpad.net/ubuntu/+source/inetutils)) for the host mode
> may show: user@unknown when not found & in host mode

View File

@ -0,0 +1,58 @@
# provides a function `path_format` that displays the current working directory (on right side)
# in different formats based on the value of the variable PATHFMT_MODE.
#
# Supported formats:
# - absolute: full absolute path (default).
# - user: Replaces your $HOME with your username (e.g. "ubuntu/dir..").
# - host: Displays as "user@hostname:/path", using ~ for home when applicable.
#
# Configure the mode in your ~/.zshrc, e.g.:
# PATHFMT_MODE="host"
# plugins=(... pathfmt ...)
#
# and re-load config with source ~/.zshrc
# default=user
: ${PATHFMT_MODE:=user}
function path_user() {
# user in home? replace with ~/path
if [[ "$PWD" == "$HOME"* ]]; then
echo "~${PWD#$HOME}"
else
echo "$PWD"
fi
}
function path_format() {
case $PATHFMT_MODE in
absolute)
#full path from /
echo "$PWD"
;;
user)
path_user
;;
host)
#*check if hostname found (requires: inetutils' hostname command)
local host
if command -v hostname >/dev/null 2>&1; then
host=$(hostname)
else
host="unknown"
fi
# path: user@hostname:path, using ~ if in home
if [[ "$PWD" == "$HOME"* ]]; then
echo "${USER}@${host}:~${PWD#$HOME}"
else
echo "${USER}@${host}:$PWD"
fi
;;
*)
#unrecognized: default fallback as user
path_user
;;
esac
}