173 lines
5.0 KiB
Bash
Executable File
173 lines
5.0 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
set -e
|
|
|
|
XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
|
|
|
|
# Derive name from the session type
|
|
|
|
get_from_session() {
|
|
SESSION_TYPE="$(slatectx-guess-session-type)"
|
|
|
|
case "$SESSION_TYPE" in
|
|
wayland)
|
|
printf "y%s-%s\n" "$( printf "%s" "$WAYLAND_DISPLAY" | sed 's/^wayland-//' )" "$(slatectx-get-workspace)" ;;
|
|
x11)
|
|
printf "x%s-%s\n" "$( printf "%s" "$WAYLAND_DISPLAY" | sed 's/^://' )" "$(slatectx-get-workspace)" ;;
|
|
tmux)
|
|
printf "t-%s\n" "$( printf "%s" "$TMUX" | awk -F, '{ print $2 }' )" ;;
|
|
xdg-*)
|
|
printf "%s-%s\n" "$SESSION_TYPE" "$XDG_SESSION_ID" ;;
|
|
*)
|
|
printf "%s\n" "$SESSION_TYPE";;
|
|
esac
|
|
}
|
|
|
|
get_label_from_path() {
|
|
cat "$XDG_CONFIG_HOME/slatectx/ctx-from-path.rules" "$XDG_CONFIG_HOME/slatectx/ctx-from-path.$(hostname).rules" 2>&- | \
|
|
grep -v '^#' | \
|
|
awk -v "here=${1/#$HOME/\~}" '{ match(here, $0, out); if (out[1]) { name=out[1] ; for ( i=2; out[i] ; i++) { name = name "///" out[i] } ; print name ; exit 0 } }'
|
|
}
|
|
|
|
shorten() {
|
|
# This is not supposed to see untrusted data
|
|
# it must be fast and produce a short output
|
|
md5sum | sed -E 's/(.{8}).*/_\1/'
|
|
}
|
|
|
|
show_help() {
|
|
cat <<EOF
|
|
Usage: slatectx-get-context-name [auto|by-session|by-path] [options...]
|
|
|
|
slatectx-get-context-name derives an identifier for a slatetx context based on the current envoirenment.
|
|
|
|
The context name can be based on the current session and workspace or on the current path (either the real pwd or slatectx-pwd).
|
|
|
|
COMMANDS:
|
|
Note, that only one of them can be used,
|
|
if multiple are specified the last one is used and others are ignored
|
|
|
|
by-session
|
|
Only derive the context name from the current session.
|
|
|
|
by-path
|
|
Only derive the context name from the given path (defaults to the pwd).
|
|
Will return an empty result if the given path does not map to a context name.
|
|
|
|
auto
|
|
The current default, will try deriving a context name
|
|
from the session and then use that information with
|
|
slatectx-pwd to get a more specific by path context.
|
|
If the path lookup fails, it falls back to a by-session.
|
|
|
|
OPTIONS:
|
|
--not-by-path
|
|
Avoids path information for context name derivation.
|
|
Used by slatectx-pwd. Currently equivalent to by-session
|
|
|
|
--not-by-session
|
|
Avoids the session information for context name derivation.
|
|
|
|
--path <path>
|
|
Manually give a path to be used instead of one derived from
|
|
the pwd or slatectx-pwd
|
|
|
|
--label
|
|
Itended for debugging.
|
|
Instead of hashing, print out the cleartext label,
|
|
derived from the path.
|
|
Do NOT use this for identifying contexts.
|
|
|
|
--no-override
|
|
Disable the SLATECTX_CONTEXT_NAME envoirenment variable.
|
|
Do NOT turn this off when using this command for figuring out the
|
|
current context.
|
|
The intention behind this option is to make the logic in this script
|
|
useable for more sophisticated scripts operting across contexts.
|
|
|
|
CONFIGURATION FILES:
|
|
|
|
Note: \$XDG_CONFIG_HOME is usually your ~/.config
|
|
|
|
\$XDG_CONFIG_HOME/slatectx/ctx-from-path.rules
|
|
\$XDG_CONFIG_HOME/slatectx/ctx-from-path.<hostname>.rules
|
|
These files contain one (awk) regex per line.
|
|
When a regex matches the caputure groups will be
|
|
joined together to form a label for the context.
|
|
(which will be hashed to keep them in a resonable
|
|
length with known save characters)
|
|
Lines starting with a # will be treated as comments.
|
|
|
|
Examples:
|
|
# A ~/Notes folder gets its own context
|
|
(^~/Notes)
|
|
# Some folders in our \$HOME have a context for each subfolder
|
|
^~/(Development|Documents|Experiments)/([^/*]*)
|
|
|
|
ENVOIRNMENT:
|
|
SLATECTX_CONTEXT_NAME
|
|
if set its contents will be returned
|
|
This behaviour can be disabled with the --no-override option.
|
|
|
|
EOF
|
|
}
|
|
|
|
COMMAND="auto"
|
|
PATH_ARG="$PWD"
|
|
GET_LABEL=""
|
|
USE_SLATECTX_PWD="y"
|
|
|
|
while [[ "$#" -gt 0 ]]; do
|
|
case "$1" in
|
|
by-session|--not-by-path) COMMAND="from-session"; shift 1;;
|
|
by-path|--not-by-session) COMMAND="from-path"; shift 1;;
|
|
auto) COMMAND="auto"; shift 1;;
|
|
|
|
--no-override) SLATECTX_CONTEXT_NAME=""; shift 1;;
|
|
|
|
--label) GET_LABEL="y"; shift 1;;
|
|
|
|
--path) PATH_ARG="$2"; USE_SLATECTX_PWD=""; shift 2;;
|
|
|
|
--help) show_help; exit 0;;
|
|
*) printf "Unknown option: %s\n" "$1" >&2; exit 1;;
|
|
esac
|
|
done
|
|
|
|
# If we already have a context name from the envoirenment, return it
|
|
|
|
if [ -n "$SLATECTX_CONTEXT_NAME" ]; then
|
|
printf "%s\n" "$SLATECTX_CONTEXT_NAME"
|
|
exit 0
|
|
fi
|
|
|
|
PATH_ARG="$(realpath "${PATH_ARG/#~/$HOME}" 2>&-)"
|
|
|
|
case "$COMMAND" in
|
|
from-session) get_from_session ;;
|
|
from-path)
|
|
LABEL="$(get_label_from_path "$PATH_ARG")"
|
|
if [[ -n "$GET_LABEL" ]] || [[ -z "$LABEL" ]] ; then
|
|
printf "%s\n" "$LABEL"
|
|
else
|
|
printf "%s" "$LABEL" | shorten
|
|
fi
|
|
;;
|
|
auto)
|
|
SLATECTX_CONTEXT_NAME="$(get_from_session)"
|
|
[ -n "$USE_SLATECTX_PWD" ] && PATH_ARG="$(slatectx-pwd get --context "$SLATECTX_CONTEXT_NAME" || true)"
|
|
if [ -n "$PATH_ARG" ] && [ -d "$PATH_ARG" ]; then
|
|
PWD_LABEL="$(get_label_from_path "$PATH_ARG")"
|
|
if [ -n "$PWD_LABEL" ]; then
|
|
if [ -n "$GET_LABEL" ] ; then
|
|
printf "%s\n" "$PWD_LABEL"
|
|
else
|
|
printf "%s" "$PWD_LABEL" | shorten
|
|
fi
|
|
exit
|
|
fi
|
|
fi
|
|
printf "%s\n" "$SLATECTX_CONTEXT_NAME"
|
|
;;
|
|
esac
|