Skip to content

Commit

Permalink
Allow fine-grained customization of command line prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuru committed Jan 21, 2022
1 parent 1149442 commit 8b274dd
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion rootfs/etc/profile.d/_workdir.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ else
fi

function file_on_host() {
[[ $GEODESIC_LOCALHOST_MISSING != "true" ]] && [[ $(_file_device "$1") == ${GEODESIC_LOCALHOST_DEVICE} ]]
[[ $GEODESIC_LOCALHOST_MISSING != "true" ]] && [[ $(_file_device "$1") == ${GEODESIC_LOCALHOST_DEVICE} ]]
}

function _default_initial_wd() {
Expand Down
12 changes: 6 additions & 6 deletions rootfs/etc/profile.d/atmos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ function _configure_atmos_base_path() {

# If $GEODESIC_WORKDIR is a descendent of either a "stacks" or "components" directory,
# use the parent of that directory as ATMOS_BASE_PATH
if [[ "${GEODESIC_WORKDIR}" =~ /(stacks|components)/ ]]; then
if [[ "${GEODESIC_WORKDIR}" =~ /stacks/ ]]; then
export ATMOS_BASE_PATH="${GEODESIC_WORKDIR%/stacks/*}"
else
export ATMOS_BASE_PATH="${GEODESIC_WORKDIR%/components/*}"
fi
if [[ "${GEODESIC_WORKDIR}" =~ /(stacks|components)/ ]]; then
if [[ "${GEODESIC_WORKDIR}" =~ /stacks/ ]]; then
export ATMOS_BASE_PATH="${GEODESIC_WORKDIR%/stacks/*}"
else
export ATMOS_BASE_PATH="${GEODESIC_WORKDIR%/components/*}"
fi
green "# Setting ATMOS_BASE_PATH to \"$ATMOS_BASE_PATH\" based on parent of workdir"
return
fi
Expand Down
40 changes: 22 additions & 18 deletions rootfs/etc/profile.d/prompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,37 +61,40 @@ function geodesic_prompt() {
plain)
# 8859-1 codepoints:
# '\[' and '\]' are bash prompt delimiters around non-printing characters
ASSUME_ROLE_ACTIVE_MARK="\["$(tput bold)$(tput setab 2)"\]»\["$(tput sgr0)"\] " # green
ASSUME_ROLE_INACTIVE_MARK=$'· '
BLACK_RIGHTWARDS_ARROWHEAD=$'=> '
BANNER_MARK=$'§ '
[[ -z $ASSUME_ROLE_ACTIVE_MARK ]] && ASSUME_ROLE_ACTIVE_MARK="\["$(tput bold)$(tput setab 2)"\]»\["$(tput sgr0)"\]" # green
[[ -z $ASSUME_ROLE_INACTIVE_MARK ]] && ASSUME_ROLE_INACTIVE_MARK=$'·'
[[ -z $BLACK_RIGHTWARDS_ARROWHEAD ]] && BLACK_RIGHTWARDS_ARROWHEAD=$'=>'
[[ -z $BANNER_MARK ]] && BANNER_MARK=$'§'
;;

unicode)
# unicode
ASSUME_ROLE_ACTIVE_MARK=$'\u2705 ' # '✅'
ASSUME_ROLE_INACTIVE_MARK=$'\u274C ' # '❌'
BLACK_RIGHTWARDS_ARROWHEAD=$'\u27A4 ' # '➤', suggest '▶' may be present in more fonts
BANNER_MARK=$'\u29C9 ' # '⧉'
[[ -z $ASSUME_ROLE_ACTIVE_MARK ]] && ASSUME_ROLE_ACTIVE_MARK=$'\u2705' # '✅'
[[ -z $ASSUME_ROLE_INACTIVE_MARK ]] && ASSUME_ROLE_INACTIVE_MARK=$'\u274C' # '❌'
[[ -z $BLACK_RIGHTWARDS_ARROWHEAD ]] && BLACK_RIGHTWARDS_ARROWHEAD=$'\u27A4' # '➤', suggest '▶' may be present in more fonts
[[ -z $BANNER_MARK ]] && BANNER_MARK=$'\u29C9' # '⧉'
;;

*)
# default
# ASSUME_ROLE_ACTIVE_MARK=$' \x01'$(tput bold)$(tput setaf 2)$'\x02\u2713 \x01'$(tput sgr0)$'\x02' # green bold '✓'
ASSUME_ROLE_ACTIVE_MARK=$' \x01'$(tput bold)$(tput setaf 2)$'\x02\u221a \x01'$(tput sgr0)$'\x02' # green bold '√'
ASSUME_ROLE_INACTIVE_MARK=$' \x01'$(tput bold)$(tput setaf 1)$'\x02\u2717 \x01'$(tput sgr0)$'\x02' # red bold '✗'
# ASSUME_ROLE_ACTIVE_MARK=$'\x01'$(tput bold)$(tput setaf 2)$'\x02\u2713\x01'$(tput sgr0)$'\x02' # green bold '✓'
[[ -z $ASSUME_ROLE_ACTIVE_MARK ]] && ASSUME_ROLE_ACTIVE_MARK=$'\x01'$(tput bold)$(tput setaf 2)$'\x02\u221a\x01'$(tput sgr0)$'\x02' # green bold '√'
[[ -z $ASSUME_ROLE_INACTIVE_MARK ]] && ASSUME_ROLE_INACTIVE_MARK=$'\x01'$(tput bold)$(tput setaf 1)$'\x02\u2717\x01'$(tput sgr0)$'\x02' # red bold '✗'
# Options for arrow per https://github.com/cloudposse/geodesic/issues/417#issuecomment-477836676
# '»' ($'\u00bb') RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK from the Latin-1 supplement Unicode block
# '≫' ($'\u226b') MUCH GREATER-THAN and
# '⋙' ($'\u22d9') VERY MUCH GREATER-THAN which are from the Mathematical Operators Unicode block
# '➤' ($'\u27a4') BLACK RIGHTWARDS ARROWHEAD from the Dingbats Unicode block
# '▶︎' ($'\u25b6\ufe0e') BLACK RIGHT-POINTING TRIANGLE which is sometimes presented as an emoji (as GitHub likes to) '▶️'
# '⏩︎' ($'\u23e9\ufe0e') BLACK RIGHT-POINTING DOUBLE TRIANGLE
BLACK_RIGHTWARDS_ARROWHEAD=$'\u2a20 ' # '⨠' Z NOTATION SCHEMA PIPING
BANNER_MARK=' '
[[ -z $BLACK_RIGHTWARDS_ARROWHEAD ]] && BLACK_RIGHTWARDS_ARROWHEAD=$'\u2a20' # '⨠' Z NOTATION SCHEMA PIPING
[[ -z $BANNER_MARK ]] && BANNER_MARK=''
;;
esac

# "(HOST)" with "HOST" in bold red
[[ -z $HOST_PROMPT_MARK ]] && HOST_PROMPT_MARK=$'(\x01'$(tput bold)$(tput setaf 1)$'\x02HOST\x01'$(tput sgr0)$'\x02)'

local level_prompt
case $SHLVL in
1) level_prompt='.' ;;
Expand Down Expand Up @@ -123,13 +126,13 @@ function geodesic_prompt() {
done

local dir_prompt
dir_prompt="${STATUS}${level_prompt} "
dir_prompt=" ${STATUS} ${level_prompt} "
if file_on_host "$(pwd -P)"; then
dir_prompt+="${ROLE_PROMPT} ("$'\x01'$(tput bold)$(tput setaf 1)$'\x02HOST\x01'$(tput sgr0)$'\x02'") \W "
dir_prompt+="${ROLE_PROMPT} ${HOST_PROMPT_MARK} \W "
else
dir_prompt+="${ROLE_PROMPT} \W "
fi
dir_prompt+=$'${GEODISIC_PROMPT_GLYPHS-$BLACK_RIGHTWARDS_ARROWHEAD}'
dir_prompt+=$'${GEODISIC_PROMPT_GLYPHS-$BLACK_RIGHTWARDS_ARROWHEAD} '

update_terraform_prompt
local old_kube_ps1_prefix="$KUBE_PS1_PREFIX"
Expand All @@ -143,7 +146,7 @@ function geodesic_prompt() {
tf_mark="${ASSUME_ROLE_ACTIVE_MARK}"
fi
if [[ -n ${GEODESIC_TF_PROMPT_LINE} ]]; then
tf_prompt="${tf_mark}${GEODESIC_TF_PROMPT_LINE}\n"
tf_prompt=" ${tf_mark} ${GEODESIC_TF_PROMPT_LINE}\n"
fi
if [[ $GEODESIC_TERRAFORM_WORKSPACE_PROMPT_ENABLED == "true" ]]; then
KUBE_PS1_PREFIX="$(yellow "cluster:")("
Expand All @@ -154,7 +157,8 @@ function geodesic_prompt() {
fi

if [ -n "${BANNER}" ]; then
PS1=$' ${BANNER_MARK}'" ${BANNER} $(kube_ps1)${secrets_active}\n${tf_prompt}${dir_prompt}"
# Intentional 2 spaces between banner mark and banner in order to offset it from level prompt
PS1=$' ${BANNER_MARK}'" ${BANNER} $(kube_ps1)${secrets_active}\n${tf_prompt}${dir_prompt}"
else
PS1="${tf_prompt}${dir_prompt}"
fi
Expand Down
6 changes: 3 additions & 3 deletions rootfs/templates/wrapper
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ function use() {
else
echo "# Mounting ${local_home} into container with workdir ${GEODESIC_HOST_CWD}"
DOCKER_ARGS+=(
--volume="${local_home}:/localhost:"
--volume="${local_home}:/localhost"
--env LOCAL_HOME="${local_home}"
)
fi
Expand All @@ -171,7 +171,7 @@ function use() {
--env DOCKER_IMAGE="${DOCKER_IMAGE%:*}"
--env DOCKER_NAME="${DOCKER_NAME}"
--env DOCKER_TAG="${DOCKER_TAG}"
--env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}"
--env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}"
)

trap run_exit_hooks EXIT
Expand All @@ -182,7 +182,7 @@ function use() {
if [ $# -eq 0 ]; then
set -- "/bin/bash" "-l" "$@"
fi
docker exec -it --env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}" "${DOCKER_NAME}" $*
docker exec -it --env GEODESIC_HOST_CWD="${GEODESIC_HOST_CWD}" "${DOCKER_NAME}" $*
else
echo "# Starting new ${DOCKER_NAME} session from ${DOCKER_IMAGE}"
echo "# Exposing port ${GEODESIC_PORT}"
Expand Down

0 comments on commit 8b274dd

Please sign in to comment.